【问题标题】:How to remove the undefined/null from array inside the array in angular如何以角度从数组中的数组中删除未定义/空值
【发布时间】:2020-09-14 08:18:03
【问题描述】:
DATA = [{
    application: [{
        name: 'Room1'
    },{
        name: 'Room2'
    },{
        name: 'Room3'
    },{
        name: 'Room4'
    },{
        name: 'Room5'
    }, , undefined, null, null],
    name: 'Batch 1',
    date: '2020-10-20'
}]

我在这里要做的是从数组中删除undefined/null。 undefined/null 应该被移除。

输出应该是这样的:

 [{
    application: [{
        name: 'Room1'
    },{
        name: 'Room2'
    },{
        name: 'Room3'
    },{
        name: 'Room4'
    },{
        name: 'Room5'
    }],
    name: 'Batch 1',
    date: '2020-10-20'
}]

它将删除 null 或 undefined

【问题讨论】:

标签: javascript typescript


【解决方案1】:

您可以使用Array.prototype.filter 方法仅保留!!item(即非空、非未定义、非空、非假和非零项)。

let DATA = [{
  application: [{
    name: 'Room1'
  }, {
    name: 'Room2'
  }, {
    name: 'Room3'
  }, {
    name: 'Room4'
  }, {
    name: 'Room5'
  }, , undefined, null, null],
  name: 'Batch 1',
  date: '2020-10-20'
}];
DATA[0].application = DATA[0].application.filter(item => !!item);
console.log(DATA);

【讨论】:

    【解决方案2】:

    DATA = [{
        application: [{
            name: 'Room1'
        },{
            name: 'Room2'
        },{
            name: 'Room3'
        },{
            name: 'Room4'
        },{
            name: 'Room5'
        }, , undefined, null, null],
        name: 'Batch 1',
        date: '2020-10-20'
    }]
    
    DATA[0].application = DATA[0].application.filter(i=> i);
    console.log(DATA)

    【讨论】:

      猜你喜欢
      • 2015-04-20
      • 2019-09-05
      • 2019-09-11
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 2020-01-01
      • 2021-04-10
      相关资源
      最近更新 更多