我想遍历一个对象数组,我不想要任何重复项

I want to loop through an array of objects and I don't want any duplicates

本文关键字:任何重 我不想 遍历 一个对象 数组      更新时间:2023-09-26

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

这是代码:

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"
    },
];
通常,

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

这个问题之前已经回答过了删除对象数组中的重复项

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

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

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"制作者时,makers将获得一个新属性:{ "Bic": true } 。在"inkwell",我们插入另一个:{ "Bic": true, "inkwell": true } 。当我们再次看到"inkwell"时,我们只是将现有属性重新分配给 true ,因此对象保持不变。价值实际上根本不重要;我喜欢true(基本上是说"我看到了比克!我也看到了墨水瓶!"(,但它可能是17.5所有重要的,或者你可以继续运行总计("我看到了两个 Bics!我看到了一个墨水瓶! { "Bic": 2, "inkwell": 1 }(。这取决于对象中的所有键都是唯一的这一事实。最后,我们只是从每个对象中提取这些键作为数组:["Bic", "inkwell", ...] .