How can I extract the specific column from an object, I have a column array and I want these fields extracted from an object which will be constructed by map loop function w
Just iterate over the col
properties and .map
to a new array. You could also consider changing the person
variable name to people
(or something like that), because it's a collection of persons, not a singular person, reducing the chance of confusion:
const people = [{
firstName: "Nick",
lastName: "Anderson",
age: 35,
sex: "M"
},
{
firstName: "yopm",
lastName: "Geyun",
age: 36,
sex: "M"
}
]
const col = ['firstName', 'age']
console.log(
people.map(
person => col.map(prop => person[prop])
)
);