javascript - How to delete array element from JSON array of objects by ID - Stack Overflow

This is probably quite easy, but giving me trouble. Given this JSON structure:"playlists" : [

This is probably quite easy, but giving me trouble. Given this JSON structure:

 "playlists" : [
      {
        "id" : "1",
        "owner_id" : "2",
        "song_ids" : [
          "8",
          "32"
        ]
      },
      {
        "id" : "2",
        "owner_id" : "3",
        "song_ids" : [
          "6",
          "8",
          "11"
        ]
      },
      {
        "id" : "3",
        "owner_id" : "7",
        "song_ids" : [
          "7",
          "12",
          "13",
          "16",
          "2"
        ]
      }
    ]

How would you delete an object from the array by key/value? In this case by ID? playlist.splice(1,1)? playlist.delete(id)? Not sure how to do this elegantly. Let's say I wish to delete the element with ID = 3, how to get this result:

 "playlists" : [
      {
        "id" : "1",
        "owner_id" : "2",
        "song_ids" : [
          "8",
          "32"
        ]
      },
      {
        "id" : "2",
        "owner_id" : "3",
        "song_ids" : [
          "6",
          "8",
          "11"
        ]
      }
]


This is probably quite easy, but giving me trouble. Given this JSON structure:

 "playlists" : [
      {
        "id" : "1",
        "owner_id" : "2",
        "song_ids" : [
          "8",
          "32"
        ]
      },
      {
        "id" : "2",
        "owner_id" : "3",
        "song_ids" : [
          "6",
          "8",
          "11"
        ]
      },
      {
        "id" : "3",
        "owner_id" : "7",
        "song_ids" : [
          "7",
          "12",
          "13",
          "16",
          "2"
        ]
      }
    ]

How would you delete an object from the array by key/value? In this case by ID? playlist.splice(1,1)? playlist.delete(id)? Not sure how to do this elegantly. Let's say I wish to delete the element with ID = 3, how to get this result:

 "playlists" : [
      {
        "id" : "1",
        "owner_id" : "2",
        "song_ids" : [
          "8",
          "32"
        ]
      },
      {
        "id" : "2",
        "owner_id" : "3",
        "song_ids" : [
          "6",
          "8",
          "11"
        ]
      }
]


Share Improve this question edited Apr 20, 2022 at 13:24 Rachel asked Apr 20, 2022 at 13:22 RachelRachel 7273 gold badges14 silver badges22 bronze badges 2
  • Could use Array.filter, which will return a copy of the array with the filtered out elements removed. – Alicia Sykes Commented Apr 20, 2022 at 13:24
  • Or, you could use Array.findIndex to get the index of an element with given ID, then Array.splice to remove that element. I've written you an example below :) – Alicia Sykes Commented Apr 20, 2022 at 13:41
Add a ment  | 

1 Answer 1

Reset to default 6

Using Array.filter, you can filter out elements that don't match a certain condition. For example:

const result = playlists.filter(playlist => playlist.id !== '2');

Here's a working demo:

/* Example Data */
const playlists = [
  {
    "id" : "1",
    "owner_id" : "2",
    "song_ids" : [ "8", "32"]
  },
  {
    "id" : "2",
    "owner_id" : "3",
    "song_ids" : ["6", "8","11" ]
  }
];

/* Takes a list of playlists, and an ID to remove */
const removePlaylistById = (plists, id) =>
  plists.filter(playlist => playlist.id !== id);

/* Removes playlist ID 2 from list, prints result */
const result = removePlaylistById(playlists, '2');
console.log(result);


Another option, would be to use Array.findIndex to get the index of an element with given ID, then use Array.splice to remove that element. This will modify the array, without the need for a copy.

For example:

const indexToRemove = playlists.findIndex((pl) => pl.id === '2');
playlists.splice(indexToRemove, 1);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744072865a4553861.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信