Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to convert object to array in javascript

// how to convert object to array in javascript
// using Object.entries()
const credits = { producer: 'John', director: 'Jane', assistant: 'Peter' };
const arr = Object.entries(credits);
console.log(arr);

/** Output:
[ [ 'producer', 'John' ],
  [ 'director', 'Jane' ],
  [ 'assistant', 'Peter' ]
]
**/

// if you want to perfrom reverse means array to object.
console.log(Object.fromEntries(arr)); // { producer: 'John', director: 'Jane', assistant: 'Peter' }

// convert object values to array
// using Object.values()
console.log(Object.values(credits)); // [ 'John', 'Jane', 'Peter' ]


// convert object keys to array
// using Object.keys()
console.log(Object.keys(credits)); // [ 'producer', 'director', 'assistant' ]
 
PREVIOUS NEXT
Tagged: #convert #object #array #javascript
ADD COMMENT
Topic
Name
9+2 =