Issue
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
Solution
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);
Answered By - Alessio Cantarella
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.