如何使用下划线从对象数组中收集所有数组唯一值?
How to collect all array unique values from array of objects using underscore?
我有一组对象。喜欢
project=[
{
name : "A"
dept : ["a", "b", "c"]
},
{
name : "B"
dept : ["a", "e", "f", "g"]
},
{
name : "C"
dept : ["b", "c", "e", "j"]
},
]
现在我想使用下划线从这个对象数组中收集 dept 的所有唯一数组值。
输出应保存在一个新数组中。喜欢
var newArray = ["a", "b", "c", "e", "f", "g", "j"]
我是 underscore.js 的新人。提前致谢。
应该这样做:
_.uniq(_.flatten(project.map(x => x.dept)));
您可以使用 Set
和 reduce()
在纯 js 中执行此操作。
var project = [{
name: "A",
dept: ["a", "b", "c"]
}, {
name: "B",
dept: ["a", "e", "f", "g"]
}, {
name: "C",
dept: ["b", "c", "e", "j"]
}];
var result = [...new Set(project.reduce((r, e) => r.concat(e.dept), []))]
console.log(result)
ES2015版本:
const newArray = project.reduce((res, {dept}) =>
(dept.forEach(v => !res.some(r => r === v) && res.push(v)), res), []);
还有
const newArray = [...new Set(Array.prototype.concat.apply([], project.map(({dept}) => dept)))]
var project = [{
name: "A",
dept: ["a", "b", "c"]
}, {
name: "B",
dept: ["a", "e", "f", "g"]
}, {
name: "C",
dept: ["b", "c", "e", "j"]
}, ]
_UNIQUEAGEARRAY = _.flatten(project.map(function(item) {
return item.dept
}));
console.log(_.uniq(_UNIQUEAGEARRAY));
document.body.innerHTML = JSON.stringify(_UNIQUEAGEARRAY);
一个Fiddle:
这是一个使用下划线的解决方案 pluck, flatten and uniq:
var result = _.chain(project)
.pluck('dept')
.flatten()
.uniq()
.value();
var project=[
{
name : "A",
dept : ["a", "b", "c"]
},
{
name : "B",
dept : ["a", "e", "f", "g"]
},
{
name : "C",
dept : ["b", "c", "e", "j"]
},
]
var result = _.chain(project)
.pluck('dept')
.flatten()
.uniq()
.value();
document.getElementById('result').textContent = JSON.stringify(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<p>
<pre id="result"></pre>
</p>
试试这个。
var projects = [{
name: "A",
dept: ["a", "b", "c"]
}, {
name: "B",
dept: ["a", "e", "f", "g"]
}, {
name: "C",
dept: ["b", "c", "e", "j"]
}]
var depts = [];
projects.map((project) => {
depts = depts.concat(project.dept)
});
var result = depts.filter((item, pos) => depts.indexOf(item) == pos);
console.log(result);
你可以使用 spread syntax ...
and Set
var project = [{ name: "A", dept: ["a", "b", "c"] }, { name: "B", dept: ["a", "e", "f", "g"] }, { name: "C", dept: ["b", "c", "e", "j"] }];
var result = project.reduce((r, a) => [...new Set([...r, ...a.dept])], []);
console.log(result);
我有一组对象。喜欢
project=[
{
name : "A"
dept : ["a", "b", "c"]
},
{
name : "B"
dept : ["a", "e", "f", "g"]
},
{
name : "C"
dept : ["b", "c", "e", "j"]
},
]
现在我想使用下划线从这个对象数组中收集 dept 的所有唯一数组值。 输出应保存在一个新数组中。喜欢
var newArray = ["a", "b", "c", "e", "f", "g", "j"]
我是 underscore.js 的新人。提前致谢。
应该这样做:
_.uniq(_.flatten(project.map(x => x.dept)));
您可以使用 Set
和 reduce()
在纯 js 中执行此操作。
var project = [{
name: "A",
dept: ["a", "b", "c"]
}, {
name: "B",
dept: ["a", "e", "f", "g"]
}, {
name: "C",
dept: ["b", "c", "e", "j"]
}];
var result = [...new Set(project.reduce((r, e) => r.concat(e.dept), []))]
console.log(result)
ES2015版本:
const newArray = project.reduce((res, {dept}) =>
(dept.forEach(v => !res.some(r => r === v) && res.push(v)), res), []);
还有
const newArray = [...new Set(Array.prototype.concat.apply([], project.map(({dept}) => dept)))]
var project = [{
name: "A",
dept: ["a", "b", "c"]
}, {
name: "B",
dept: ["a", "e", "f", "g"]
}, {
name: "C",
dept: ["b", "c", "e", "j"]
}, ]
_UNIQUEAGEARRAY = _.flatten(project.map(function(item) {
return item.dept
}));
console.log(_.uniq(_UNIQUEAGEARRAY));
document.body.innerHTML = JSON.stringify(_UNIQUEAGEARRAY);
一个Fiddle:
这是一个使用下划线的解决方案 pluck, flatten and uniq:
var result = _.chain(project)
.pluck('dept')
.flatten()
.uniq()
.value();
var project=[
{
name : "A",
dept : ["a", "b", "c"]
},
{
name : "B",
dept : ["a", "e", "f", "g"]
},
{
name : "C",
dept : ["b", "c", "e", "j"]
},
]
var result = _.chain(project)
.pluck('dept')
.flatten()
.uniq()
.value();
document.getElementById('result').textContent = JSON.stringify(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<p>
<pre id="result"></pre>
</p>
试试这个。
var projects = [{
name: "A",
dept: ["a", "b", "c"]
}, {
name: "B",
dept: ["a", "e", "f", "g"]
}, {
name: "C",
dept: ["b", "c", "e", "j"]
}]
var depts = [];
projects.map((project) => {
depts = depts.concat(project.dept)
});
var result = depts.filter((item, pos) => depts.indexOf(item) == pos);
console.log(result);
你可以使用 spread syntax ...
and Set
var project = [{ name: "A", dept: ["a", "b", "c"] }, { name: "B", dept: ["a", "e", "f", "g"] }, { name: "C", dept: ["b", "c", "e", "j"] }];
var result = project.reduce((r, a) => [...new Set([...r, ...a.dept])], []);
console.log(result);