var objs = [
{labels: ["label1", "label2"]},
{labels: ["label1", "label3"]},
{labels: ["label2", "label4"]}
]
我正在尝试提取此[“label1”,“label2”,“label3”,“label4”]
labels = this.get('objs').map(function(obj) {
return obj.labels.map(function(label) {
return label;
});
});
console.log(labels);
但是上面的代码打印了 [["label1", "label2"], ["label1", "label3"], ["label2", "label4"]]
有人可以帮我解决这个问题吗?
最佳答案
首先,您可以映射 objs
数组以仅获取 labels
数组,然后使用 Set
删除重复条目。
const objs = [
{labels: ["label1", "label2"]},
{labels: ["label1", "label3"]},
{labels: ["label2", "label4"]},
];
const r = [...new Set([].concat(...objs.map(({ labels }) => labels)))];
console.log(r);
ES5:
const objs = [
{labels: ["label1", "label2"]},
{labels: ["label1", "label3"]},
{labels: ["label2", "label4"]},
];
const r = [...new Set([].concat(...objs.map(function(v) {
return v.labels;
})))];
console.log(r);
关于javascript - 提取内部数组和对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47190281/