简体   繁体   English

如何删除具有相同ID的两个json对象

[英]how to remove two json objects with same id

If I have a .json file with the following contents: 如果我有一个具有以下内容的.json文件:

[
         {
                    "id": "1234",
                    "day": "Monday",
                    "course": "Math110",

         },
         {
                    "id": "1234",
                    "day": "Wednesday",
                    "title": "Math110",

         },
         {
                    "id": "1345",
                    "day": "Tuesday",
                    "title": "Economics210",

         }
]

How can I remove all the objects with id "1234" in Javascript? 如何在JavaScript中删除ID为“ 1234”的所有对象? (note there are two objects with the same ID) (请注意,有两个具有相同ID的对象)

Would delete["1234"] work? delete["1234"]有效?

Use .filter to filter out elements of an array: 使用.filter过滤掉数组的元素:

 const input = [{ "id": "1234", "day": "Monday", "course": "Math110", }, { "id": "1234", "day": "Wednesday", "title": "Math110", }, { "id": "1345", "day": "Tuesday", "title": "Economics210", } ]; const output = input.filter(({ id }) => id !== '1234'); console.log(output); 

You can parse the JSON to an array and then use Array.prototype.filter to get a new array which doesn't have the objects with id "1234" : 您可以将JSON解析为一个数组,然后使用Array.prototype.filter获取一个新的数组,该数组不包含ID为"1234"的对象:

 const json = '[{"id":"1234","day":"Monday","course":"Math110"},{"id":"1234","day":"Wednesday","title":"Math110"},{"id":"1345","day":"Tuesday","title":"Economics210"}]'; const result = JSON.parse( json ).filter( obj => obj.id !== "1234" ); console.log( JSON.stringify( result, null, ' '.repeat(8) ) ); 

You cannot use delete operator because its for deleting a property in an object . 您不能使用delete运算符,因为它用于删除对象中的属性 You actually want to delete an object inside an array. 您实际上要删除数组内的对象。

Use the good old for loop and Array.splice() : 使用好旧的for循环和Array.splice()

 var inputArray = [{ "id": "1234", "day": "Monday", "course": "Math110", }, { "id": "1234", "day": "Wednesday", "title": "Math110", }, { "id": "1345", "day": "Tuesday", "title": "Economics210", } ]; var deleteId = "1234"; for (var i = 0; i < inputArray.length; i++) { if (inputArray[i].id === deleteId) { inputArray.splice(i, 1); i = i-1; } } console.log(inputArray); 

Note: If you are fine with creating another array and not modifying the existing array, use Array.filter() as mentioned in the other answers. 注意:如果可以创建另一个数组而不修改现有数组,可以使用其他答案中提到的Array.filter()

You have to delete by specifying the position of the item in the array: 您必须通过指定项目在数组中的位置来删除:

 const arr = [{ "id": "1234", "day": "Monday", "course": "Math110", }, { "id": "1234", "day": "Wednesday", "title": "Math110", }, { "id": "1345", "day": "Tuesday", "title": "Economics210", } ]; arr.forEach(function(item, i){ if(item.id == "1234") delete arr[i]; }); console.log(arr.filter(j => j)) 

You can use filter method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter 您可以使用过滤器方法: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

items = [
    {
        "id": "1234",
        "day": "Monday",
        "course": "Math110",

    },
    {
         "id": "1234",
         "day": "Wednesday",
         "title": "Math110",

    },
    {
         "id": "1345",
         "day": "Tuesday",
         "title": "Economics210",

     }
];

items.filter(function(item) { 
   return item.id !== '1234';  
});

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

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