How to check object empty?

sujon :

I have got object of array. I am trying to check empty.

  const data = {
      test:[],
      test2:[],
      test1:["can"]
    }

Here is my trying code:

const dataObj = Object.values(data)

console.log(dataObj)

my output would be :

      const data = {
          test1:["can"]
        }
Pranav C Balan :

You can use Object.entries and Array#reduce methods.

const data = {
  test: [],
  test2: [],
  test1: ["can"]
}

const res = Object.entries(data).reduce((obj, [k, v]) => {
  if (v && v.length) obj[k] = v;
  return obj;
}, {})

console.log(res)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=382962&siteId=1