我正在尝试将同一个对象推送到一个数组中,并对对象进行一些修改。但是当我进行修改并推送对象时,数组中的第一个对象将被覆盖。
var details = [{"name":"john","id":2,"personalEmail":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a505552547a5d575b535614595557" rel="noreferrer noopener nofollow">[email protected]</a>","workEmail":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e495f0c7e59535f5752105d5153" rel="noreferrer noopener nofollow">[email protected]</a>"}]; 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));
}
}
}
这是 fiddle http://jsfiddle.net/LQg7W/2311/ 。 我们可以在控制台(F12 控制台选项卡)中看到输出。
输出获取:
second::::[{"name":"<strong>joh</strong>n","id":2,"personalEmail":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1166702351767c70787d3f727e7c" rel="noreferrer noopener nofollow">[email protected]</a>","workEmail":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec9b8ddeac8b818d8580c28f8381" rel="noreferrer noopener nofollow">[email protected]</a>"},{"name":"<strong>joh</strong>n","id":2,"personalEmail":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2156401361464c40484d0f424e4c" rel="noreferrer noopener nofollow">[email protected]</a>","workEmail":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c1b6a0f381a6aca0a8adefa2aeac" rel="noreferrer noopener nofollow">[email protected]</a>"}]
预期输出:
second::::[{"name":"<strong>joh</strong>n","id":2,"personalEmail":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8de7e2e5e3cdeae0ece4e1a3eee2e0" rel="noreferrer noopener nofollow">[email protected]</a>","workEmail":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98eff9aad8fff5f9f1f4b6fbf7f5" rel="noreferrer noopener nofollow">[email protected]</a>"},{"name":"<strong>joh</strong>n","id":2,"personalEmail":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1c6d083f1d6dcd0d8dd9fd2dedc" rel="noreferrer noopener nofollow">[email protected]</a>","workEmail":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ef9efbccee9e3efe7e2a0ede1e3" rel="noreferrer noopener nofollow">[email protected]</a>"}]
最佳答案
Javascript 使用对象的引用。因此,在数组中,您将添加对第一个对象的引用,然后添加对同一对象的第二个引用。基本上,只有一个对象和两个引用(有点像文件系统上的单个文件,有两个快捷方式),因此您所做的任何更改都会出现在该对象的所有引用中。
您需要复制对象,然后编辑副本。看看这个问题:How do I correctly clone a JavaScript object?
...或者只使用像 Underscore 这样的库来完成复制。 (编辑:Underscore 实际上并不具有对深度复制的 native 支持,但是 lodash does - 而且您可能应该使用它。)
关于javascript - 在javascript中如何将相同的对象两次插入数组并进行一些修改而不覆盖数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19969738/