我正在尝试使用数组。这是一些工作代码,解释了我正在做的事情。
// query
var a = ["1000", "2000", "3000"];
var b = ["2000"];
for (i in b) {
var index = a.indexOf(b[i]);
};
if (index > -1) {
a.splice(index, 1);
};
a
现在,当我对查询结果使用相同的逻辑时,它不再起作用。
// query
queryDo = cts.andQuery([
cts.jsonPropertyValueQuery("displayable", "true"),
cts.jsonPropertyValueQuery("section", "dikw Track Events"),
cts.jsonPropertyValueQuery("name", 'dikw_default'),
cts.collectionQuery(["reference/application"])
]);
queryDont = cts.andQuery([
cts.jsonPropertyValueQuery("displayable", "false"),
cts.jsonPropertyValueQuery("section", "dikw Track Events"),
cts.jsonPropertyValueQuery("name", 'Helpdesk'),
cts.collectionQuery(["reference/application"])
]);
var qDo = cts.jsonPropertyWords("code", null, "document", queryDo).toArray();
var qDont = cts.jsonPropertyWords("code", null, "document", queryDont).toArray();
for (i in qDont) {
var index = qDo.indexOf(qDont[i]);
};
if (index > -1) {
qDo.splice(index, 1);
};
qDo
我已经验证两个查询的结果都是一个数组。数组由值组成,就像示例代码一样; 1000、2000 等 另外,当我使用 notAndQuery 从第一个查询中排除第二个查询的结果时,这没有效果。
例如,当我查看 qDo[2] 时,会返回正确的值。
我的 andNotQuery:
queryDo = cts.andQuery([
cts.jsonPropertyValueQuery("displayable", "true"),
cts.jsonPropertyValueQuery("section", "dikw Track Events"),
cts.jsonPropertyValueQuery("name", 'dikw_default'),
cts.collectionQuery(["reference/application"])
]);
queryDont = cts.andQuery([
cts.jsonPropertyValueQuery("displayable", "false"),
cts.jsonPropertyValueQuery("section", "dikw Track Events"),
cts.jsonPropertyValueQuery("name", 'Helpdesk'),
cts.collectionQuery(["reference/application"])
]);
andnot = cts.andNotQuery(queryDo, queryDont);
result = cts.jsonPropertyWords("code", null, "document", andnot);
最佳答案
for/in 运算符可能不会按照您期望的方式迭代数组:
“不能保证 for...in 将以任何特定顺序返回索引,并且它将返回所有可枚举属性,包括具有非整数名称的属性和继承的属性。”
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
此外,for/in 可能会抑制 v8 中的优化:
https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#5-for-in
如果您使用熟悉的for (var i=0; i < array.length; i++) {...}
声明,这会产生预期的结果吗?
希望有帮助,
关于MarkLogic 8 JavaScript 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32346089/