如何从ejs中数组中的所有对象获取所有使用的值
How to get all used values from all objects in array in ejs
假设我有一个数组,其中包含“图像”对象。在“图像”中,我有另一个数组,称为“标签”。我有这个来排序图像。
但是,我不知道如何在不重复的情况下获取所有“标签”数组中的所有值。
下面是两个“图像”对象和我想要获取的数据的概述。
{
"image1": { //IMAGE OBJECT 1
"path": "/res/img/path",
"name": "Red Car",
"tags": ["Red", "Cars"] //I want both of these values.
},
"image2": { //IMAGE OBJECT 2
"path": "/res/img/otherpath",
"name": "Blue Car",
"tags": ["Blue", "Cars"] //I only want the 'Blue' tag, because 'Cars' would be duplicate.
},
"image3": { //IMAGE OBJECT 3
"path": "/res/img/otherotherpath",
"name": "Red Fridge",
"tags": ["Red", "Fridge"] //I only want the 'Fridge' tag, because 'Red' would be duplicate.
}
有人知道怎么做吗?
我不需要确切的代码,只需要概念。我想自己弄清楚代码:)
您可以结合使用 array.map()
和 array.reduce()
。
console.log(
array
.map(obj => obj.tags) // get all tags
.flat() // and convert it to one big list
.reduce((arr, val) => arr.includes(val) ? arr : arr.concat(val), []) // remove duplicates
);
试试这个,
var data = {
"image1": { //IMAGE OBJECT 1
"path": "/res/img/path",
"name": "Red Car",
"tags": ["Red", "Cars"] //I want both of these values.
},
"image2": { //IMAGE OBJECT 2
"path": "/res/img/otherpath",
"name": "Blue Car",
"tags": ["Blue", "Cars"] //I only want the 'Blue' tag, because 'Cars' would be duplicate.
},
"image3": { //IMAGE OBJECT 3
"path": "/res/img/otherotherpath",
"name": "Red Fridge",
"tags": ["Red", "Fridge"] //I only want the 'Fridge' tag, because 'Red' would be duplicate.
}
}
var filteredArray = new Set();
Object.keys(data).forEach((key) => {
data[key]['tags'].forEach((tag) => {
filteredArray.add(tag)
})
});
filteredArray = Array.from(filteredArray).sort();
console.log(filteredArray);
const colors = ['red', 'green', 'green', 'red', 'blue', 'red', 'blue']
function copyArrayWithNoDupes(array) {
return Array.from(new Set([...array])).sort();
}
console.log(copyArrayWithNoDupes(colors))
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set
希望这段代码对您有所帮助
var obj = {
"image1": { //IMAGE OBJECT 1
"path": "/res/img/path",
"name": "Red Car",
"tags": ["Red", "Cars"] //I want both of these values.
},
"image2": { //IMAGE OBJECT 2
"path": "/res/img/otherpath",
"name": "Blue Car",
"tags": ["Blue", "Cars"] //I only want the 'Blue' tag, because 'Cars' would be duplicate.
},
"image3": { //IMAGE OBJECT 3
"path": "/res/img/otherotherpath",
"name": "Red Fridge",
"tags": ["Red", "Fridge"] //I only want the 'Fridge' tag, because 'Red' would be duplicate.
}
}
Object.values(obj).flatMap(o=>o.tags).filter((elem, index, self)=>index === self.indexOf(elem))
使用Object.keys
var tags = {
"image1": { //IMAGE OBJECT 1
"path": "/res/img/path",
"name": "Red Car",
"tags": ["Red", "Cars"] //I want both of these values.
},
"image2": { //IMAGE OBJECT 2
"path": "/res/img/otherpath",
"name": "Blue Car",
"tags": ["Blue", "Cars"] //I only want the 'Blue' tag, because 'Cars' would be duplicate.
},
"image3": { //IMAGE OBJECT 3
"path": "/res/img/otherotherpath",
"name": "Red Fridge",
"tags": ["Red", "Fridge"] //I only want the 'Fridge' tag, because 'Red' would be duplicate.
}
}
var filteredTags = new Set();
Object.keys(tags).forEach((key) => {
tags[key]['tags'].forEach((tag) => {
filteredTags.add(tag)
})
});
filteredTags = Array.from(filteredTags).sort();
console.log(filteredTags);
假设我有一个数组,其中包含“图像”对象。在“图像”中,我有另一个数组,称为“标签”。我有这个来排序图像。 但是,我不知道如何在不重复的情况下获取所有“标签”数组中的所有值。
下面是两个“图像”对象和我想要获取的数据的概述。
{
"image1": { //IMAGE OBJECT 1
"path": "/res/img/path",
"name": "Red Car",
"tags": ["Red", "Cars"] //I want both of these values.
},
"image2": { //IMAGE OBJECT 2
"path": "/res/img/otherpath",
"name": "Blue Car",
"tags": ["Blue", "Cars"] //I only want the 'Blue' tag, because 'Cars' would be duplicate.
},
"image3": { //IMAGE OBJECT 3
"path": "/res/img/otherotherpath",
"name": "Red Fridge",
"tags": ["Red", "Fridge"] //I only want the 'Fridge' tag, because 'Red' would be duplicate.
}
有人知道怎么做吗? 我不需要确切的代码,只需要概念。我想自己弄清楚代码:)
您可以结合使用 array.map()
和 array.reduce()
。
console.log(
array
.map(obj => obj.tags) // get all tags
.flat() // and convert it to one big list
.reduce((arr, val) => arr.includes(val) ? arr : arr.concat(val), []) // remove duplicates
);
试试这个,
var data = {
"image1": { //IMAGE OBJECT 1
"path": "/res/img/path",
"name": "Red Car",
"tags": ["Red", "Cars"] //I want both of these values.
},
"image2": { //IMAGE OBJECT 2
"path": "/res/img/otherpath",
"name": "Blue Car",
"tags": ["Blue", "Cars"] //I only want the 'Blue' tag, because 'Cars' would be duplicate.
},
"image3": { //IMAGE OBJECT 3
"path": "/res/img/otherotherpath",
"name": "Red Fridge",
"tags": ["Red", "Fridge"] //I only want the 'Fridge' tag, because 'Red' would be duplicate.
}
}
var filteredArray = new Set();
Object.keys(data).forEach((key) => {
data[key]['tags'].forEach((tag) => {
filteredArray.add(tag)
})
});
filteredArray = Array.from(filteredArray).sort();
console.log(filteredArray);
const colors = ['red', 'green', 'green', 'red', 'blue', 'red', 'blue']
function copyArrayWithNoDupes(array) {
return Array.from(new Set([...array])).sort();
}
console.log(copyArrayWithNoDupes(colors))
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set
希望这段代码对您有所帮助
var obj = {
"image1": { //IMAGE OBJECT 1
"path": "/res/img/path",
"name": "Red Car",
"tags": ["Red", "Cars"] //I want both of these values.
},
"image2": { //IMAGE OBJECT 2
"path": "/res/img/otherpath",
"name": "Blue Car",
"tags": ["Blue", "Cars"] //I only want the 'Blue' tag, because 'Cars' would be duplicate.
},
"image3": { //IMAGE OBJECT 3
"path": "/res/img/otherotherpath",
"name": "Red Fridge",
"tags": ["Red", "Fridge"] //I only want the 'Fridge' tag, because 'Red' would be duplicate.
}
}
Object.values(obj).flatMap(o=>o.tags).filter((elem, index, self)=>index === self.indexOf(elem))
使用Object.keys
var tags = {
"image1": { //IMAGE OBJECT 1
"path": "/res/img/path",
"name": "Red Car",
"tags": ["Red", "Cars"] //I want both of these values.
},
"image2": { //IMAGE OBJECT 2
"path": "/res/img/otherpath",
"name": "Blue Car",
"tags": ["Blue", "Cars"] //I only want the 'Blue' tag, because 'Cars' would be duplicate.
},
"image3": { //IMAGE OBJECT 3
"path": "/res/img/otherotherpath",
"name": "Red Fridge",
"tags": ["Red", "Fridge"] //I only want the 'Fridge' tag, because 'Red' would be duplicate.
}
}
var filteredTags = new Set();
Object.keys(tags).forEach((key) => {
tags[key]['tags'].forEach((tag) => {
filteredTags.add(tag)
})
});
filteredTags = Array.from(filteredTags).sort();
console.log(filteredTags);