这是我的功能:
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);
关于javascript - 我没有使用的阵列如何更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40369769/