gpt4 book ai didi

javascript - 我没有使用的阵列如何更新?

转载 作者:行者123 更新时间:2023-12-03 05:44:53 27 4
gpt4 key购买 nike

这是我的功能:

function RemoveOutputKeys(array){
var temp = array;
for(var object in temp){
delete temp[object]['statusCode']
delete temp[object]['statusResponse']
}
console.log(array)
if(temp == array)
console.log("how is this possible?!?!!?!?!")
return temp
}

这是我提供的输入,

array = [{'statusCode':400},{'statusCode':200}]

temp 更新是有意义的,但我不希望 array 更新。我该如何解决这个问题?

谢谢

最佳答案

使用Array.prototype.filter()代替for in

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

function RemoveOutputKeys(array) {
return array.filter(function(myArray) {
if (!myArray['statusCode'] && !myArray['statusResponse']) {
return myArray;
}
});
}

var originalArray = [{'statusCode':400}, {'statusCode':200}, {'test': 'test'}];

var tempArray = RemoveOutputKeys(originalArray);

console.log(originalArray, tempArray);

https://jsfiddle.net/3kbypvcs/2/

关于javascript - 我没有使用的阵列如何更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40369769/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com