我想遍历一个对象数组,我不想要任何重复项
I want to loop through an array of objects and I don't want any duplicates
我是初学者,所以请..要温柔。
这是代码:
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);
说明:对象makers
、bodies
等用作哈希表。当第一次遇到"Bic"
制作者时,makers
将获得一个新属性:{ "Bic": true }
。在"inkwell"
,我们插入另一个:{ "Bic": true, "inkwell": true }
。当我们再次看到"inkwell"
时,我们只是将现有属性重新分配给 true
,因此对象保持不变。价值实际上根本不重要;我喜欢true
(基本上是说"我看到了比克!我也看到了墨水瓶!"(,但它可能是17.5
所有重要的,或者你可以继续运行总计("我看到了两个 Bics!我看到了一个墨水瓶! { "Bic": 2, "inkwell": 1 }
(。这取决于对象中的所有键都是唯一的这一事实。最后,我们只是从每个对象中提取这些键作为数组:["Bic", "inkwell", ...]
.
相关文章:
- 使自动完成可搜索下拉菜单don'我不想在点击搜索时显示列表
- RequireJS加载我没有的资源'我不想
- 我想遍历一个对象数组,我不想要任何重复项
- 如何在javascript中调用onclick事件?我没有'我不想在我的HTML页面中使用任何事件
- IE11没有'我不想把重点放在“disabled”元素上
- 可以'我不想用BlackBerry Webworks 2来展示Toast
- 将参数传递给函数I don'我不想立即执行
- Jquery点击事件don'我不想开火
- 我需要创建一个事件侦听器.但是我不想使用任何框架
- 我可以'Don’我不想让我的程序继续下去;m使用document.write,因为它在html中使用JavaSc
- 页面卸载时,出现我不想要的确认框
- 不想返回整个对象,如何过滤掉我不想要的东西
- 当我添加警报(“现在它可以工作”);这个函数有效,但我不想要这个
- 我不想支持在 asp.net mvc web/intranet应用程序中关闭javascript
- 我想创建一个从 50 到 1 的倒计时,但我不想在每个数字之间倒计时相同的时间量
- 尽管设置了标头,但我不想使用 JSONP,但 cors 无法解析
- 表单值,我不想重置它们
- 谷歌地图v3-地图没有'我不想从mysqldb加载标记
- 悬停函数没有'I don’我不想工作
- 等待 ajax 返回的正确方法(我不想使用成功处理程序