【问题标题】:Updating objects of array in array with array of Object使用对象数组更新数组中的数组对象
【发布时间】:2021-04-29 03:15:12
【问题描述】:

我有两个 javaScript 数组

let x = [
    {
        id: 'Abc',
        children: [
            {
                id: 12,
                name: 'john'
            }, {
                id: 13,
                name: 'dow'
            }
        ]
    }, {
        id: 'xyz',
        children: [
            {
                id: 123,
                name: 'jack'
            }, {
                id: 134,
                name: 'abc'
            }
        ]
    }
]
let y = [
    {
        id: 12,
        name: 'mac'
    }, {
        id: 13,
        name: 'dow'
    }, {
        id: 123,
        name: 'Tom'
    }, {
        id: 134,
        name: 'abc'
    }
]

我想用y 更新我的x,并像这样更新数组

[
    {
        id: 'Abc',
        children: [
            {
                id: 12,
                name: 'mac'
            }, {
                id: 13,
                name: 'dow'
            }
        ]
    }, {
        id: 'xyz',
        children: [
            {
                id: 123,
                name: 'Tom'
            }, {
                id: 134,
                name: 'abc'
            }
        ]
    }
]

我试过这个solution 像这样

x.map((a, index)=>{
    a.children.map((b, i)=>{
        // console.log('update')
        y.find(o => o.id === b.id) || b;
    })
})

但我有undefined。我搜索了很多答案,但没有得到任何运气。

【问题讨论】:

    标签: javascript arrays json es6-modules jsobject


    【解决方案1】:

    首先为y 制作循环对象,然后为x 使用map 的子对象

    let x = [
      {
        id: "Abc",
        children: [
          {
            id: 12,
            name: "john",
          },
          {
            id: 13,
            name: "dow",
          },
        ],
      },
      {
        id: "xyz",
        children: [
          {
            id: 123,
            name: "jack",
          },
          {
            id: 134,
            name: "abc",
          },
        ],
      },
    ];
    let y = [
      {
        id: 12,
        name: "mac",
      },
      {
        id: 13,
        name: "dow",
      },
      {
        id: 123,
        name: "Tom",
      },
      {
        id: 134,
        name: "abc",
      },
    ];
    
    const lookupY = {};
    y.forEach(({ id, name }) => (lookupY[id] = name));
    
    const newX = x.map(({ id, children }) => ({
      id,
      children: children.map((item) => ({ id:item.id, name: lookupY[item.id] })),
    }));
    
    console.log(newX)

    【讨论】:

      【解决方案2】:
      x.map((a, index)=>{
          a.children.map((b, i)=>{
              // console.log('update')
              y.find(o => o.id === b.id) || b;
          })
      })
      

      首先,您在使用数组函数时犯了一个常见错误: 括号{}对于一行指令是可选的,但是你需要指定return关键字。

      arr.filter(v => v === 2) 等价于arr.filter(v => {return v === 2})。忘记returnfilter() 将返回一个空数组。

      单线解决方案:

      const res = x.map((a, index) => ({ ...a, children: a.children.map((b, i) => y.find(o => o.id === b.id) || b) }));
      

      代码 sn-p :

      let x = [
          {
              id: 'Abc',
              children: [
                  {
                      id: 12,
                      name: 'john'
                  }, {
                      id: 13,
                      name: 'dow'
                  }
              ]
          }, {
              id: 'xyz',
              children: [
                  {
                      id: 123,
                      name: 'jack'
                  }, {
                      id: 134,
                      name: 'abc'
                  }
              ]
          }
      ]
      let y = [
          {
              id: 12,
              name: 'mac'
          }, {
              id: 13,
              name: 'dow'
          }, {
              id: 123,
              name: 'Tom'
          }, {
              id: 134,
              name: 'abc'
          }
      ]
      
      const res = x.map((a, index) => 
        ({ ...a, children: a.children.map((b, i) => y.find(o => o.id === b.id) || b) }));
      
      console.log(res);

      【讨论】:

        【解决方案3】:

        首先你忘了在回调函数中返回结果。那么你不应该丢失对象的其他键。

        const x = [
          {
            id: 'Abc',
            children: [
              {
                id: 12,
                name: 'john'
              }, {
                id: 13,
                name: 'dow'
              }
            ]
          }, {
            id: 'xyz',
            children: [
              {
                id: 123,
                name: 'jack'
              }, {
                id: 134,
                name: 'abc'
              }
            ]
          }
        ];
        const y = [
          {
            id: 12,
            name: 'mac'
          }, {
            id: 13,
            name: 'dow'
          }, {
            id: 123,
            name: 'Tom'
          }, {
            id: 134,
            name: 'abc'
          }
        ];
        
        const newArr = x.map((a, index) => {
          const children = a.children.map((b, i) => {
            return y.find(o => o.id === b.id) || b;
          })
          return { ...a, children };
        })
        
        console.log(newArr);

        【讨论】:

          猜你喜欢
          • 2023-03-04
          • 2021-07-08
          • 2022-11-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-12
          相关资源
          最近更新 更多