I have an object that looks like this:
descriptions: {
1: {
description: "abc"
},
2: {
description: "def"
},
3: {
description: "ghi"
}
}
Is there an easy way to transform that object into an array like this?:
descriptions: ["abc","def","ghi"]
You could flatMap the values of the object.
var descriptions = { 1: { description: "abc" }, 2: { description: "def" }, 3: { description: "ghi" } }, values = Object.values(descriptions).flatMap(Object.values); console.log(values);
const obj = { descriptions: { 1: { description: "abc" }, 2: { description: "def" }, 3: { description: "ghi" } } }; console.log(Object.values(obj.descriptions).map(({description}) => description))
descriptions= {
1: {
description: "abc"
},
2: {
description: "def"
},
3: {
description: "ghi"
}
}
var keys = Object.keys(descriptions);
var values = keys.map((key) => descriptions[key].description);
values
will be the array you are looking for.
or something like this
function f(obj) {
let array = []
for (const prop in obj) {
array.push(Object.values(obj[prop])[0])
}
return array
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.