我找不到类似的问题而且我有点卡住了。我有以下JSON数组:
[
{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1","3" ]
},
{
"Name": "element3",
"Attributes": []
}
]
我正在尝试在“Attributes”属性中创建一个包含所有唯一元素的数组,但是我无法遍历每个对象,然后循环遍历数组元素以返回唯一值。我试图用filter()或map()来做这件事。
编辑:我想要一系列独特的元素,所以:[1,2,3]。
答案 0 :(得分:1)
您可以使用 Array#reduce
和 Array#filter
方法
var data = [{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1", "3"]
}, {
"Name": "element3",
"Attributes": []
}
]
console.log(
// iterate over array elements
data.reduce(function(arr, ele) {
// push the unique values to array
[].push.apply(arr,
// filter out unique value
ele.Attributes.filter(function(v) {
// check element present in array
return arr.indexOf(v) == -1;
})
);
// return the unique array
return arr;
// set initial argument as an empty array
}, [])
);
var data = [{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1", "3"]
}, {
"Name": "element3",
"Attributes": []
}
]
console.log(
data.reduce((arr, ele) => ([].push.apply(arr, ele.Attributes.filter((v) => arr.indexOf(v) == -1)), arr), [])
);
答案 1 :(得分:1)
你可以用几种Array方法来做。例如:
var result = [
{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1","3" ]
},
{
"Name": "element3",
"Attributes": []
}
]
// map to [ ["1", "2"], ["1", "3"], [] ]
.map(item => item.Attributes)
// flatten to [ "1", "2", "1", "3" ]
.reduce((prev, curr) => prev.concat(curr), [])
// filter unique [ "1", "2", "3" ]
.filter((item, i, arr) => arr.indexOf(item) === i)
console.log(result)
答案 2 :(得分:0)
var uniqueArr = [];
var arr = [
{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1","3" ]
},
{
"Name": "element3",
"Attributes": []
}
];
arr.forEach(function(obj) {
var attr = obj.Attributes;
attr.forEach(function(val){
if (uniqueArray.indexOf(val) < 0) {
uniqueArray.push(val)
}
});
})
答案 3 :(得分:0)
如果lodash是一个选项,您可以轻松获得所需内容:
> _.chain(foo).map('Attributes').flatten().uniq().value()
["1", "2", "3"]
答案 4 :(得分:0)
您有答案可供选择。只是为了好玩:这个使用es6
"use strict";
let uniqueAttr = [];
const obj = [
{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1","3" ]
},
{
"Name": "element3",
"Attributes": []
}
];
obj.forEach( element =>
element.Attributes.forEach(
attr => uniqueAttr.indexOf(attr) < 0 && uniqueAttr.push(attr)
)
);
document.querySelector("#result").textContent = uniqueAttr;
<pre id="result"></pre>
答案 5 :(得分:0)
试试这个,它有助于解决这个问题。
var data = [{
"Name": "element1",
"Attributes": ["1", "2"]
}, {
"Name": "element2",
"Attributes": ["1", "3"]
}, {
"Name": "element3",
"Attributes": []
}];
var Attributes = [];
$.each(data, function(i, e) {
$.each(e.Attributes, function(i, e) {
Attributes.push(parseInt(e));
});
});
Attributes = $.unique(Attributes);
alert(Attributes);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
&#13;
答案 6 :(得分:0)
在ES6 / ES2015中,您可以使用Set和spread operator:
const input = [
{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1","3" ]
},
{
"Name": "element3",
"Attributes": []
}
];
const output = [...new Set([].concat(...input.map(item => item.Attributes)))];
console.log(output);
说明(由内而外):
input.map(item => item.Attributes)
产生一个由Attributes数组组成的数组[].concat(...)
使数组变平,即产生一个包含所有Attributes值(包括重复项)的数组new Set()
从数组中生成一个Set,即仅存储唯一的Attribute值[...]
根据集合的值生成一个数组,即生成所有唯一属性值的数组答案 7 :(得分:0)
以@dfsq答案为基础,您可以用单个map
替换两个reduce
和flatMap
var result = [
{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1","3" ]
},
{
"Name": "element3",
"Attributes": []
}
]
// map & flatten to [ "1", "2", "1", "3" ]
.flatMap(item => item.Attributes)
// filter unique [ "1", "2", "3" ]
.filter((item, i, arr) => arr.indexOf(item) === i)
console.log(result)