我试图将同一个对象推入一个数组,并对对象进行一些修改。但是当我修改并推送对象时,数组中的第一个对象被覆盖。
var details = [{"name":"john","id":2,"personalEmail":"john@gmail.com","workEmail":"wa2@gmail.com"}]; var searchData = "joh";
var pattern1 = new RegExp("(?:^|[\\s\@])"+searchData, "i");
var data = [];
for(var i=0;i<details.length;i++){
var name = details[i].name;
if(pattern1.test(name)){
var dom = name.replace(name.match(pattern1), '<strong>'+ name.match(pattern1) + '</strong>');
details[i].name = dom;
data.push(details[i]);
console.log("first ::::"+JSON.stringify(data));
if(details[i].workEmail != null){
details[i].personalEmail = details[i].workEmail;
data.push(details[i]);
console.log("second::::"+JSON.stringify(data));
}
}
}
这是小提琴http://jsfiddle.net/LQg7W/2311/。我们可以在控制台(F12 控制台选项卡)中看到输出。
输出获取:
second::::[{"name":"<strong>joh</strong>n","id":2,"personalEmail":"wa2@gmail.com","workEmail":"wa2@gmail.com"},{"name":"<strong>joh</strong>n","id":2,"personalEmail":"wa2@gmail.com","workEmail":"wa2@gmail.com"}]
预期输出:
second::::[{"name":"<strong>joh</strong>n","id":2,"personalEmail":"john@gmail.com","workEmail":"wa2@gmail.com"},{"name":"<strong>joh</strong>n","id":2,"personalEmail":"wa2@gmail.com","workEmail":"wa2@gmail.com"}]