【问题标题】:Remove inner element from JSON array in Angular从Angular中的JSON数组中删除内部元素
【发布时间】:2020-10-06 15:37:50
【问题描述】:

我有以下 JSON 数组。

[{
    "Group": "Title1",
    "subGroup": [{
        "name": "Test1",
        "id": 3
    }, {
        "name": "Hello",
        "id": 4
    }]
},
 {
    "Group": "Title2",
    "subGroup": [{
        "name": "Test2",
        "id": 5
    }, {
        "name": "Test2",
        "id": 6
    }]
}]

我只想在 name = "Hello" 时删除整个 JSON 对象。我有以下代码

  let deleteIndex=-1;
    MyData.forEach(sampleData => {
      deleteIndex = sampleData.subGroup.findIndex(data=> data.name === 'Hello');
      console.log(deleteIndex);
      delete sampleData.subGroup[deleteIndex];
    });
    console.log(JSON.stringify(MyData));
  }

很遗憾,这不起作用。我想要这样的数据

 [{
        "Group": "Title1",
        "subGroup": [{
            "name": "Test1",
            "id": 3
        }]
    },
     {
        "Group": "Title2",
        "subGroup": [{
            "name": "Test2",
            "id": 5
        }, {
            "name": "Test2",
            "id": 6
        }]
    }]

【问题讨论】:

    标签: javascript arrays json angular


    【解决方案1】:

    尝试使用过滤器。

    const arr = [{
        "Group": "Title1",
        "subGroup": [{
            "name": "Test1",
            "id": 3
        }, {
            "name": "Hello",
            "id": 4
        }]
    },
     {
        "Group": "Title2",
        "subGroup": [{
            "name": "Test2",
            "id": 5
        }, {
            "name": "Test2",
            "id": 6
        }]
    }];
    for (const item of arr) {
      item.subGroup = item.subGroup.filter(subitem => subitem.name !== 'Hello');
    }
    console.log(arr);

    【讨论】:

      【解决方案2】:

      我会为此使用mapfilter

      const newData = MyData.map(data => ({
         // spread the old data properties
         ...data,
         // for the subGroup array, only keep the ones where .name does not equal 'hello'
         subGroup: data.subGroup.filter(subData => subData.name !== 'hello'),
      }));
      
      console.log(newData);
      

      【讨论】:

        【解决方案3】:

        你可以简单地使用Array.map来完成它

        const input = [{
            "Group": "Title1",
            "subGroup": [{
                "name": "Test1",
                "id": 3
            }, {
                "name": "Hello",
                "id": 4
            }]
        },
         {
            "Group": "Title2",
            "subGroup": [{
                "name": "Test2",
                "id": 5
            }, {
                "name": "Test2",
                "id": 6
            }]
        }];
        
        const output = input.map((item) => ({
          Group: item.Group,
          subGroup: item.subGroup.filter(({ name }) => name !== 'Hello')
        }));
        
        console.log(output);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-09-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-19
          • 2020-08-19
          • 1970-01-01
          相关资源
          最近更新 更多