简体   繁体   中英

how to remove two json objects with same id

If I have a .json file with the following contents:

[
         {
                    "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? (note there are two objects with the same ID)

Would delete["1234"] work?

Use .filter to filter out elements of an array:

 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" :

 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 . You actually want to delete an object inside an array.

Use the good old for loop and 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.

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

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';  
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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