var array = ["object1","object2","object3","object4","object5"];
var copy = array.slice();
copy.forEach(function(value) {
if(value === "object3"){
value = "newObject3"
}
});
console.log(copy );
如果我想将数组中的
object3
移动到第一个索引,请给它分配一个新值。我该怎么做?什么是最有效且更少的时间?可以使用lodash之类的任何库。 最佳答案
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);