简体   繁体   English

根据数组对象数据创建一个新数组

[英]Create a new array based on array object data

I have a nested objects in arrays.我在数组中有一个嵌套对象。

{
  "page": [
    {
      "num": "1",
      "comp": [
        {
        "foo": "bar",
        "bar": "foo",
        "name": "comp1",
          "items": [
            {
              "fooBar":"barFoo",
              "itemData": 
                {
                  "foo": "bar",
                  "bar": "foo",
                  "fooBar": "barFor"
                }
              
            }
          ]
        }
      ]
    },
    {
      "num": "2",
      "comp": [
        {
        "foo": "bar",
        "bar": "foo",
        "name": "comp2",
          "items": [
            {
              "fooBar":"barFoo",
              "itemData": 
                {
                  "foo": "bar",
                  "bar": "foo",
                  "fooBar": "barFor"
                }
              
            }
          ]
        },
        {
          "items": [
            {
              "fooBar":"barFoo2",
              "itemData": 
                {
                  "foo": "bar",
                  "bar": "foo",
                  "fooBar": "barFor"
                }
              
            },
            {
              "itemData": 
                {
                  "foo": "bar",
                  "bar": "foo",
                  "fooBar": "barFor"
                }
              
            }
          ]
        }
      ]
    },
    {
      "num": "3"
    }
  ]
}

So what I'm trying to do is create a new array with itemData but keep the general structure of the object intact.所以我想做的是用itemData创建一个新数组,但保持对象的一般结构完好无损。

I can't just delete every key/values not needed cause the key can change or new ones can be added and it would be hard to maintain.我不能只删除不需要的每个键/值,因为键可以更改或可以添加新键,并且很难维护。 So I was thinking of a few nested for loops to get to the itemData object:所以我在考虑使用一些嵌套的 for 循环来获取itemData对象:

for (const [i, page] of this.testingAnimation.pages.entries()){
                for (const [j, comp] of page.comp.entries()){
                    for (const [k, item] of comp.items.entries()){
                        if (item.itemData !== undefined) {
                            
                        } else {

                        }
                    }
                }
            }

but now that I have itemData how do I put it in the array while keep the general nested structure of the original object just without the extra key/values但是现在我有了itemData如何将它放入数组中,同时保持原始对象的一般嵌套结构,而无需额外的键/值

expected Output:预期输出:

{
  "page": [
    {
      "comp": [
        {
          "items": [
            {
              "itemData": 
                {
                  "foo": "bar",
                  "bar": "foo",
                  "fooBar": "barFor"
                }
            }
          ]
        }
      ]
    },
    {
      "comp": [
        {
          "items": [
            {
              "itemData": 
                {
                  "foo": "bar",
                  "bar": "foo",
                  "fooBar": "barFor"
                }
              
            }
          ]
        },
        {
          "items": [
            {
              "itemData": 
                {
                  "foo": "bar",
                  "bar": "foo",
                  "fooBar": "barFor"
                }
              
            },
            {
              "itemData": 
                {
                  "foo": "bar",
                  "bar": "foo",
                  "fooBar": "barFor"
                }
              
            }
          ]
        }
      ]
    },
    {
      "num": "3"
    }
  ]
}

How about you replace your inner loop (the for (const [k, item] of comp.items.entries()) {...} part) with this:你如何用这个替换你的内部循环( for (const [k, item] of comp.items.entries()) {...}部分):

comp.items = comp.items.map(item => ({ itemData: item.itemDate }));

That will just replace each comp.items array with a new array, where each object only has the single key you want to keep.这只会用一个新数组替换每个comp.items数组,其中每个对象只有您想要保留的单个键。

If you don't actually want to modify the data in-place (ie, create a new data structure with the desired structure, and leave alone the original), you can use something like the following:如果您实际上不想就地修改数据(即,创建一个具有所需结构的新数据结构,而不是原始数据结构),您可以使用以下内容:

const filtered = {
  ...this.testingAnimation,
  page: this.testingAnimation.page.map(page =>
    'comp' in page
      ? {
        ...page,
        comp: page.comp.map(comp =>
          'items' in comp
            ? {
              ...comp,
              items: comp.items.map(item => ({ itemData: item.itemData }))
            }
            : comp
          )
      }
      : page
  )
}

Note: this solution is not using Object.entries() , but if you want to use that, you should use the standard-compliant Object.entries(someObject) because in general, someObject.entries() is not defined.注意:这个解决方案没有使用Object.entries() ,但如果你想使用它,你应该使用符合标准的Object.entries(someObject)因为一般来说, someObject.entries()没有定义。 Also, for arrays, this function can be used, but in general it makes more sense to use someArray.forEach(...) or newArray = someArray.map(item => ...) .此外,对于数组,可以使用此函数,但通常使用someArray.forEach(...)newArray = someArray.map(item => ...)更有意义。 MDN is a great resource for more help. MDN是获得更多帮助的绝佳资源。

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

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