Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I have an array that looks like this:
const myArray = [ { 'John': 50 }, { 'Adam': 29 }, { 'Jack': 40 } ]
How do I convert the array into an object that looks like this?
const convertedObject = { 'John': 50, 'Adam': 29, 'Jack': 40 }
Object.assign
You can spread the array into Object.assign():
Object.assign()
const myArray = [ { 'John': 50 }, { 'Adam': 29 }, { 'Jack': 40 } ] const convertedObject = Object.assign({}, ...myArray) console.log(convertedObject)
Add a comment
You can use Array.reduce() for that:
Array.reduce()
const myArray = [ {'John':50}, {'Adam':29}, {'Jack':40} ] var res = myArray.reduce((acc, item) => { var key = Object.keys(item)[0]; acc[key] = item[key]; return acc; }, {}); console.log(res);
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
Object.assignon it several times.