简体   繁体   English

如何使用push方法或类似方法在JavaScript中构造对象而不是数组对象

[英]How to construct an object in javascript not an array object using push method or similar

I'm trying to build a javascript object through the push method of an array. 我正在尝试通过数组的push方法构建一个javascript对象。 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' } } ]

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 codepen: https ://codepen.io/anon/pen/qoGoKp editors = 0012

  • In objects, properties always need a key ( $1 , $2 ,etc). 在对象中,属性始终需要一个键( $1$2等)。
    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. 不能使用person.2.1.name Therefor I prepended the numbers with $ , so it becomes person.$2.$1.name , which is possible. 因此,我在数字前加上$ ,因此它变成了person.$2.$1.name ,这是可能的。
    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. 为了能够将下一个属性“推”到现有对象的末尾,我使用Object.keys(person).length获取下一个可用的索引号。
    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. 请注意,在您的情况下,您也可以在for循环中使用整数i ,但Object.keys().length最类似于Array.push()方法。
    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. 另外,如果以后需要添加其他几个属性,则无需更改for循环即可更正索引号。 Now, the 3 in i<3; 现在, 3i<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. 代表要添加到对象中的属性的数量,因此,如果以后要添加另外5个属性,则for循环将像for (let i=0; i<5; i++) {一样开始for (let i=0; i<5; i++) {会保持不变。 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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM