简体   繁体   English

如何从ejs数组中的所有对象获取所有使用的值

[英]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.

相关问题 从对象数组中获取所有真实值 - Get all truthy values from an array of objects Javascript 如何从对象数组中获取属性的所有值? - Javascript How to get all values for a property from an Array of Objects? 如何获取对象数组中对象的所有属性值的数组? - How to get an array of the values of all properties of objects inside an array of objects? 如何从具有嵌套 arrays 对象的对象数组中获取所有特定值? - How to get all specific values from an array of objects with nested arrays of objects? 如何从检查所有对象值的数组中删除相同的对象? - How to remove identical objects from an array checking all of the objects values? 如何从对象数组中获取属性本身的所有唯一值,该属性本身就是数组 - How to get from an array of objects all unique values of a property that is an array itself 如何从数组获取所有退出值 - How to get all Exit values from Array 如何实际从该数组中获取所有值? - How to actually get all values from this array? 如何一次从具有嵌套对象数组的多个文档中获取与来自 mongodoDb 的查询匹配的所有值 - How get to All values matching the query from mongodoDb at Once from multiple documents having nested array of objects 如何获取对象数组中特定键的所有值? - How to get all values of a specific key in array of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM