0
const obj ={Rating:7.5 , Actor:[], Age:undefined}

want to remove Actor:[]

function clean(obj) {
  for (var propName in obj) {
    if (obj[propName] === undefined || obj[propName] == [] ) {
      delete obj[propName];
    }
  }
  return obj
}

this function is only removing undefined values

1 Answers1

0

Two different javascript objects cannot be equal to each other.

Below is a demo of that:

console.log([] === [])
console.log([] == [])

In your specific case you will have to check two things:

  1. The value is an array.
  2. The array is actually empty.

If both are true, then your expression should evaluate to true.

let arr = [];
let arr2 = [1];

if(Array.isArray(arr) && arr.length === 0){
console.log('isempty');
}

if(Array.isArray(arr2) && arr2.length === 0){
console.log('isempty');
}
Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39