简体   繁体   English

遍历 object 的数组并更改 object 的值

[英]Iterate over an array of object and change object value

I'm trying to iterate over an array of objects that looks like this:我正在尝试遍历一个看起来像这样的对象数组:

const arr = [{
  id: 1,
  name: "John",
  storeName: "Amazon",
  price: 100,
  isRecieved: false,
  deliveryDate: some date
}, ....]

I'm iterating over the array with product ID that I received from the user, finding the relevant item according to it's id and changing the key "isRecieved" from false to true.我正在使用从用户那里收到的产品 ID 遍历数组,根据它的 ID 找到相关项目,并将键“isRecieved”从 false 更改为 true。

This is my code:这是我的代码:

const filterReceivedProduct = (list, id) => {
  const changeReceivedState = list.filter((item) => {
      return item.id === id ? **item[isRecieved]** = true : item;
  })
  return changeReceivedState
}

But when I'm trying to access the object using bracket notation it return the value (in this case 'false') and I can't change the value to true..但是当我尝试使用括号表示法访问 object 时,它返回值(在本例中为“false”)并且我无法将值更改为 true..

What I'm doing wrong?我做错了什么?

You don't need to use .filter() here.你不需要在这里使用.filter() The filter method is for removing elements from an array. filter 方法用于从数组中删除元素。 What you want to do is map each element/object to a new object, which has all the properties from the old object, containing an updated isRecieved property.您要做的是将每个元素/对象map转换为新的 object,它具有旧 object 的所有属性,包含更新的isRecieved属性。 To create a new object with all the properties of the old object, you can spread the properties and values from the old object into a new one ( {...item} ).要创建具有旧 object 的所有属性的新 object,您可以将旧object的属性和值扩展到新的 ( {...item} )。 You can then update the isRecived based on your whether the item id matched the id passed into your function:然后,您可以根据项目 ID 是否与传递到您的 function 中的 ID 匹配来更新isRecived

 const arr = [{ id: 1, name: "John", storeName: "Amazon", price: 100, isRecieved: false, deliveryDate: 'some date' }, { id: 2, name: "Alex", storeName: "Apple", price: 200, isRecieved: false, deliveryDate: 'some date2' }]; const filterReceivedProduct = (list, id) => list.map(item => ({...item, isRecieved: item.id === id // sets isRecieved to true or false based on the equalty comparison })); const res = filterReceivedProduct(arr, 1); console.log(res);

By creating a new object within the .map() callback, you're never mutating the original objects in your arr , treating everything as immutable.通过在.map()回调中创建一个新的 object ,您永远不会改变arr中的原始对象,将所有内容视为不可变的。

Here filter make no sense.这里的filter没有意义。 Filter use to filter some array based on condition.过滤器用于根据条件filter一些数组。 If you just want to change some property of an object inside array then use map for this purpose.如果您只想更改数组内部 object 的某些属性,请为此目的使用map

See below example.请参见下面的示例。

 const arr = [{ id: 1, name: "John", storeName: "Amazon", price: 100, isRecieved: false, deliveryDate: 'some date' }, { id: 2, name: "Doe", storeName: "Amazon", price: 100, isRecieved: false, deliveryDate: 'some date' }]; filterReceivedProduct = (list, id) => { return list.map(item => { if(item.id === id) { item.isRecieved = true; } return item; }); } console.log(filterReceivedProduct(arr, 1))

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

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