0

If i have a JSON object as below: jsonObj = {key1 :"", key2: "", key3:""}

Is it possible to know that all values are empty using a single function or property of JSON? I tried jsonObj.length == 0 & also !Object.values(jsonObj).length but that doesnt work.

2

4 Answers 4

2

There is no such function but you can use Object.values and every. Object.values will create an array consisting of object values & every will check if all the value in that array pass the test condition

let jsonObj = {
  key1: "",
  key2: "",
  key3: ""
};

let val = Object.values(jsonObj).every((item) => {
  return item === "";
})
console.log(val)

Alternatively you can create a function and use for..in to iterate the object. If the value of the key is not "" then return false , else return true

function validateObj(obj) {
  for (let keys in obj) {
    if (obj[keys] !== "") {
      return false;
    }
  }
  return true;
}

let jsonObj = {
  key1: "",
  key2: "",
  key3: ""
};

let jsonObj2 = {
  key1: "123",
  key2: "",
  key3: ""
};

console.log(validateObj(jsonObj));
console.log(validateObj(jsonObj2));

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

Comments

0

If you want to test if the property of every value in a JavaScript object is an empty string, then you need to loop over them and test them.

The simplest way to do that is to convert to an array of values and then filter them.

const object = {
  key1: "",
  key2: "",
  key3: ""
};

const array_of_values = Object.values(object);

const array_of_not_empty_strings = array_of_values.filter( value => value !== "" );

if (array_of_not_empty_strings.length) {
    console.log(array_of_not_empty_strings.length + " values were not empty strings");
} else {
    console.log("Every value was an empty string");
}

Or more concisely:

const object = {
  key1: "",
  key2: "",
  key3: ""
};

console.log(!!Object.values(object).filter( v => v !== "" ).length);

Comments

0

You could use Object.keys() and check for .some() to exit as soon as possible from the loop

const jsonObj = {key1 :"", key2: "", key3:""};
const allValsEmpty = !Object.keys(jsonObj).some(k => jsonObj[k] !== "");

console.log(allValsEmpty) // true

basically, as soon the loop encounters a value that is not empty - will stop the iteration.

Object.keys() - MDN
Array.prototype.some() - MDN

2 Comments

Those double negatives are really hard to reason about — it's not the case that some values are not... every() is a better choice.
@MarkMeyer I must agree, eventually, but since we already have answers providing the .every() scenario (that iterates the entire array) - here's just a hard to reason example with an early exit - by using .some(). If some has value... therefore not all are empty. Am I mistaken something?
-1

Well there is no direct way to check , But you can check like this :

jsonObj = {key1 :"", key2: "", key3:""}
Object.values(jsonObj).every(el=>el.length === 0)

3 Comments

@poojau I think brk provided the same answer minutes before. One-liner-or-not is just the way to write implicit returns in JavaScript arrow functions.
the solution will return true for empty arrays [] also, if it's expected Object.values(jsonObj).every(el => !el.length) more compact
I just showed what he wanted to know , if you will see his term above he said he tried like jsonObj.length == 0 , thats why i showed with that only , its not about one liner or more obiously it can be written in many ways .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.