[英]How to get all used values from all objects in array in ejs
Let's say I have an array, which contains "image" objects.假设我有一个数组,其中包含“图像”对象。 Inside an "image" I have another array, called "tags".在“图像”中,我有另一个数组,称为“标签”。 I have this to sort images.我有这个来排序图像。 However, I do not know how to get all values in all "tags" arrays, without getting duplicates.但是,我不知道如何在没有重复的情况下获取所有“标签”数组中的所有值。
Here's an overview of two "image" objects and the data I want to get.这是两个“图像”对象的概述以及我想要获取的数据。
{
"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.
}
Does anyone know how to do this?有谁知道如何做到这一点? I do not need exact code, just the concept.我不需要确切的代码,只需要概念。 I wanna figure out the code myself :)我想自己弄清楚代码:)
You can make use of array.map()
in combination with array.reduce()
.您可以使用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
);
Try this,尝试这个,
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 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set
I hope this code helping you我希望这个代码对你有帮助
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))
Use Object.keys
使用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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.