从对象数组

时间:2018-06-05 09:18:11

标签: javascript

我有一个像这样的对象数组。结果显示在console.log()

(5) [{…}, {…}, {…}, {…}, {…}]
0:
  name:"SOMEVALUE"
  partyDetails"SOMEVALUE"
  __proto__:Object
1:
  name: "SOMEVALUE"
  partyDetails: "SOMEVALUE"
  __proto__:Object
2:
  name:"SOMEVALUE"
  partyDetails: "SOMEVALUE"
  __proto__: Object
3:
  name:"SOMEVALUE"
  partyDetails:"SOMEVALUE"
  __proto__: Object
4:
  name:"SOMEVALUE"
  partyDetails: "SOMEVALUE"
  __proto__:Object
length:5
__proto__:Array(0)

通过此命令从对象中删除后,

delete  $scope.excelInfo.columnDefs[3];
delete  $scope.excelInfo.columnDefs[4];

我在console.log上得到了这个

(5) [{…}, {…}, {…}, empty × 2]
 0:
   name:"SOMEVALUE"
   partyDetails:"SOMEVALUE"
   __proto__:Object
 1:
   name:"SOMEVALUE"
   partyDetails:"SOMEVALUE"
   __proto__:Object
 2:
   name:"SOMEVALUE"
   partyDetails:"SOMEVALUE"
   __proto__:Object
 length:5
 __proto__:Array(0) 

如何看到删除后最后一个对象数组中有空×2

如何删除这2个空行?

这2个空行在视图中使我的数据错误。

有什么好的解决方案吗?

我不想检查数据是否等于null。

非常感谢

3 个答案:

答案 0 :(得分:2)

您可以使用splice()删除最后两个对象而不设置undefined,就像使用delete

时一样



var arr = [
  {
    name:"SOMEVALUE1"
  },
  {
    name:"SOMEVALUE2"
  },  
  {
    name:"SOMEVALUE3"
  },  
  {
    name:"SOMEVALUE4"
  },  
  {
    name:"SOMEVALUE5"
  }
];

arr.splice(3,2)
console.log(arr);




或者使用delete,您还需要仅过滤定义的对象,



var arr = [
  {
    name:"SOMEVALUE1"
  },
  {
    name:"SOMEVALUE2"
  },  
  {
    name:"SOMEVALUE3"
  },  
  {
    name:"SOMEVALUE4"
  },  
  {
    name:"SOMEVALUE5"
  }
];

delete arr[3];
delete arr[4];
var res = arr.filter(item => item);
console.log(res);




答案 1 :(得分:1)

使用Array.splice

LoginWithAmazon

e.g。



provider name




答案 2 :(得分:1)

Array.prototype.splice()数组而不是delete

$scope.excelInfo.columnDefs.splice(3, 2);
//                                 ^  ^
//                                 |  |
//                                 |  Number of elements to remove
//                                 Index at which to start removing

splice()修改了数组,因此无需重新分配。