let person1 = {
name: "John Doe",
age: 30,
profession: "Engineer"
};
let person2 = {
name: "Jane Smith",
age: 25,
profession: "Doctor"
};
We have two different people with their information stored in objects, and now I will demonstrate 2 methods to combine both objects into an array.
Object.entries()
Object.keys()
The first method is demonstrated below:
let firstArray = Object.entries(person1);
Here, we assigned the details of person1 to firstArray
, and upon running this code, you will get output similar to this:
(3) [Array(2), Array(2), Array(2)]
The second method is shown here:
let secondArray = Object.keys(person2);
In this case, we assigned the details of person2 to secondArray
, and when executed, the output will be:
(3) ["name", "age", "profession"]