-2

Hi i have the data like below,

data = [
  { 
    attributes: [{key: '', value: ''}, {key: 'name1', value: ''}],
    info: '',
    meshes: [],
  }
  {
   attributes: [{key: '', value: ''}, {key: '', value: ''}],
   info: '',
   meshes: [],
  }
   .....so on.... 
  ]

So from above data i want to check if each attributes has key and value empty or undefined. how can i check that. could someone help me with this. thanks.

niina
  • 119
  • 2
  • 15

3 Answers3

1

Here's similar SO post.

Try the following codes below:

function isEmptyObject(o) {
    return Object.keys(o).every(function(x) {
        return o[x]===''||o[x]===null;  // or just "return o[x];" for falsy values
    });
}

or

const isEmpty = Object.values(object).every(x => (x === null || x === ''));
Jessica Rodriguez
  • 2,899
  • 1
  • 12
  • 27
0

Firstly, iterate over data, then, check length of attribute array

data.forEach(attr => {
  if(attr.length > 0){
    // attr contains key and value
  }
})
Engineer
  • 1,243
  • 11
  • 18
0
for (let x in data) {
  for (let y in data[x].attributes) {
    if ((key === '') || (value === '')) {
      return ('data:' + x + 'attribute:' + y + 'missing key or value');
    }
  }
}

Untested. Loops through all data and attributes to test for empty key/value. You can add an extra else if to check for value and key separately.

Joshiepillow
  • 227
  • 1
  • 12