I have such an object.
let Filus = {
male: {
hat: [1],
jacket: [2],
pants: [3],
shoes: [4],
suit: [5]
}
};
I want to get this array from this object.
let Filus = [1,2,3,4,5];
How to do it?
Just use Object.values
and flat
- this works even if you don't know the key of the nested object:
let Filus = {
male: {
hat: [1],
jacket: [2],
pants: [3],
shoes: [4],
suit: [5]
}
};
const res = Object.values(Object.values(Filus)[0]).flat();
console.log(res);
ES5 syntax:
var Filus = {
male: {
hat: [1],
jacket: [2],
pants: [3],
shoes: [4],
suit: [5]
}
};
var res = Object.keys(Filus[Object.keys(Filus)[0]]).map(function(key) {
return Filus[Object.keys(Filus)[0]][key];
}).reduce(function(acc, curr) {
return acc.concat(curr);
});
console.log(res);
It's also easy if you have the key:
let Filus = {
male: {
hat: [1],
jacket: [2],
pants: [3],
shoes: [4],
suit: [5]
}
};
const res = Object.values(Filus.male).flat();
console.log(res);
ES5 syntax:
var Filus = {
male: {
hat: [1],
jacket: [2],
pants: [3],
shoes: [4],
suit: [5]
}
};
var res = Object.keys(Filus.male).map(function(key) {
return Filus.male[key];
}).reduce(function(acc, curr) {
return acc.concat(curr);
});
console.log(res);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加