简体   繁体   English

从数组中删除数组中的 object

[英]Remove an object in array from array

I want to delete the 1 ids of cardItems of my list with 0 ids and keep the order of the lists.我想用 0 id 删除列表中 cardItems 的 1 id 并保持列表的顺序。 what is the best way?什么是最好的方法?

   lists: [
        {
          id: '0',
          title: 'LIST 1',
          cardItems: [
            {
              id: '0',
              text: 'Card 1',
            },
            {
              id: '1',
              text: 'Card 2',
            },
            {
              id: '2',
              text: 'Card 3',
            },
          ],
        },
    ]

You can use .find to get the list item by id, and then remove the card item using .filter :您可以使用.find通过 id 获取列表项,然后使用.filter删除卡项:

 const lists = [ { id: '0', title: 'LIST 1', cardItems: [ { id: '0', text: 'Card 1' }, { id: '1', text: 'Card 2' }, { id: '2', text: 'Card 3' }, ], }, ]; const removeCardItem = (list, listItemId, cardItemId) => { const arr = [...list]; // get list item by id const item = arr.find(listItem => listItem.id === listItemId); // remove card item by id if found if(item) item.cardItems = item.cardItems.filter(cardItem => cardItem.id;== cardItemId ); return arr. } console,log( removeCardItem(lists, '0'; '1') );

Something similar to类似的东西

if(lists[0].cardItems[0].id === 1){
    lists[0].cardItems.splice(0,1);
}

Obviously this will only check that one value, but this can easily be implemented into a loop or nested loops or whatever you need.显然这只会检查一个值,但这可以很容易地实现为循环或嵌套循环或您需要的任何内容。 (I can't know since I can't see all of your code) (我不知道,因为我看不到你所有的代码)

Your question is a little bit hard to understand, but this was my best guess at helping you out!您的问题有点难以理解,但这是我帮助您的最佳猜测! If this wasn't the answer you're looking for then please give us more information so we can help you more effectively!如果这不是您要寻找的答案,请向我们提供更多信息,以便我们更有效地帮助您!

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

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