Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to convert an object to a list in js

/*
if you want to create an array from all the properties names of an object you use
the Object.keys() method;
if you want to make an array out of the values you use the Object.values() method;
if you want to create an array from both keys and values use the
Object.entries() method;
*/

const client= {
  "name": "John",
  "id": 00019857,
  "itemsPurchased": [
  	"apple",
  	"banana",
  	"kiwi",
  ]
}

Examples:
// with the keys
console.log(Object.keys(client));
// output: ["name", "id", "itemsPurchased"]

// with the values
console.log(Object.values(client));
// output: ["John", 00019857, ["apple", "banana", "kiwi"]]

// with both keys and values
console.log(Object.entries(client));
// output: [("name", "john"), ("id", 00019857), ("itemsPurchased", ["apple", "banana"])]
 
PREVIOUS NEXT
Tagged: #convert #object #list #js
ADD COMMENT
Topic
Name
4+7 =