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

标签 javascript arrays

这是我的功能:

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/

相关文章:

javascript - Selectize.js:使用默认值填充

c++ - 如何在可变大小的类中创建指针数组?

javascript - 乘以嵌套数组长度并返回单个值

c# - 重新排列数组中的数据库查询结果以查找重复行

c++ - 将名称添加到 C++ 中的数组位置

javascript - 获取所有可能集合的算法

php - 使用 AJAX 返回表中的所有记录

javascript - 三元运算符检查多个字符串

javascript - 根据javascript中的顺序号将对象推送到数组

javascript - jquery ui datepicker range clear value 基于另一个选择的值