【问题标题】:Set the Index of each object, of an nested object into an property将嵌套对象的每个对象的索引设置为属性
【发布时间】:2018-08-01 13:03:19
【问题描述】:

早上好!

有谁知道如何将嵌套对象的每个对象的索引插入到属性中?

类似这样的:

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

【问题讨论】:

  • 你想插入myIndex对吗?
  • 谢谢大家!感谢所有帮助!
  • @LainIwakura 不要忘记投票并接受最能解决您的问题的答案。我们喜欢他们的支持:D

标签: javascript object nested


【解决方案1】:

您可以使用递归方法并为所有数组添加索引。

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; }

【讨论】:

  • 尼娜,你有博客之类的吗?您的回答总是非常简洁且经过深思熟虑。以至于我不遗余力地查看它们,即使是在我也回答过的问题上。
【解决方案2】:

您可以使用 .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 链接:https://codepen.io/bergur/pen/rrdyKX?editors=0010

如果您想遍历 myObj 的所有属性,您可以使用 Object.keys(myObj) 然后遍历它们并一一更改。

【讨论】:

  • 如果你改变对象,为什么还要映射?
【解决方案3】:

您可以对数组进行迭代并将i(迭代的当前索引)分配给对象属性,例如myObj.A.forEach((d, i) => d.myIndex = i);,其中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);

【讨论】:

    【解决方案4】:

    您可以使用Object.keysObject.valuesArray.forEach 的组合来实现此目的。使用您的“myObject”变量作为起点,类似这样...

    使用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;
        });
    });
    

    使用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;
        });
    });
    

    希望对您有所帮助。 :-D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-19
      • 2013-09-27
      • 2019-04-23
      • 2021-07-27
      • 1970-01-01
      • 2021-07-10
      相关资源
      最近更新 更多