I have an array like this:
我有一个像这样的数组:
var json.result= [
{"id":"0","category":"Camera","name":"600D Kit", "condition":"OK"},
{"id":"1","category":"Camera","name":"600D Kit", "condition":"missing cap"},
{"id":"2","category":"Camera","name":"7D Kit", "condition":"OK"},
{"id":"3","category":"Camera","name":"7D Kit", "condition":"OK"}]
I am trying to do the following: 1/ Find all the "name" 2/ When found duplicated "name", print only the first object
我正在尝试执行以下操作:1 /查找所有“名称”2 /当找到重复的“名称”时,仅打印第一个对象
So that the result would be:
equipment id: 0
category: Camera
name: 600D Kit
因此结果将是:设备ID:0类别:摄像机名称:600D套件
equipment id: 2
category: Camera
name: 7D Kit
设备ID:2类:摄像机名称:7D套件
The following is my attempt:
以下是我的尝试:
json.result.map(function(equipment){
var equipmentNamesArray = equipment.name;
var arr = $.makeArray(equipmentNamesArray);
var uniqueNames = equipmentNamesArray.reduce(function(a,b){
if (a.indexOf(b) < 0) a.push(b);
}, equipmentNamesArray);
console.log("equipment ID: ", uniqueNames.id);
console.log("category: ", uniqueNames.category);
console.log("name: ", uniqueNames);
});
However, I have an error of TypeError: equipmentNamesArray.reduce is not a function
. I would appreciate very much if anyone could tell me which part of the logic have I done wrong please :)))
但是,我有一个TypeError错误:equipmentNamesArray.reduce不是一个函数。我非常感谢,如果有人能告诉我逻辑的哪一部分我做错了请:)))
Cheers, Karen
干杯,凯伦
1 个解决方案
#1
1
Here is the code you need:
这是您需要的代码:
的jsfiddle
var json = [], equipmentNamesArray = [];
json.result= [
{"id":"0","category":"Camera","name":"600D Kit", "condition":"OK"},
{"id":"1","category":"Camera","name":"600D Kit", "condition":"missing cap"},
{"id":"2","category":"Camera","name":"7D Kit", "condition":"OK"},
{"id":"3","category":"Camera","name":"7D Kit", "condition":"OK"}];
json.result.map(function(equipment) {
if ($.inArray(equipment.name, equipmentNamesArray) === -1) {
console.log(equipment);
equipmentNamesArray.push(equipment.name);
}
});
You have to create the equipmentNamesArray
array to contain the items checked before everything else, when you start iterate the main result
array you check if the equipment.name
is already in the equipmentNamesArray
array.
您必须创建equipmentNamesArray数组以包含在其他所有内容之前检查的项目,当您开始迭代主结果数组时,检查equipment.name是否已在equipmentNamesArray数组中。
If it's not in the array print it and push it to the array.
如果它不在阵列中打印它并将其推送到阵列。
Then the next one will be skipped because is contained in the equipmentNamesArray
array yet.
然后将跳过下一个,因为它还包含在equipmentNamesArray数组中。
Here is another version with a change to keep a new array with only the items you want: JSFiddle 2
这是另一个版本,只需更改一个新数组,只包含你想要的项目:JSFiddle 2
#1
1
Here is the code you need:
这是您需要的代码:
的jsfiddle
var json = [], equipmentNamesArray = [];
json.result= [
{"id":"0","category":"Camera","name":"600D Kit", "condition":"OK"},
{"id":"1","category":"Camera","name":"600D Kit", "condition":"missing cap"},
{"id":"2","category":"Camera","name":"7D Kit", "condition":"OK"},
{"id":"3","category":"Camera","name":"7D Kit", "condition":"OK"}];
json.result.map(function(equipment) {
if ($.inArray(equipment.name, equipmentNamesArray) === -1) {
console.log(equipment);
equipmentNamesArray.push(equipment.name);
}
});
You have to create the equipmentNamesArray
array to contain the items checked before everything else, when you start iterate the main result
array you check if the equipment.name
is already in the equipmentNamesArray
array.
您必须创建equipmentNamesArray数组以包含在其他所有内容之前检查的项目,当您开始迭代主结果数组时,检查equipment.name是否已在equipmentNamesArray数组中。
If it's not in the array print it and push it to the array.
如果它不在阵列中打印它并将其推送到阵列。
Then the next one will be skipped because is contained in the equipmentNamesArray
array yet.
然后将跳过下一个,因为它还包含在equipmentNamesArray数组中。
Here is another version with a change to keep a new array with only the items you want: JSFiddle 2
这是另一个版本,只需更改一个新数组,只包含你想要的项目:JSFiddle 2