0

I want to check the following array have any empty value or not

Example

The following should return true

[
    {
        "price": "12",
        "number": "1"
    },
    {
        "price": "12",
        "number": "1"
    }
]

The following should return false

[
    {
        "price": "12",
        "number": "1"
    },
    {
        "price": "12",
        "number": ""
    }
]
2
  • No. Because it has both keys and values, but the other doesn't have one value. Commented Sep 3, 2021 at 16:19
  • Shouldn't the first example be false, and the second example be true, if the question is "Does it have an empty value or not"? Commented Sep 3, 2021 at 16:50

2 Answers 2

1

You can use Array.some and Object.values to check whether one of the property value's length of an item in the array is smaller than 1:

const arr=[{price:"12",number:"1"},{price:"12",number:"1"}];

const isValid = !arr.some(e => Object.values(e).some(f => f.length < 1))

console.log(isValid)

Sign up to request clarification or add additional context in comments.

5 Comments

Why f.length < 1 instead of f.length == 0 ?
Just a side note: a problem might arise if the number attribute can contain an actual number, or be null or undefined rather than an empty string; in that case f.length would throw an error. If values other than strings should be taken into account, it might be worth replacing f => f.length < 1 with f => ['', null, undefined].includes(f)
@BenkI + at secan ... For exactly such edge cases it was always better if the OP would specify the exact requirements. Otherwise the OP gets A's which exactly match/cover what was ask for. Also, how are empty items ... { } ... (no properties no values) supposed to be handled?
@PeterSeliger just to be clear, I was not implying the answer given by Spectric is wrong or incomplete; I just thought it could be worth mentioning the issue in case the OP did not take that eventuality into consideration.
@secan ... Me neither, that's the reason for having primarily addressed the OP in my above comment.
1

function isNonEmptyValue(value) {
  return (
    (value != null)
    && (value !== '') 
  //&& any other *empty* value constraint
  );
}
function hasEntriesAndNoEmptyValues(item) {
  const values = Object.values(item);
  return (
    values.length >= 1 &&
    values.every(value => isNonEmptyValue(value))
  );
}

console.log(
  [{
    "price": "12",
    "number": "1",
  }, {
    "price": "12",
    "number": "",

  }].every(hasEntriesAndNoEmptyValues)
);
console.log(
  [{
    "price": "12",
    "number": "1",
  }, {
    "price": "12",
    "number": " ",

  }].every(hasEntriesAndNoEmptyValues)
);
console.log(
  [{

  }, {
    "price": "12",
    "number": " ",

  }].every(hasEntriesAndNoEmptyValues)
);

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.