Update if exists or add new element to array of objects - elegant way in javascript + lodash
- 阅读更多 关于 Update if exists or add new element to array of objects - elegant way in javascript + lodash
问题 So I have an array of objects like that: var arr = [ {uid: 1, name: "bla", description: "cucu"}, {uid: 2, name: "smth else", description: "cucarecu"}, ] uid is unique id of the object in this array. I'm searching for the elegant way to modify the object if we have the object with the given uid, or add a new element, if the presented uid doesn't exist in the array. I imagine the function to be behave like that in js console: > addOrReplace(arr, {uid: 1, name: 'changed name', description: