Javascript: moving elements in an array -
var array = ["object1","object2","object3","object4","object5"]; var copy = array.slice(); copy.foreach(function(value) { if(value === "object3"){ value = "newobject3" } }); console.log(copy );
if want move object3
in array first index, after assigned new value. how do it? , whats effective , less time? libraries lodash can used.
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);
Comments
Post a Comment