包含对象逻辑的 Javascript 数组失败。出了什么问题?

发布于 2024-11-14 00:57:17 字数 1299 浏览 3 评论 0原文

我有两个数组,其中一个数组保存另一个数组的所有唯一值。第二个数组保存在另一个数组中找到的重复值的唯一值。

下面的代码循环遍历第一个数组,并检查该数组索引中的值是否与第二个数组索引中的值匹配,以查找被欺骗的项目。如果是这样,它将在项目文本之前添加一个箭头。

这似乎工作不正常,它正在打印第一个数组,而不是检查重复的项目。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请帮我爱他 2024-11-21 00:57:17
if ($.inArray(aos[i], dennis)) {

“$.inArray()”的返回值不是布尔值。这是一个索引。因此得到一个布尔值“它在数组中吗?”答案,你比较-1,当没有找到该项目时返回。

if ($.inArray(aos[i], dennis) > -1) {

您也可以只编写 ~$.inArray(aos[i], dennis) 来获取布尔值,但我不太愿意推荐这样做,因为有些成年人可能会对我大喊大叫。

现在,也就是说,您发布的代码中缺少一些非常重要的东西:“i”是什么?

if ($.inArray(aos[i], dennis)) {

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.

if ($.inArray(aos[i], dennis) > -1) {

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"?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文