0
var array = ["object1","object2","object3","object4","object5"];
var copy = array.slice();
copy.forEach(function(value) {
 if(value === "object3"){
  value = "newObject3"

 }
});

console.log(copy );

If i want to move object3 in the array to the first index, after i assigned it a new value. How do I do it? and Whats the most effective and less time? any libraries like lodash can be used.

Jan
  • 3,393
  • 4
  • 21
  • 47

1 Answers1

1

var array = ["object1", "object2", "object3", "object4", "object5"];
var copy = array.slice();
copy.forEach(function(value, index, theArray) {
  if (value === "object3") {
    theArray[index] = theArray[0];
    theArray[0] = "newObject3";
  }
});

console.log(copy);
Weedoze
  • 13,683
  • 1
  • 33
  • 63