首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript深度嵌套数组v的浅层数组拼接

JavaScript深度嵌套数组v的浅层数组拼接
EN

Stack Overflow用户
提问于 2015-12-02 20:39:20
回答 2查看 102关注 0票数 0

我有一个对象数组,而对象数组又包含对象数组。我希望同时过滤“顶层”(浅)数组和“嵌套”数组。为了维护一个确定的数组,我遍历了remoteData,并将每个对象都推入了definitiveArray和filteredArr中。我的想法是我只操纵filteredArray。

如果我使用filteredArray.spice(0,1)遍历并拼接出filteredArray的浅数组中的一项,它只会影响filteredArray,即我试图操作的数组。但是,如果我尝试使用filteredArray.Colours.splice(0,1)从filteredArray.nested数组中拼接出一个项,它将从filteredArray和definitiveArray中删除该项。

我猜这是由于我对JavaScript的引用是如何工作的误解所致。如能就如何克服这一问题提供解释和指导,将不胜感激。

这里提供了plnkr http://plnkr.co/edit/MKSlTogO3YkzHuKnHmwH

代码语言:javascript
复制
remoteData = [
{ Id: 1, Text: 'Item 1', Colours: [ { Id: 1, Colour: 'Red' }, { Id: 2, Colour: 'Orange' } ] },  
{ Id: 2, Text: 'Item 2', Colours: [ { Id: 1, Colour: 'Yellow' }, { Id: 2, Colour: 'Green' } ] },
{ Id: 3, Text: 'Item 3', Colours: [ { Id: 1, Colour: 'Blue' }, { Id: 2, Colour: 'Indigo' } ] },
{ Id: 4, Text: 'Item 4', Colours: [ { Id: 1, Colour: 'Violet' }, { Id: 2, Colour: 'Red' } ] },
{ Id: 5, Text: 'Item 5', Colours: [ { Id: 1, Colour: 'Orange' }, { Id: 2, Colour: 'Yellow' } ] },],
definitiveArray = [],
filteredArray = []; 

/*
* splicing an item out of the deeply nested array affects both
*/
len = remoteData.length;
for (var i = 0; i < len; i++) {
    //push data into both the definitive array and the filter array
    definitiveArray.push(remoteData[i]);
    filteredArray.push(remoteData[i]);
}
console.log(filteredArray[0].Colours.length);   //obviously 2
console.log(definitiveArray[0].Colours.length); //obviously 2

//splice the first item in the first Colours array for ONLY the filteredArray
filteredArray[0].Colours.splice(0, 1);
console.log(filteredArray[0].Colours.length);   //obviously 1
console.log(definitiveArray[0].Colours.length); //also 1 rather than 2 that I expected


/*
* If however I splice an item out of the shallow array, it affects only the filteredArray
*/
console.log(filteredArray.length);   //obviously 5
console.log(definitiveArray.length); //obviously 5

//remove the first item in ONLY the filteredArray
filteredArray.splice(0, 1);

console.log(filteredArray.length);   //obviously 4
console.log(definitiveArray.length); //still 5 as this array has been left unaffected

这里

EN

回答 2

Stack Overflow用户

发布于 2015-12-02 20:44:16

您需要为每个嵌套数组创建一个新实例。

这里有一个简单的方法-

代码语言:javascript
复制
for (var i = 0; i < len; i++) {
    //push data into both the definitive array and the filter array
    definitiveArray.push(JSON.parse(JSON.stringify(remoteData[i])));
    filteredArray.push(JSON.parse(JSON.stringify(remoteData[i])));
}
票数 1
EN

Stack Overflow用户

发布于 2015-12-02 20:54:30

我认为如果在数组拼接或切片中有object,可以使用JSON.parse(JSON.stringify(remoteData))

代码语言:javascript
复制
remoteData = [
{ Id: 1, Text: 'Item 1', Colours: [ { Id: 1, Colour: 'Red' }, { Id: 2, Colour: 'Orange' } ] },  
{ Id: 2, Text: 'Item 2', Colours: [ { Id: 1, Colour: 'Yellow' }, { Id: 2, Colour: 'Green' } ] },
{ Id: 3, Text: 'Item 3', Colours: [ { Id: 1, Colour: 'Blue' }, { Id: 2, Colour: 'Indigo' } ] },
{ Id: 4, Text: 'Item 4', Colours: [ { Id: 1, Colour: 'Violet' }, { Id: 2, Colour: 'Red' } ] },
{ Id: 5, Text: 'Item 5', Colours: [ { Id: 1, Colour: 'Orange' }, { Id: 2, Colour: 'Yellow' } ] },],
definitiveArray = [],
filteredArray = []; 

/*
* splicing an item out of the deeply nested array affects both
*/
len = remoteData.length;
for (var i = 0; i < len; i++) {
    //push data into both the definitive array and the filter array
    definitiveArray.push(remoteData[i]);
   
}
filteredArray=JSON.parse(JSON.stringify(definitiveArray))
console.log(filteredArray[0].Colours.length);   //obviously 2
console.log(definitiveArray[0].Colours.length); //obviously 2

//splice the first item in the first Colours array for ONLY the filteredArray
filteredArray[0].Colours.splice(0, 1);
alert(filteredArray[0].Colours.length);   //obviously 1
alert(definitiveArray[0].Colours.length); //also 1 rather than 2 that I expected


/*
* If however I splice an item out of the shallow array, it affects only the filteredArray
*/
console.log(filteredArray.length);   //obviously 5
console.log(definitiveArray.length); //obviously 5

//remove the first item in ONLY the filteredArray
filteredArray.splice(0, 1);

console.log(filteredArray.length);   //obviously 4
console.log(definitiveArray.length); //still 5 as this array has been left unaffected
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34042988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档