包含对象逻辑的 Javascript 数组失败。出了什么问题?
我有两个数组,其中一个数组保存另一个数组的所有唯一值。第二个数组保存在另一个数组中找到的重复值的唯一值。
下面的代码循环遍历第一个数组,并检查该数组索引中的值是否与第二个数组索引中的值匹配,以查找被欺骗的项目。如果是这样,它将在项目文本之前添加一个箭头。
这似乎工作不正常,它正在打印第一个数组,而不是检查重复的项目。
var aos = ["a","a","b","c","d","e","f","f","f","g","g","g","h","h","h"];
// Credits to the guy who helped me with this, internet w00t!
var duplicates = _.select(aos, function(val) {
var True = _.after(2, function() {
return true;
});
return _.any(aos, function(elem) {
return elem === val ? True() : false;
});
});
aos.sort();
aos = _.unique(aos);
var dennis = _.unique(duplicates);
if ($.inArray(aos[i], dennis)) {
console.log("CONTAINS!", dennis, aos[i]);
$("tr[number='" + i + "']").find("td:first-child").prepend("<img src='images/arrow.jpg' style='padding-right: 5px;'/>");
} else
console.log("no!");
输出:
no!
CONTAINS! ["a", "f", "g", "h"] b
CONTAINS! ["a", "f", "g", "h"] c
CONTAINS! ["a", "f", "g", "h"] d
CONTAINS! ["a", "f", "g", "h"] e
CONTAINS! ["a", "f", "g", "h"] f
CONTAINS! ["a", "f", "g", "h"] g
CONTAINS! ["a", "f", "g", "h"] h
输出应该是什么:
CONTAINS!
no!
no!
no!
no!
CONTAINS!
CONTAINS!
CONTAINS!
我可能做错了什么?如果需要更多信息或澄清,请告诉我。
I have two arrays, one of the arrays holds all the unique values of another array. The second array holds the unique values of the duplicate values found of another array.
The following code below loops through the first array and checks to see if a value in that array index matches a value in the second array index for a duped item. If it does it will add an arrow before the item's text.
This doesn't seem to be working correctly and it's printing the first array, rather then checking for a duped item.
var aos = ["a","a","b","c","d","e","f","f","f","g","g","g","h","h","h"];
// Credits to the guy who helped me with this, internet w00t!
var duplicates = _.select(aos, function(val) {
var True = _.after(2, function() {
return true;
});
return _.any(aos, function(elem) {
return elem === val ? True() : false;
});
});
aos.sort();
aos = _.unique(aos);
var dennis = _.unique(duplicates);
if ($.inArray(aos[i], dennis)) {
console.log("CONTAINS!", dennis, aos[i]);
$("tr[number='" + i + "']").find("td:first-child").prepend("<img src='images/arrow.jpg' style='padding-right: 5px;'/>");
} else
console.log("no!");
Output:
no!
CONTAINS! ["a", "f", "g", "h"] b
CONTAINS! ["a", "f", "g", "h"] c
CONTAINS! ["a", "f", "g", "h"] d
CONTAINS! ["a", "f", "g", "h"] e
CONTAINS! ["a", "f", "g", "h"] f
CONTAINS! ["a", "f", "g", "h"] g
CONTAINS! ["a", "f", "g", "h"] h
What the output should be:
CONTAINS!
no!
no!
no!
no!
CONTAINS!
CONTAINS!
CONTAINS!
What could I possibly be doing wrong? If more information, or clarification is needed please let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“$.inArray()”的返回值不是布尔值。这是一个索引。因此得到一个布尔值“它在数组中吗?”答案,你比较-1,当没有找到该项目时返回。
您也可以只编写
~$.inArray(aos[i], dennis)
来获取布尔值,但我不太愿意推荐这样做,因为有些成年人可能会对我大喊大叫。现在,也就是说,您发布的代码中缺少一些非常重要的东西:“i”是什么?
The return value from "$.inArray()" is not a boolean. It's an index. Thus to get a boolean "is it in the array?" answer, you compare to -1, which is returned when the item is not found.
You could also write just
~$.inArray(aos[i], dennis)
to get a boolean, but I'm hesitant to recommend that because some grown-up is likely to yell at me.Now, that said, there's something very important missing from the code you posted: what is "i"?