香港专业教育学院有一系列的对象。每个对象内都有另一个数组。我想从这些数组中提取不同的值。
var data = [
{categorie: 'Stuff', type: 'One', designs: ['A', 'B']},
{categorie: 'Stuff', type: 'Two', designs: ['C']},
{categorie: 'Stuff', type: 'One', designs: ['D']},
{categorie: 'Other', type: 'Three', designs: ['C', 'D']}
];
console.log([...new Set(data.map(x => x.categorie))]);
console.log([...new Set(data.map(x => x.type))]);
//expected output for designs ['A','B','C','D']Run Code Online (Sandbox Code Playgroud)
您可以使用flatMap()代替map()
var data = [
{categorie: 'Stuff', type: 'One', designs: ['A', 'B']},
{categorie: 'Stuff', type: 'Two', designs: ['C']},
{categorie: 'Stuff', type: 'One', designs: ['D']},
{categorie: 'Other', type: 'Three', designs: ['C', 'D']}
];
console.log([...new Set(data.flatMap(x => x.designs))]);
//expected output for designs ['A','B','C','D']Run Code Online (Sandbox Code Playgroud)
如果您的浏览器不支持,flatMap()则可以concat()与传播运算符一起使用。
var data = [
{categorie: 'Stuff', type: 'One', designs: ['A', 'B']},
{categorie: 'Stuff', type: 'Two', designs: ['C']},
{categorie: 'Stuff', type: 'One', designs: ['D']},
{categorie: 'Other', type: 'Three', designs: ['C', 'D']}
];
console.log([...new Set([].concat(...data.map(x => x.designs)))]);
//expected output for designs ['A','B','C','D']Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
59 次 |
| 最近记录: |