简体   繁体   English

将嵌套对象的每个对象的索引设置为属性

[英]Set the Index of each object, of an nested object into an property

Good morning ! 早上好 !

Does anyone know how i can insert the index of each object of a nested object into an property ? 有谁知道如何将嵌套对象的每个对象的索引插入属性?

Something like this: 像这样的东西:

const myObj = 
{
  "@type": "someType",
  A: [
    {
      "@type": "someType0",
      order: "DESC",
      myIndex: 0
    },
    {
      "@type": "someType1",
      order: "DESC",
      myIndex: 1
    },
    {
      "@type": "someType2",
      order: "DESC",
      myIndex: 2
    }
  ],
  B: [],
};

You can use the .map function to alter the objects within array. 您可以使用.map函数来更改数组中的对象。

 const myObj = { "@type": "someType", A: [ { "@type": "someType0", order: "DESC" }, { "@type": "someType1", order: "DESC" }, { "@type": "someType2", order: "DESC" } ], B: [], }; const newA = myObj.A.map((item,index) => { item.myIndex = index; return item; }) console.log(newA); 

CodePen link: https://codepen.io/bergur/pen/rrdyKX?editors=0010 CodePen链接: https ://codepen.io/bergur/pen/rrdyKX edit = 0010

If you wanna loop through all the properties of myObj you can use Object.keys(myObj) and then go through them and alter them one by one. 如果你想循环遍历myObj的所有属性,你可以使用Object.keys(myObj) ,然后浏览它们并逐个改变它们。

You can do that iterating over the array and assigning i (current index of iteration) to the object property, like myObj.A.forEach((d, i) => d.myIndex = i); 您可以对数组进行迭代并将i (当前迭代索引)分配给对象属性,例如myObj.A.forEach((d, i) => d.myIndex = i); , where d - is current item, and i - is current index. ,其中d - 是当前项目, i - 是当前索引。

 const myObj = { "@type": "someType", A: [ { "@type": "someType0", order: "DESC" }, { "@type": "someType1", order: "DESC" }, { "@type": "someType2", order: "DESC" } ], B: [], }; myObj.A.forEach((d, i) => d.myIndex = i); console.log(myObj); 

You could use a recursive approach and add for all arrays an index. 您可以使用递归方法并为所有数组添加索引。

 function setIndex(object) { Object.values(object).forEach(v => { if (Array.isArray(v)) { v.forEach((o, i) => { o.index = i; setIndex(o); }); } }); } const object = { "@type": "someType", A: [{ "@type": "someType0", order: "DESC" }, { "@type": "someType1", order: "DESC" }, { "@type": "someType2", order: "DESC" }], B: [] }; setIndex(object); console.log(object); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You can use a combination of Object.keys or Object.values and Array.forEach to achieve this. 您可以使用Object.keysObject.valuesArray.forEach的组合来实现此目的。 Using your 'myObject' variable as a starting point, something like this... 使用'myObject'变量作为起点,就像这样......

Using Object.keys 使用Object.keys

/* loop through keys of 'myObject' */
Object.keys(myObject).forEach(function(key) {
    /* check that 'myObject[key] is an Array &&
       loop through items in each 'myObject[key]'
       to add unique 'myIndex' property */
    Array.isArray(myObject[key]) && myObject[key].forEach(function(obj, idx) {
        obj.myIndex = idx;
    });
});

Using Object.values 使用Object.values

/* loop through 'myObject' property values */
Object.values(myObject).forEach(function(val) {
    /* check 'val' is an Array &&
       loop through each item in 'val' 
       to add unique 'myIndex' property */
    Array.isArray(val) && val.forEach(function(obj, idx) {
        obj.myIndex = idx;
    });
});

Hope that helps. 希望有所帮助。 :-D :-D

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

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