0

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' } } ]
kamome
  • 828
  • 3
  • 11
  • 27

1 Answers1

2

I think this is what you're looking for:

let person = {
  "$0": {age:0, name:"", alias:"", "$1":{name:"", alias:""}}
};
for (let i=0; i<3; i++) {
  let j = Object.keys(person).length;
  person["$"+j] = {age:j, name:j+1, alias:"segundo", "$1":{name:"xxsxs", alias:"adfa"}};
}

console.log(person);
console.log(person.$2.$1.name);
console.log(person.$3.alias);
console.log(person.$3.$1.alias);
codepen: https://codepen.io/anon/pen/qoGoKp?editors=0012
  • In objects, properties always need a key ($1,$2,etc).
    And those keys always have to be a string, numbers will be automatically typecasted into strings: see this link (under "Property names").
  • For the above reason, and specific to dot-notation itself, you cannot use only a number to access the object's properties when using dot-notation. Using person.2.1.name is not possible. Therefor I prepended the numbers with $, so it becomes person.$2.$1.name, which is possible.
    You can change $ to any other valid character, like _, but I found this the most readable.
  • In order to be able to 'push' the next property to the end of the existing object, I use Object.keys(person).length to get the next available index number.
    Note that in your case you could also use the integer i from the for-loop, but Object.keys().length most closely resembles the Array.push() method.
    Also, if at a later point you would need to add another couple of properties, you wouldn't have to alter your for-loop to correct for the index numbers. Now, the 3 in i<3; stands for the number of properties you want to add to the object, so if later on you wanted to add another 5 properties, your for-loop would start like for (let i=0; i<5; i++) { and everything else would stay the same. You could even make a function for it to make it more flexible:

    function addPersons(n) {
      for (let i=0; i<n; i++) {
        let j = Object.keys(person).length;
        person["$"+j] = {age:j, name:j+1, alias:"segundo", "$1":{name:"xxsxs", alias:"adfa"}};
      }
    }
    addPersons(3);
    addPersons(5);
    
myfunkyside
  • 3,890
  • 1
  • 17
  • 32