2

我正在尝试使用数组。这是一些解释我在做什么的工作代码。

// 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);
4

3 回答 3

3

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++) {...}陈述,那会产生预期的结果吗?

希望有帮助,

于 2015-09-03T14:40:20.727 回答
2

我认为你的逻辑有缺陷。最后的 if 似乎放错了地方。如果您使用数字扩展示例以包含另一个值,则会出错:

var a = ["1000", "2000", "3000", "4000"];
var b = ["2000", "3000"];    

for (i in b) {
  var index = a.indexOf(b[i]);
};

if (index > -1) {
    a.splice(index, 1);
};

a

相反, if 应该在 for 循环中:

var a = ["1000", "2000", "3000", "4000"];
var b = ["2000", "3000"];    

for (i in b) {
  var index = a.indexOf(b[i]);

  if (index > -1) {
      a.splice(index, 1);
  };
};

a

但是以某种方式探索使用 notQuery 可能是值得的。根据您的数据布局,这可能会起作用。这也会更有效率,因为您不必在搜索后进行自定义过滤。

于 2015-09-02T07:05:04.113 回答
0

After filing a bug at MarkLogic I got this repsonse from the engineering team:

"The return from the lexicon call is a StringWithFrequency (not just a String) so the JS layer wraps it"

You can create an Array from the call to cts.jsonPropertyWords as you did before, but you'll need to iterate through and get the string values for each item in turn first. Something like this:

  1. Create the Array from cts.jsonPropertyWords as before
  2. Create a new Array
  3. Iterate through the original Array and push each items's string value into the new Array. The call to indexOf will work on the new Array.

Work around

var trackCodes = cts.jsonPropertyWords("code", null, "document", cts.andQuery([])).toArray();
var tc2 = new Array();

for (var i = 0; i < trackCodes.length; i++){
  tc2.push(trackCodes[i].toString())
}

tc2.indexOf(["your value"])
于 2016-03-18T08:26:40.110 回答