I have two arrangements:
const myArray1 = [{updatedProp: 'prop'}, {updatedProp2: 'prop2'}];
const myArray2 = [{oldProp: 'oldProp'}, 'Una cadena', {oldProp2: 'oldProp2'}];
How do I make it so that under the condition that if the element is a Object
, it must be replaced by the new one in the order the arrays are initially found?
The result I expect is:
[updatedProp, 'A string', updatedProp2]
That is, something like:
const myUpdateArray = myArray2.map(element => {
if(typeof element === 'object'){
return //elemento actualizado
}
});
To solve this simply create an index starting at 0, map the old array and if the property is an object (
typeof p === 'object'
) return the update array property at the position of the current index and then increment the index, if not an object ( the current item of the old array) the value of index will not be increased so the position will be reserved for when it is an object again, all this is taking into account that you ask for a replacement in the order that the arrays are initially found.Look at the example :
I hope it helps, greetings.
I turned the array with the updated elements upside down and replaced the old elements with the updated ones at the object's position whenever the condition required it by popping them out in order with pop().