I had an object with an array of elements:
const item= [{name : a, color : white, status : offline},
{name : b, color : red, status : offline},
{name : c, color : yellow, status : online},
{name : e, color : green, status : online}]
I need to create a new object with only the 'name' properties of the elements who has 'status:online' properties.
Here's my code:
let newItem={};
let objLen=Object.keys(item).length;
for(const i=0;i<=objLen;i++){
if([item.status][i] === 'online' && [item.status][i] != 'offline'){
newItem[i]=[item.name][i]
}
}
console.log(newItem);
The problem is that 'newItem' returns this:
newItem= [{},
{},
{name : c},
{name : e}]
How can I return the object without the empty elements? Like this:
newItem= [{name : c},
{name : e}]