I'm trying to build a javascript object through the push method of an array. There are some loops inside to fill the values, but the result is a matrix. I know it's an object too, but I like to use dot notation to use the resulting object. How to do this and achieve an object, not an array
let person = [{
age: 0,
name: "",
second_name: "",
objeto2: {
name2: "",
second_name2: ""
}
}]
for (let i = 1; i <= 3; i++) {
person.push({ age: i, name: i + 1, second_name: "segundo", objeto2: { name2: "xxsxs", second_name2: "adfa" } })
};
the results:
[ { age: 0,
name: '',
second_name: '',
objeto2: { name2: '', second_name2: '' } },
{ age: 1,
name: 2,
second_name: 'segundo',
objeto2: { name2: 'xxsxs', second_name2: 'adfa' } },
{ age: 2,
name: 3,
second_name: 'segundo',
objeto2: { name2: 'xxsxs', second_name2: 'adfa' } },
{ age: 3,
name: 4,
second_name: 'segundo',
objeto2: { name2: 'xxsxs', second_name2: 'adfa' } } ]