简体   繁体   English

我如何在 React 的数组中更新 object 中的键值?

[英]How do i update value of a key in an object inside an Array in React?

I have a dynamic list of items a user adds.我有一个用户添加的项目的动态列表。 I want to avoid duplicating when a user adds an item already present in the list.当用户添加列表中已存在的项目时,我想避免重复。 My list looks like我的清单看起来像

itemList = [ {itemId:1, name:"x", quantity:5}, {itemId:4, name:"y", quantity:2}]

so now if a user adds item x with quantity 2 i want the object with item x to update the quantity to 7 rather than adding a whole new object. I am using find() method to get the item already present and storing it to a variable, itemObj is the item the user recently added.所以现在如果用户添加数量为 2 的项目 x ,我希望带有项目 x 的 object 将数量更新为 7,而不是添加一个全新的 object。我正在使用 find() 方法获取已经存在的项目并将其存储到变量,itemObj 是用户最近添加的项目。

let alrItem = state.itemList.find(
    (e) => e.itemId === itemObj.itemId
  );
let newItem = alrItem;
newItem.quantity += itemObj.quantity;

How do I merge this newItem to the itemList so that it just updates the quantity of that specific item?我如何将这个 newItem 合并到 itemList 以便它只更新该特定项目的数量?

What you are doing is finding the object in the itemList array and then mutating the state directly.您正在做的是在itemList数组中找到 object,然后直接改变 state。 State should not be mutated directly. State 不应直接突变。

Instead of using .find() method, use the .map() method to iterate over the array and update the quantity of the item that matches with the id of the new item.不使用.find()方法,而是使用.map()方法遍历数组并更新与新项目 ID 匹配的项目数量。

let updatedItemList = state.itemList.map((item) => {
   if (item.itemId === itemObj.itemId) {
      return { ...item, quantity: item.quantity + itemObj.quantity };
   }
   return item;
});

// update the state
setItemList(updatedItemList);

Note that above code will not do anything if the item isn't already present in the itemList .请注意,如果该项目尚未出现在itemList中,则上述代码将不会执行任何操作。 Ideally, you should also handle the case where the new item isn't already present in the itemList .理想情况下,您还应该处理新项目尚未出现在itemList中的情况。 In this case, you should just add the new item in the itemList .在这种情况下,您应该只在itemList中添加新项目。

To handle this case, all you need is a extra variable that can be used to know whether the if condition inside the .map() method evaluated to true or not.要处理这种情况,您所需要的只是一个额外的变量,该变量可用于了解.map()方法中的if条件是否计算为真。

let exists = false;

let updatedItemList = state.itemList.map((item) => {
   if (item.itemId === itemObj.itemId) {
      exists = true;
      return { ...item, quantity: item.quantity + itemObj.quantity };
   }
   return item;
});

// if the item isn't present in the list, add it in the "updatedItemList"
if (!exists) {
   updatedItemList.push(itemObj);
}

// update the state
setItemList(updatedItemList);
let itemList = [ {itemId:1, name:"x", quantity:5}, {itemId:4, name:"y", quantity:2}]
let itemObj = {itemId:1, name:"x", quantity:2}

const target = itemList.find(element =>
    element.itemId === itemObj.itemId
);

if (target) {
    target.quantity = target.quantity + itemObj.quantity;
} else {
    itemList.push(itemObj);
}

console.log('itemList: ' + JSON.stringify(itemList));

OUTPUT: OUTPUT:

itemList: [{"itemId":1,"name":"x","quantity":7},{"itemId":4,"name":"y","quantity":2}] itemList: [{"itemId":1,"name":"x","quantity":7},{"itemId":4,"name":"y","quantity":2}]

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

相关问题 我如何更新状态以使用 array.map 函数更新数组内对象的值 - how do i update state in react for updating the value of an object inside an array using array.map function 我如何在React状态下更新数组内部的对象属性的值 - How do I update the value of an object property inside of an array in React state 如何用React Hooks更新对象内部数组的特定值? - How to update with React Hooks specific value of an array inside an object? 如何使用变量键正确更新反应 state object? - How do I update a react state object correctly with a variable key? 如何从数组内部的 object 中找到匹配的键/对值 - How do I find matching key/pair value from object inside of an array 如果我在对象中有一个数组并且该对象在一个大数组中,我该如何更新 React Native 状态? - How can I update the React Native state if I have an array inside an object and the object is inside in a big array? 我在javascript中更新了一个数组(key,value)对象 - I update an array (key,value) object in javascript 我如何使用 setState 更新数组内的对象 - How do i use setState to update an object inside an array 如何在 React Hooks 中更新 object 数组中的状态 `onChange` - How do I update states `onChange` in an array of object in React Hooks 如何在 React 中访问 Array 中的对象值? - How do I access object's values inside of Array in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM