用另一个子数组的值填充数组
Populate array with values from another subarray
假设我有一个 json 这样的结果:
{
"results": [{
"id": 1, "name": "test1"
},
{
"id": 2, "name": "test2",
"tags": [{"name": "car"},{ "name": "love"}]
},
{
"id": 3, "name": "test3",
"tags": [{"name": "car"},{"name": "bike"},{"name": "motorcycle"}]
}
]
}
我想装载一个只有唯一标签的新数组,在这个例子中是汽车、爱情、自行车、摩托车。
有时标签为 null 或为空...
有什么方法可以不用循环来做到这一点吗? jquery 或下划线适合吗?可以用纯 javascript?
来完成
干杯
const tagSet = new Set ( // use ES6 set to get rid of duplicates
obj.results
.filter(r => r.hasOwnProperty('tags')) // check for tags property
.map(r => r.tags) // extract array of array of tag obj
.reduce((a, b) => a.concat(b), []) // flatten into array of obj
.map(flat => flat.name) // extract name property
);
const uniques = [...tagSet]; // convert back into array
console.log(uniques); // [ 'car', 'love', 'bike', 'motorcycle' ]
既然你问了underscore
,我就给你一个lodash
的解决方案(因为lodash已经有效的替代了下划线)。您可以使用 lodash
使用带有内置方法的函数式方法来完成此操作,例如:
_.flatMap() -- 通过 属性 将对象内部的值抓取到单个数组中,并展平任何嵌套数组
_.compact() -- 删除任何 false
、null
、0
、""
、undefined
, 和 NaN
_.map() --同上,不压平(这里不需要)
_.uniq() -- return 只有唯一值
var data = {
"results": [{
"id": 1, "name": "test1"
},
{
"id": 2, "name": "test2",
"tags": [{"name": "car"},{ "name": "love"}]
},
{
"id": 3, "name": "test3",
"tags": [{"name": "car"},{"name": "bike"},{"name": "motorcycle"}]
}
]
};
var uniq = _.uniq(_.map(_.compact(_.flatMap(data.results, "tags")), "name"));
console.log(uniq);
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
或者,您可以使用 _.chain()
链接语句,而不是嵌套它们(为了提高可读性),如下所示:
var uniq = _
.chain(data.results)
.flatMap("tags")
.compact()
.map("name")
.uniq()
.value(); // [ 'car', 'love', 'bike', 'motorcycle' ]
假设我有一个 json 这样的结果:
{
"results": [{
"id": 1, "name": "test1"
},
{
"id": 2, "name": "test2",
"tags": [{"name": "car"},{ "name": "love"}]
},
{
"id": 3, "name": "test3",
"tags": [{"name": "car"},{"name": "bike"},{"name": "motorcycle"}]
}
]
}
我想装载一个只有唯一标签的新数组,在这个例子中是汽车、爱情、自行车、摩托车。
有时标签为 null 或为空...
有什么方法可以不用循环来做到这一点吗? jquery 或下划线适合吗?可以用纯 javascript?
来完成干杯
const tagSet = new Set ( // use ES6 set to get rid of duplicates
obj.results
.filter(r => r.hasOwnProperty('tags')) // check for tags property
.map(r => r.tags) // extract array of array of tag obj
.reduce((a, b) => a.concat(b), []) // flatten into array of obj
.map(flat => flat.name) // extract name property
);
const uniques = [...tagSet]; // convert back into array
console.log(uniques); // [ 'car', 'love', 'bike', 'motorcycle' ]
既然你问了underscore
,我就给你一个lodash
的解决方案(因为lodash已经有效的替代了下划线)。您可以使用 lodash
使用带有内置方法的函数式方法来完成此操作,例如:
_.flatMap() -- 通过 属性 将对象内部的值抓取到单个数组中,并展平任何嵌套数组
_.compact() -- 删除任何
false
、null
、0
、""
、undefined
, 和NaN
_.map() --同上,不压平(这里不需要)
_.uniq() -- return 只有唯一值
var data = {
"results": [{
"id": 1, "name": "test1"
},
{
"id": 2, "name": "test2",
"tags": [{"name": "car"},{ "name": "love"}]
},
{
"id": 3, "name": "test3",
"tags": [{"name": "car"},{"name": "bike"},{"name": "motorcycle"}]
}
]
};
var uniq = _.uniq(_.map(_.compact(_.flatMap(data.results, "tags")), "name"));
console.log(uniq);
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
或者,您可以使用 _.chain()
链接语句,而不是嵌套它们(为了提高可读性),如下所示:
var uniq = _
.chain(data.results)
.flatMap("tags")
.compact()
.map("name")
.uniq()
.value(); // [ 'car', 'love', 'bike', 'motorcycle' ]