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

What I'm trying to do here is to remove the undefined/null from the array. the undefined/null should be remove.

output should be like this:

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

it will remove the null or undefined

Panda
  • 365
  • 9
  • 23

2 Answers2

1

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)
ABGR
  • 4,631
  • 4
  • 27
  • 49
1

You could use the Array.prototype.filter method to keep only !!item (i.e. not-null, not-undefined, not-empty, not-false and not-zero items).

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);
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34