如何在不更改“list”变量的情况下执行以下操作?据我所知,文档(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)中的filter方法应该返回一个New变量,但是下面对我说它只是一个引用,因为“list”变量发生了变化? 有人可以解释这种行为以及如何解决它(不想更改原始的“列表”var)?
var list = [{topic : "fussball", arr : [1,2]}, {topic : "soccer", arr : [3,4]},
{topic : "fussball", arr : [5,6]},{topic : "soccer", arr : [7,8]}];
function someFilterMethod(list, topic){
let tops = list.filter( function(obj){
if(obj.topic == topic){
return obj;}
});
for(let i = 0; i < tops.length ; i++){
if(i < (tops.length)) {
for(let j = (i+1) ; j<tops.length ; j++){
if( tops[i].topic.trim() == tops[j].topic.trim() ){
tops[i].arr.push.apply(tops[i].arr, tops[j].arr);
tops.splice(j, 1);
}
}
}
}
return tops;
}
var a = someFilterMethod(list, "fussball");
console.log(a);
//> Array [Object { topic: "fussball", arr: Array [1, 2, 5, 6] }]
console.log(list);
//> Array [Object { topic: "fussball", arr: Array [1, 2, 5, 6] }, Object { topic: "soccer", arr: Array [3, 4] }, Object { topic: "fussball", arr: Array [5, 6] }, Object { topic: "soccer", arr: Array [7, 8] }, Object { topic: "hockey", arr: Array [9, 10] }, Object { topic: "golf", arr: Array [11, 12] }, Object { topic: "hockey", arr: Array [13, 14] }, Object { topic: "golf", arr: Array [15, 16] }]
someFilterMethod只接受相同“主题”的元素,连接它们的数组并返回它。
答案 0 :(得分:2)
你需要解析和格式化json,否则它将修改现有的对象
var list = [{topic : "fussball", arr : [1,2]}, {topic : "soccer", arr : [3,4]},
{topic : "fussball", arr : [5,6]},{topic : "soccer", arr : [7,8]}];
function someFilterMethod(list, topic){
let tops = JSON.parse(JSON.stringify(list)).filter( function(obj){
if(obj.topic == topic){
return obj;}
});
for(let i = 0; i < tops.length ; i++){
if(i < (tops.length)) {
for(let j = (i+1) ; j<tops.length ; j++){
if( tops[i].topic.trim() == tops[j].topic.trim() ){
tops[i].arr.push.apply(tops[i].arr, tops[j].arr);
tops.splice(j, 1);
}
}
}
}
return tops;
}
var a = someFilterMethod(list, "fussball");
console.log(a);
//> Array [Object { topic: "fussball", arr: Array [1, 2, 5, 6] }]
console.log(list);
//> Array [Object { topic: "fussball", arr: Array [1, 2, 5, 6] }, Object { topic: "soccer", arr: Array [3, 4] }, Object { topic: "fussball", arr: Array [5, 6] }, Object { topic: "soccer", arr: Array [7, 8] }, Object { topic: "hockey", arr: Array [9, 10] }, Object { topic: "golf", arr: Array [11, 12] }, Object { topic: "hockey", arr: Array [13, 14] }, Object { topic: "golf", arr: Array [15, 16] }]
答案 1 :(得分:1)
使用Array.filters,会创建Array的新引用,但是,对象继续共享相同的引用。因此,过滤后的数组对象的任何变化都意味着原始数组的变化。
您可以尝试以下
var list = [{topic : "fussball", arr : [1,2]}, {topic : "soccer", arr : [3,4]}, {topic : "fussball", arr : [5,6]},{topic : "soccer", arr : [7,8]}];
function someFilterMethod(list, topic){
return list.reduce((a,c) => {
if(c.topic == topic) a.arr = [...a.arr, ...c.arr];
return a;
}, {topic, arr: []});
}
var a = someFilterMethod(list, "fussball");
console.log(a);
console.log(list);
&#13;