1

I have an JavaScript object with keys and values. I need to validate that object's values, and if it contains value 'empty', I want to give an alert. I want to give alert only once even if there were multiple 'empty' values.

I tried like this;

data = [
  {first_name: 'John', last_name: 'Doe'},
  {first_name: 'Kiki', last_name: 'empty'},
  {first_name: 'Kim', last_name: 'empty'},
]

data.forEach(function(value){
  if(value.last_name == 'empty'){
    alert('Please fill all the field');
    return false;
  }
});

This could validate the value, but it's giving alert twice.

I want to show alert only once. How can I do this?

4 Answers 4

4

Use some:

const data = [{
    first_name: 'John',
    last_name: 'Doe'
  },
  {
    first_name: 'Kiki',
    last_name: 'empty'
  },
  {
    first_name: 'Kim',
    last_name: 'empty'
  },
];

if (data.some(e => Object.values(e).some(f => f == "empty"))) {
  alert("There is an empty value");
}

If you want to show the other data as well (like which property in which index), you could use the other parameters in the callback function:

const data = [{
    first_name: 'John',
    last_name: 'Doe'
  },
  {
    first_name: 'Kiki',
    last_name: 'empty'
  },
  {
    first_name: 'Kim',
    last_name: 'empty'
  },
];

if (data.some(e => Object.values(e).some(f => f == "empty"))) {
  let index = data.findIndex(e => Object.values(e).some(f => f == "empty"));
  let [property] = Object.entries(data[index]).find(([k, v]) => v == "empty");
  alert(`The property ${property} at index ${index} is empty.`);
}

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

1 Comment

Thank you so much! The second code improved my program a lot!
1

You can use the function some to check if at least one object's last_name === 'empty'

const data = [{first_name: 'John',last_name: 'Doe'},{first_name: 'Kiki',last_name: 'empty'  },{first_name: 'Kim',last_name: 'empty'}];

// The function some returns true 
// if at least one object meets the predicate 'last_name === "empty"'.
if (data.some(({last_name}) => last_name === "empty")) alert("There is an empty value");

Comments

0

It's giving you the alert twice, once for Kiki and once for Kim. If you want to stop the loop after the first occurrence, you need to change "return false" to "break" like this:

data.forEach(function(value){
  if(value.last_name == 'empty'){
    alert('Please fill all the field');
    break;
  }
});

1 Comment

Also, your JSON would come back with null if it were empty instead of a string that says empty like in your example. So, your example is checking for someone who has the last name 'empty' instead of someone who left it blank.
0

I think a loop can be handy in this kind of situation

let data = [
  {first_name: 'John', last_name: 'Doe'},
  {first_name: 'Kiki', last_name: 'empty'},
  {first_name: 'Kim', last_name: 'empty'},
]

// data.forEach(function(value){
//   if(value.last_name == 'empty'){
//     alert('Please fill all the field');
//     return false;
//   }
// });

for(let i of data) {
  if(i.last_name == "empty") {
    alert('Please fill all the field');
    break;
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.