-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.

5
  • 1
    What did you try? What result do you expect? Do you want to filter data? Do you want a boolean result if any object in the array does not match your conditions? Commented May 3, 2019 at 14:02
  • Iterate over data and check the object property attributes. Commented May 3, 2019 at 14:02
  • Both the empty string and undefined are falsy Commented May 3, 2019 at 14:03
  • 1
    data.every(x => x.every(a => a.key === '' && a.value === '')) Commented May 3, 2019 at 14:07
  • this seems to be the right one thanks. Commented May 3, 2019 at 14:19

3 Answers 3

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 === ''));
Sign up to request clarification or add additional context in comments.

Comments

0

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

data.forEach(attr => {
  if(attr.length > 0){
    // attr contains key and value
  }
})

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.