【问题标题】:How to replace the object with the new object using javascript or lodash?如何使用 javascript 或 lodash 用新对象替换对象?
【发布时间】:2020-09-07 16:11:23
【问题描述】:

我有如下的数据结构,

const data = {
    getSomething: {
        id: '1',
        items: [
            { 
                 id: '1',
                 orders: [
                     {
                         id: '1',
                         name: 'one1',
                     }
                 ],
                 subItems: [
                     { id: '1', name: 'subitem1' },
                     { id: '2', name: 'subitem2' },
                 ],
            },
            { 
                 id: '2',
                 orders: [
                     {
                         id: '2',
                         name: 'two1',
                     }
                 ],
                 subItems: [
                     { id: '2', name: 'subitem1' },
                     { id: '3', name: 'subitem2' },
                 ],
            }
        ],
    }
}

现在我有另一个数据,如下所示,

const item1 = { 
                 id: '2',
                 orders: [
                     {
                         id: '3',
                         name: 'one1',
                     }
                 ],
            }
        

我必须用上面的 item1 替换 data 中 id 为 1 的项目。我怎么能用lodash做到这一点。或任何其他我可以轻松做到的方式。

有人可以帮我解决这个问题吗?谢谢。

【问题讨论】:

  • 请尝试解决您自己的问题
  • 那么你最终得到了 2 个 id 为 2 的项目,这是故意的吗?
  • 不应该更换。

标签: javascript reactjs lodash


【解决方案1】:

const data = {
  getSomething: {
    id: '1',
    items: [{
        id: '1',
        orders: [{
          id: '1',
          name: 'one1',
        }],
        subItems: [{
            id: '1',
            name: 'subitem1'
          },
          {
            id: '2',
            name: 'subitem2'
          },
        ],
      },
      {
        id: '2',
        orders: [{
          id: '2',
          name: 'two1',
        }],
        subItems: [{
            id: '2',
            name: 'subitem1'
          },
          {
            id: '3',
            name: 'subitem2'
          },
        ],
      }
    ],
  }
}

const item1 = {
  id: '2',
  orders: [{
    id: '3',
    name: 'one1',
  }],
}

Object.assign(data.getSomething.items.find(item => item.id === item1.id),item1);

console.log(data);

【讨论】:

    【解决方案2】:

    你看起来像这样吗?

    const data = {
        getSomething: {
            id: '1',
            items: [
                { 
                     id: '1',
                     orders: [
                         {
                             id: '1',
                             name: 'one1',
                         }
                     ],
                     subItems: [
                         { id: '1', name: 'subitem1' },
                         { id: '2', name: 'subitem2' },
                     ],
                },
                { 
                     id: '2',
                     orders: [
                         {
                             id: '2',
                             name: 'two1',
                         }
                     ],
                     subItems: [
                         { id: '2', name: 'subitem1' },
                         { id: '3', name: 'subitem2' },
                     ],
                }
            ],
        }
    }
    
    const item1 = { 
                     id: '2',
                     orders: [
                         {
                             id: '3',
                             name: 'one1.......',
                         }
                     ],
                     subItems: [
                         { id: '2', name: 'subitem1....' },
                         { id: '3', name: 'subitem2....' },
                     ],
                }
    
    let result = data.getSomething.items.map(item=> item.id===item1.id ? ({...item1}): item);
    
    console.log('-----Original----', data.getSomething.items);
    console.log('-----Updated-----', result);

    【讨论】:

    • 谢谢。 lodash 中是否有方法可以做这样的事情?
    【解决方案3】:

    这将用 item1 覆盖数据中 id 为 1 的项目:

    const data = {
        getSomething: {
            id: '1',
            items: [
                { 
                     id: '1',
                     orders: [
                         {
                             id: '1',
                             name: 'one1',
                         }
                     ],
                     subItems: [
                         { id: '1', name: 'subitem1' },
                         { id: '2', name: 'subitem2' },
                     ],
                },
                { 
                     id: '2',
                     orders: [
                         {
                             id: '2',
                             name: 'two1',
                         }
                     ],
                     subItems: [
                         { id: '2', name: 'subitem1' },
                         { id: '3', name: 'subitem2' },
                     ],
                }
            ],
        }
    }
    
    const item1 = { 
                     id: '2',
                     orders: [
                         {
                             id: '3',
                             name: 'one1',
                         }
                     ],
                }
    
    const oldItem = data.getSomething.items.find(item => item.id == 1);
    Object.assign(oldItem, item1);
    console.log(data);
    

    【讨论】:

      猜你喜欢
      • 2016-10-20
      • 2019-01-04
      • 2016-10-30
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      • 2019-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多