【问题标题】:How can I find the index in nested array of objects?如何在嵌套对象数组中找到索引?
【发布时间】:2021-11-30 16:11:15
【问题描述】:

我有一个名为dishes 的嵌套对象数组,其中有唯一的ID。如果id 存在于foodItems 中的dishes 中,我想查找数组的索引,然后将其从foodItems 中删除

const dishes = [{
        cuisine: 'Chinese',
        foodItems: [{
                id: '1',
                dishName: 'Chicken noodles',
                price: '6.00 USD'
            },
            {
                id: '2',
                dishName: 'Fried rice',
                price: '5.00 USD'
            },
            {
                id: '3',
                dishName: 'Dumplings',
                price: '2.00 USD'
            }
        ],
    },
    {
        cuisine: 'Indian',
        foodItems: [{
                id: '4',
                dishName: 'Tikka masala',
                price: '10.00 USD'
            },
            {
                id: '5',
                dishName: 'Naan Bread',
                price: '2.00 USD'
            },
            {
                id: '6',
                dishName: 'Dal Fry',
                price: '4.00 USD'
            }
        ],
    }
];

这是我尝试过的(这不起作用并在index 中返回undefined):

let removeId = (id) => {
    let index = dishes.forEach(dish => dish.foodItems.findIndex(item => item.id === id)); // returns undefined
    if (index !== -1) {
        dishes.forEach(dish=> dish.foodItems.splice(index, 1)); // remove obj from foodItems array
        return true;
    } else {
        return false;
    }
}

【问题讨论】:

    标签: javascript arrays json object javascript-objects


    【解决方案1】:

    您需要在forEach() 回调方法中包含其余的逻辑。但是,考虑到您希望在删除项目后返回true,使用for ... of 循环会更容易:

    const dishes = [{
        cuisine: 'Chinese',
        foodItems: [{
            id: '1',
            dishName: 'Chicken noodles',
            price: '6.00 USD'
          },
          {
            id: '2',
            dishName: 'Fried rice',
            price: '5.00 USD'
          },
          {
            id: '3',
            dishName: 'Dumplings',
            price: '2.00 USD'
          }
        ],
      },
      {
        cuisine: 'Indian',
        foodItems: [{
            id: '4',
            dishName: 'Tikka masala',
            price: '10.00 USD'
          },
          {
            id: '5',
            dishName: 'Naan Bread',
            price: '2.00 USD'
          },
          {
            id: '6',
            dishName: 'Dal Fry',
            price: '4.00 USD'
          }
        ],
      }
    ];
    
    const removeId = (id) => {
        for (let dish of dishes) {
            const index = dish.foodItems.findIndex(fi => fi.id === id);
            if (index > -1) {
              dish.foodItems.splice(index, 1);
              return true;
            }
        };
        return false;
    }
    
    console.log(removeId('1'));
    console.log(removeId('4'));
    console.log(removeId('7'));
    
    console.log(dishes);

    【讨论】:

    • 谢谢,这有帮助。但是,一旦拼接操作成功,我还想返回一个布尔值true,如果没有,则返回false。当我尝试在if 语句末尾添加return true 到您的答案并执行console.log(removeId('1')) 时,它会打印undefined。有没有办法在拼接后返回true
    • @shariksid 更新了我的答案。
    • 谢谢你这回答了我的问题!我已经接受了 :) 另外,仅出于学习目的,如果拼接操作后foodItems 中没有对象,我会尝试从dishes 中删除完整对象。这可能吗?
    【解决方案2】:

    你可以试试这个,使用 for of 并添加条目变量你 for 循环

    const dishes = [
      {
        cuisine: "Chinese",
        foodItems: [
          {
            id: "1",
            dishName: "Chicken noodles",
            price: "6.00 USD",
          },
          {
            id: "2",
            dishName: "Fried rice",
            price: "5.00 USD",
          },
          {
            id: "3",
            dishName: "Dumplings",
            price: "2.00 USD",
          },
        ],
      },
      {
        cuisine: "Indian",
        foodItems: [
          {
            id: "4",
            dishName: "Tikka masala",
            price: "10.00 USD",
          },
          {
            id: "5",
            dishName: "Naan Bread",
            price: "2.00 USD",
          },
          {
            id: "6",
            dishName: "Dal Fry",
            price: "4.00 USD",
          },
        ],
      },
    ];
    
    for (const [idx, items] of dishes.entries()) {
      console.log(items.foodItems)
    }
    

    这里的文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

    【讨论】:

    • 只会抛出错误,甚至无法达到规定的要求。
    猜你喜欢
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 2021-07-27
    相关资源
    最近更新 更多