I have an object called ‘times’, that holds another object called ‘20102’, that holds a list of 3 objects. It looks like this:
times: { 20102: [ { name:'jane', age:12 }, { name:'josh', age:19 }, { name:'jill', age:14 }, ] }
However, what I want it to look like is this:
times:[ { name:'jane', age:12 }, { name:'josh', age:19 }, { name:'jill', age:14}, ]
I was thinking of doing a double for loop but that’s not efficient. What’s a better way?
Advertisement
Answer
using Object.values() and flat()
var x = { times: { 20102: [{ 'key': '1' }, { 'key': '2' }, { 'key': '3' }, ] } }; x.times = Object.values(x.times).flat(); console.log(x);
If you know there will only be one key
var x = { times: { 20102: [{ 'key': '1' }, { 'key': '2' }, { 'key': '3' }, ] } }; x.times = Object.values(x.times)[0]; console.log(x);