How can I loop over this array of objects and output the key of the object
var people = [
{ name: "jon", age: 42 },
{ name: "mary", age: 32 }
]
so the above would return:
// "name" "age", "name" "age"
Use Object.keys() to extract keys from an object.
var people = [
{ name: "jon", age: 42 },
{ name: "mary", age: 32 }
]
console.log(people.map(o => Object.keys(o).join(" ")).join(", "));
Another example to match the exact output:
var people = [{
name: "jon",
age: 42
}, {
name: "mary",
age: 32
}]
var result = people.map(o => Object.keys(o).map(string => `"${string}"`).join(' ')).join(', ');
console.log(result);
people.forEach( x => console.log(Object.keys(x)))