How to convert an object containing objects into array of objects in Node.js

3 Answers

0 votes
const objOfObjs = {
   "a": {id: 1, language: 'javascript'},
   "b": {id: 2, language: 'node.js'},
   "c": {id: 3, language: 'typescript'},
   "d": {id: 4, language: 'python'},
};
 
 
const arr = Object.values(objOfObjs);

console.log(arr); 
   
   
 
 
/*
run:
   
[
  { id: 1, language: 'javascript' },
  { id: 2, language: 'node.js' },
  { id: 3, language: 'typescript' },
  { id: 4, language: 'python' }
]

*/

 



answered May 21, 2022 by avibootz
0 votes
const objOfObjs = {
   "a": {id: 1, language: 'javascript'},
   "b": {id: 2, language: 'node.js'},
   "c": {id: 3, language: 'typescript'},
   "d": {id: 4, language: 'python'},
};
 
 
const arr = Object.keys(objOfObjs).map(key => objOfObjs[key]);

console.log(arr); 
   
   
 
 
/*
run:
   
[
  { id: 1, language: 'javascript' },
  { id: 2, language: 'node.js' },
  { id: 3, language: 'typescript' },
  { id: 4, language: 'python' }
]

*/

 



answered May 21, 2022 by avibootz
0 votes
const objOfObjs = {
   "a": {id: 1, language: 'javascript'},
   "b": {id: 2, language: 'node.js'},
   "c": {id: 3, language: 'typescript'},
   "d": {id: 4, language: 'python'},
};
 
 
const arr = Object.entries(objOfObjs).map((entry) => ( { [entry[0]]: entry[1] } ));

console.log(arr); 
   
   
 
 
/*
run:
   
[
  { a: { id: 1, language: 'javascript' } },
  { b: { id: 2, language: 'node.js' } },
  { c: { id: 3, language: 'typescript' } },
  { d: { id: 4, language: 'python' } }
]

*/

 



answered May 21, 2022 by avibootz

Related questions

...