I have a big object array persons
persons = [{name:'john1'}, {name:'john2'},...]
I iterated the array and found the object I am interested to edit
objectToEdit = persons .find((person)=>person.name==='john1')
Now I created an edited object in immutable way (someOtherPerson = {name:'johnxx'}
)
objectFinal = {...objectToEdit, someOtherPerson}
Now I want to replace this objectFinal
with objectToEdit
in persons
array, without having to traverse the array again. But doing objectToEdit =objectFinal
, will just assign objectToEdited's reference to objectToEdit
, without making any change in the persons
array
Is there a clean way to achieve this without traversing the array?
Edit:
In this example, the object in persons jave just one key (i.e, name
). This is to make question minimal. In my project, I have more than 30 keys.
If you want to edit an object in a list,in place, use Array.prototype.some
var persons = [{
name: 'john1'
}, {
name: 'jack5'
}]
var someOtherPerson = {
name: 'johnxx'
}
persons.some(function(person) {
// if condition, edit and return true
if (person.name === 'john1') {
// use Object.keys to copy properties
Object.keys(someOtherPerson).forEach(function(key) {
person[key] = someOtherPerson[key]
})
// or use assign to merge 2 objects
Object.assign(person, someOtherPerson);
return true // stops iteration
}
})
console.log(JSON.stringify(persons))
If you want to avoid mutating the objects in the original array, you might use .findIndex
instead, and reassign the item in the array at that index:
const persons = [{name:'john1'}, {name:'john2'}];
const objectToEditIndex = persons.findIndex((person) => person.name === 'john1');
const someOtherPerson = {name:"johnxx"};
persons[objectToEditIndex] = {
...persons[objectToEditIndex],
...someOtherPerson
};
console.log(persons);
Doing this:
objectFinal = {...objectToEdit, someOtherPerson}
you lose the reference to the original objectToEdit
object. To edit it, you can do
`objectToEdit.value = otherValue`.
PS: Having said that, you only lose reference to the first level object. If that object, contains another object or array, you have reference to it:
objectFinal.innerObject.value = otherValue;
I have 4 inputs in a single form: 1 - Name 2 - First Name 3 - Last Name 4 - Staff Name (Hidden input) I need to mirror the values inside Name text field onto the Staff Name text field everytime the ...
I have 4 inputs in a single form: 1 - Name 2 - First Name 3 - Last Name 4 - Staff Name (Hidden input) I need to mirror the values inside Name text field onto the Staff Name text field everytime the ...
I am busy working with some code that is responing in an unexpected way (to me). It involves handling Node.js promise exceptions. I have the following function modified so that all it does is fail ...
I am busy working with some code that is responing in an unexpected way (to me). It involves handling Node.js promise exceptions. I have the following function modified so that all it does is fail ...
I am trying to upload-file using angularFileUpload in php,but i will get this error in angular. [$injector:unpr] Unknown provider: $uploadProvider I have tried answer from this question but no ...
I am trying to upload-file using angularFileUpload in php,but i will get this error in angular. [$injector:unpr] Unknown provider: $uploadProvider I have tried answer from this question but no ...
function myFunc(){ console.log(myFunc.message); } myFunc.message = "Hi John"; myFunc(); Executing the above results in - Answer: 'Hi John' How is the function myFunc have the property message ...
function myFunc(){ console.log(myFunc.message); } myFunc.message = "Hi John"; myFunc(); Executing the above results in - Answer: 'Hi John' How is the function myFunc have the property message ...