is there a function in lodash to replace matched item

前端 未结 15 2324
孤街浪徒
孤街浪徒 2020-12-07 13:42

I wonder if there is a simpler method in lodash to replace an item in a JavaScript collection? (Possible duplicate but I did not understand the answer there:)

I look

15条回答
  •  长情又很酷
    2020-12-07 14:19

    If you're looking for a way to immutably change the collection (as I was when I found your question), you might take a look at immutability-helper, a library forked from the original React util. In your case, you would accomplish what you mentioned via the following:

    var update = require('immutability-helper')
    var arr = [{id: 1, name: "Person 1"}, {id:2, name:"Person 2"}]
    var newArray = update(arr, { 0: { name: { $set: 'New Name' } } })
    //=> [{id: 1, name: "New Name"}, {id:2, name:"Person 2"}]
    

提交回复
热议问题