Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用forEach和map进行迭代时,数组值会发生变化_Javascript_Ecmascript 6 - Fatal编程技术网

Javascript 使用forEach和map进行迭代时,数组值会发生变化

Javascript 使用forEach和map进行迭代时,数组值会发生变化,javascript,ecmascript-6,Javascript,Ecmascript 6,我正在尝试实现一些逻辑inn,我希望数组保持原样,但是当我尝试使用forEach并映射info数组中的值时,会发生变化 var info = [{"date":"today","location":["a","b","c","d","e","f","g","h"]},{"

我正在尝试实现一些逻辑inn,我希望数组保持原样,但是当我尝试使用forEach并映射info数组中的值时,会发生变化

var info = [{"date":"today","location":["a","b","c","d","e","f","g","h"]},{"date":"yesterday","location":["a","b","c"]},{"date":"tomorrow","location":["a","b","c"]}]

var showItems = [];
var restrictItems= [];
info.forEach(res =>{
                if(res.location.length && restrictItems.length<5)
                {
                    let slicedLocation = res.location.slice(0,5-restrictItems.length);
                    slicedLocation.map((item, i) => {
                        
                            restrictItems.push(item);
                        
                    });
                    res.location = slicedLocation;
                    showItems.push(res);
                }
            })

console.log("showItemsshowItems",showItems);// here i am getting the output as [{"date":"today","location":["a","b","c","d","e"]}] which is expected
console.log("info",info)// here i am not getting the original value of info array(f,g,h) is missing in First array.
[{"date":"today","location":["a","b","c","d","e"]},{"date":"yesterday","location":["a","b","c"]},{"date":"tomorrow","location":["a","b","c"]}]
var info=[{“日期”:“今天”,“地点”:[“a”,“b”,“c”,“d”,“e”,“f”,“g”,“h”]},{“日期”:“昨天”,“地点”:[“a”,“b”,“c”]},{“日期”:“明天”,“地点”:[“a”,“b”,“c”]}]
var showItems=[];
var限制项=[];
info.forEach(res=>{
if(res.location.length&&restrictItems.length{
限制项。推(项);
});
res.location=切片位置;
showItems.push(res);
}
})
console.log(“showItemsshowItems”,showItems);//这里我得到的输出是[{“date”:“today”,“location”:[“a”,“b”,“c”,“d”,“e”]}],这是预期的结果
console.log(“info”,info)//这里我没有得到第一个数组中缺少info数组(f,g,h)的原始值。
[{“日期”:“今天”,“地点”:[“a”,“b”,“c”,“d”,“e”]},{“日期”:“昨天”,“地点”:[“a”,“b”,“c”]},{“日期”:“明天”,“地点”:[“a”,“b”,“c”]}]

请在此提供帮助,提前感谢。

您应该克隆
info
变量

var cloned = JSON.parse(JSON.stringify(info))
并使用
克隆的
变量保留
信息
var info=[{“日期”:“今天”,“地点”:[“a”,“b”,“c”,“d”,“e”,“f”,“g”,“h”]},{“日期”:“昨天”,“地点”:[“a”,“b”,“c”]},{“日期”:“明天”,“地点”:[“a”,“b”,“c”]}]
var cloned=JSON.parse(JSON.stringify(info))
var showItems=[];
var限制项=[];
克隆的。forEach(res=>{
if(res.location.length&&restrictItems.length{
限制项。推(项);
});
res.location=切片位置;
showItems.push(res);
}
})
日志(“showItemsshowItems”,showItems);

console.log(“info”,info)
您可以使用slice()方法克隆原始数组以防止变异:


让clonedInfo=info.slice()数组的第一项:将其切片(0,5)(表示abcde),并将其分配给“切片位置”,然后分配
res.location=切片位置。这就是为什么您的第一项“位置”只包含abcde