我是初学者所以请......温柔。
以下是代码:
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"
},
];
答案 0 :(得分:0)
通常,您希望对数组进行排序,然后如果存在重复项,则它们将在数组中彼此相邻。
此问题之前已经得到了回答 Removing duplicates in array of objects
或者不是自己编写,而是可以利用现有的实现,例如lodash&#39; s https://lodash.com/docs#uniq
答案 1 :(得分:0)
如果我理解你在问题评论中的澄清,那么这应该是单独的字段值数组:
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);
说明:对象makers
,bodies
等用作哈希表。首次遇到"Bic"
制作者时,makers
会获得一个新属性:{ "Bic": true }
。在"inkwell"
,我们会插入另一个:{ "Bic": true, "inkwell": true }
。当我们再次看到"inkwell"
时,我们只需将现有属性重新分配给true
,因此对象不会更改。这个价值实际上根本不重要;我喜欢true
(基本上说“我看到了Bic!我也看到了墨水池!”),但它本身可能是17.5
,或者你可以一直保持运行(“我看到了两个Bics!我看到一个墨水瓶!“{ "Bic": 2, "inkwell": 1 }
)。这取决于对象中的所有键都是唯一的。最后,我们只是从每个对象中提取这些键作为数组:["Bic", "inkwell", ...]
。