Javascript 更改后,为什么concated数组上的操作会影响两个数组?

Javascript 更改后,为什么concated数组上的操作会影响两个数组?,javascript,node.js,arrays,concatenation,Javascript,Node.js,Arrays,Concatenation,我运行下面的过程,从一个对象收集所有产品属性和选项,以便更新产品 出于某种奇怪的原因,主数组类别发生了更改,我似乎不明白为什么 在以下过程之后,categoryUrls[random key].attributes.options将添加新值 const {categoryUrls} = require('./helpers/categoryUrls.js') getCategory = function (categoryUrls) { let att = [] f

我运行下面的过程,从一个对象收集所有产品属性和选项,以便更新产品

出于某种奇怪的原因,主数组类别发生了更改,我似乎不明白为什么

在以下过程之后,
categoryUrls[random key].attributes.options
将添加新值

const {categoryUrls} = require('./helpers/categoryUrls.js')

getCategory = function (categoryUrls) {

    let att = []
    
    for (const key in categoryUrls) {
        if (categoryUrls[key].attributes) {// check if has attribute 
            att = att.concat(categoryUrls[key].attributes)
            
            for(var i=0; i<att.length; ++i) {// check if attribute ID already exist and remove duplicates
                for(var j=i+1; j<att.length; ++j) {
                    if(att[i].id === att[j].id){
                        for (let k = 0; k < att[j].options.length; k++) { // if attribute ID already exist unite options
                            let exist = att[i].options.find(option => option == att[j].options[k]) // check if option no already exist 
                            if (!exist) {
                                att[i].options.push(att[j].options[k])
                            }
                        }
                        att.splice(j--, 1);
                    }
                }
            }
        }
    }
    return att
}


let data = getCategory(categoryUrls)
productData = productData.concat(data)
const{categoryUrls}=require('./helpers/categoryUrls.js')
getCategory=函数(categoryUrls){
设att=[]
用于(类别中的常量键){
if(categoryUrls[key].attributes){//检查是否有属性
att=att.concat(categoryUrls[key].属性)

对于(var i=0;i,如果我知道您试图将新数组保存到
att
,但让原始数组不发生变化,但您正在对categoryURL应用该过程。是否正确?如果是,请记住数组是引用类型,因此,如果使用它们,则更改实际数组,它们不会作为字符串或整数使用

尝试在使用数组之前复制它,并在其副本上应用该过程

试试这个:

const {categoryUrls} = require('./helpers/categoryUrls.js')

getCategory = function (categoryUrls) {

    let newArray = [...categoryUrls]

    let att = []
    
    for (const key in categoryUrls) {
        if (categoryUrls[key].attributes) {// check if has attribute 
            att = att.concat(newArray[key].attributes)
            
            for(var i=0; i<att.length; ++i) {// check if attribute ID already exist and remove duplicates
                for(var j=i+1; j<att.length; ++j) {
                    if(att[i].id === att[j].id){
                        for (let k = 0; k < att[j].options.length; k++) { // if attribute ID already exist unite options
                            let exist = att[i].options.find(option => option == att[j].options[k]) // check if option no already exist 
                            if (!exist) {
                                att[i].options.push(att[j].options[k])
                            }
                        }
                        att.splice(j--, 1);
                    }
                }
            }
        }
    }
    return att
}


let data = getCategory(categoryUrls)
productData = productData.concat(data)
const{categoryUrls}=require('./helpers/categoryUrls.js')
getCategory=函数(categoryUrls){
让newArray=[…categoryUrls]
设att=[]
用于(类别中的常量键){
if(categoryUrls[key].attributes){//检查是否有属性
att=att.concat(newArray[key].attributes)

对于(var i=0;我认为[…a..b..]与concat方法相同。我认为问题还在于数组包含对象。正如您所说,它不像字符串和数字。因此[…a..b..]也不起作用。