【问题标题】:I want to loop through an array of objects and I don't want any duplicates我想遍历一个对象数组,我不想要任何重复
【发布时间】:2015-03-23 02:04:05
【问题描述】:

我是初学者,所以请......温柔点。

代码如下:

for (i = 0; i<importedData.data.length; i++){
    var b=importedData.data[i].maker;
    console.log(b);
    // console.log(importedData.data[i].maker);

        var bt = importedData.data[i].body;
    console.log(bt);
        var ic = importedData.data[i].inkcolor;
    console.log(ic);
        var pt = importedData.data[i].type;
    console.log(pt);

};

我得到了他的结果,但由于有 25 个不同的对象具有相同的字段,因此存在重复。

以下是 JSON 供您查看

exports.data = [
    {
        "type": "ballpoint",
        "maker": "Bic",
        "inkcolor": "blue",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image01.jpg"
    },

    {
        "type": "fountain",
        "maker": "inkwell",
        "inkcolor": "blue",
        "body": "metal",
        "length": "6 inches",
        "img" : "image02.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "Bic",
        "inkcolor": "blue",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image03.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "inkwell",
        "inkcolor": "blue",
        "body": "ceramic",
        "length": "5 inches",
        "img" : "image04.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "luxor",
        "inkcolor": "blue",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image05.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "Bic",
        "inkcolor": "blue",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image06.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "Bic",
        "inkcolor": "blue",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image07.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "montblanc",
        "inkcolor" : "blue",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image08.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "luxor",
        "inkcolor": "blue",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image09.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "Bic",
        "inkcolor": "blue",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image10.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "inkwell",
        "inkcolor": "blue",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image11.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "papermate",
        "inkcolor": "blue",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image12.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "montblanc",
        "inkcolor": "blue",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image13.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "Bic",
        "inkcolor": "blue",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image14.jpg"
    },
    {
        "type": "fountain",
        "maker": "montblanc",
        "inkcolor": "black",
        "body": "gold",
        "length": "6 inches",
        "img" : "image15.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "Bic",
        "inkcolor": "yellow",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image16.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "sharpie",
        "inkcolor": "blue",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image17.jpg"
    },
    {
        "type": "fountain",
        "maker": "papermate",
        "inkcolor": "black",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image18.jpg"
    },
    {
        "type": "felt",
        "maker": "Bic",
        "inkcolor": "blue",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image19.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "technicalpen",
        "inkcolor": "blue",
        "body": "plastic",
        "length": "4 inches",
        "img" : "image20.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "Bic",
        "inkcolor": "red",
        "body": "metal",
        "length": "5 inches",
        "img" : "image21.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "technicalpen",
        "inkcolor": "blue",
        "body": "plastic",
        "length": "5 inches",
        "img" : "image22.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "Bic",
        "inkcolor": "green",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image23.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "parker",
        "inkcolor": "black",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image24.jpg"
    },
    {
        "type": "ballpoint",
        "maker": "uniball",
        "inkcolor": "red",
        "body": "plastic",
        "length": "6 inches",
        "img" : "image25.jpg"
    },
];

【问题讨论】:

  • 您显示的示例数据有 no 个重复项。也就是说,数组中的每一项都至少有一个与其他项不同的字段。您是说要从数组中删除 项,如果它们是“重复的”,则与其他项具有相同的typeinkcolorbody(因为这些是您在循环中使用的三个字段)?还有,这和钛有什么关系?
  • 不太确定你想要什么。你能给出想要的输出吗?是否要跳过包含任何重复字段的整条记录?所有重复的字段?只跳过我们刚刚看到的字段?...
  • 重复是什么意思?每个 on 都有不同的 img 字段,但其他字段可能相同。更具体地说明什么构成重复。
  • 为任何不一致或遗漏向所有人道歉,因为我是一个新人。背景是钛的一个移动应用项目。
  • @nnnnnn 你是说你想从数组中删除项目,如果它们是“重复的”,因为它们与其他项目具有相同的类型、墨水颜色和正文(因为这三个字段是你在你的循环中使用)?是的,这就是我想要的。

标签: javascript titanium


【解决方案1】:

通常您希望对数组进行排序,然后如果存在重复项,它们将在数组中彼此相邻。

这个问题之前已经回答过了 Removing duplicates in array of objects

或者,您可以利用现有的实现,例如 lodash 的 https://lodash.com/docs#uniq,而不是自己编写

【讨论】:

    【解决方案2】:

    如果我理解您对问题 cmets 的澄清,这应该使字段值的单独数组:

    var makers = {},
        bodies = {},
        inkcolors = {},
        types = {};
    
    for (var i = 0; i < importedData.data.length; i++) {
        var item = importedData.data[i];
        makers[item.maker] = true;
        bodies[item.body] = true;
        inkcolors[item.inkcolor] = true;
        types[item.type] = true;
    };
    makers = Object.keys(makers);
    bodies = Object.keys(bodies);
    inkcolors = Object.keys(inkcolors);
    types = Object.keys(types);
    
    console.log(makers);
    console.log(bodies);
    console.log(inkcolors);
    console.log(types);
    

    说明:makersbodies 等对象用作哈希表。当第一次遇到"Bic" maker 时,makers 将获得一个新属性:{ "Bic": true }。在"inkwell" 上,我们插入另一个:{ "Bic": true, "inkwell": true }。当我们再次看到"inkwell" 时,我们只是将现有属性重新分配给true,所以对象没有改变。价值实际上根本不重要;我喜欢true(基本上是说“我看到了Bic!我也看到了墨水瓶!”),但它可能是17.5,或者你可以继续运行(“我看到了两个Bic!我看到一个墨水池!”{ "Bic": 2, "inkwell": 1 })。这取决于对象中的所有键都是唯一的这一事实。最后,我们只是从每个对象中提取这些键作为一个数组:["Bic", "inkwell", ...]

    【讨论】:

    • 谢谢。它给了我一个错误。说“找不到变量:制造商”;
    • 啊,对了,我忘记初始化了。我马上贴上去。
    • 你这个人。它向我展示了我在控制台中想要的东西。老实说..虽然如果你能分解你做了什么,我会很感激......也许特别是 object.keys 部分。再次感谢
    猜你喜欢
    • 1970-01-01
    • 2013-02-02
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多