How can JS find an object with a specific key value in the Array with an object and assign values to other keys of that object

clipboard.png
as shown in the figure, I want to find the desired object with a specific id and be able to assign values to its contents.

it is known that you can use underscore_.findewhere

but want to know how everyone does it? How to write 0 0

in js native database
Mar.02,2021

function findWhere(list, properties) {
  const propertiesArray = Object.entries(properties)
  return list.find(item => {
    return propertiesArray.every(pair => {
      const key = pair[0]
      const value = pair[1]
      return item[key] && item[key] === value
    })
  })
}

const result = findWhere(shoppinglists, {id: 'clothes', title: 'Clothes'}) // result

tell me about the defect.

  1. will only return one object that satisfies the condition. If there is more than one object that meets the condition, you can overwrite it with the forEach method.
  2. if the value is a reference type, it cannot be determined and additional logic is required.
Menu