JavaScript How to edit value of an object using the reference? edit value object,edit object value javascript,php edit object

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.

Answer:1

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))
Answer:2

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);
Answer:3

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;
Answer:4

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 ...

  1. jquery returning object object
  2. jquery not returning object
  3. jquery returning empty object
  4. jquery selector not returning object

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 ...

  1. node await catch exception
  2. node await catch
  3. node await catch error
  4. node await try catch
  5. node js await catch error
  6. node async await catch error
  7. node js await catch
  8. node async await catch
  9. node await then catch
  10. node js async await catch
  11. node async await try catch
  12. node js await try catch

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 ...

  1. angularjs file uploading
  2. angularjs file upload example
  3. angularjs file upload example jsfiddle
  4. angularjs file upload formdata
  5. angularjs file upload tutorial
  6. angularjs file upload with progress
  7. angularjs file upload validation
  8. angularjs file upload directive
  9. angularjs file upload example plunker
  10. angularjs file upload get path
  11. angularjs file upload drag and drop
  12. angularjs file upload get filename
  13. angularjs file upload progress bar
  14. angularjs file upload max size
  15. angularjs file upload with form data
  16. angularjs file upload progress bar example
  17. angularjs file upload w3schools
  18. angularjs file upload base64
  19. angularjs file upload ng-change
  20. angularjs file upload size validation

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 ...

  1. javascript functions have arguments
  2. do javascript functions have to return a value
  3. javascript can functions have properties
  4. functions have javascript