Efficient programming: how to gracefully judge that the values of all objects in the array are not empty?

foreword

In front-end development, it is a common task to determine whether the values ​​of all objects in the array are not empty. This task may involve multiple objects and multiple properties, so it needs a concise and efficient way to handle it. This article will introduce three different methods to help you deal with this problem easily.


Method 1: Use every() method and Object.values() method

every()method

parameter describe
currentValue must. the value of the current element
index optional. the index of the current element
arr optional. the array object the current element belongs to

Object.values()method

parameter describe
obj It is the object to return the value of its own enumerable property

every()method is used to check whether each element in the array satisfies the specified condition. Object.values()method is used to get all the values ​​of an object and returns an array of values. In this example, we use every()the method to check that each object's value is not null. In the test function, we use Object.values()the method to get all the values ​​of the object, and every()the method to check that each value is not an empty string. allValuesNotEmptyThe value of the variable is if all objects' values ​​are non-null, trueotherwise false.

code show as below:

const arr = [
  {
    
     name: "xhong", age: "25" },
  {
    
     name: "xlan", age: "30" },
  {
    
     name: "xshi", age: "" },
];
const allValuesNotEmpty = arr.every((obj) => {
    
    
  return Object.values(obj).every((value) => value !== "");
});
console.log(allValuesNotEmpty); // 输出 false

console print

insert image description here


Method 2: Use every() method and Object.keys() method

Object.keys()method

parameter describe
obj the object whose enumeration's own properties are to be returned

Object.keys()method is used to get all the keys of an object and returns an array of keys. In this example, we use every()the method to check that each object's value is not null. In the test function, we use Object.keys()the method to get all the keys of the object, and every()the method to check that the value corresponding to each key is not an empty string. allValuesNotEmptyThe value of the variable is if all objects' values ​​are non-null, trueotherwise false.

code show as below:

const arr = [
  {
    
     name: "li", age: "25" },
  {
    
     name: "zhang", age: "" },
  {
    
     name: "shi", age: "35" },
];
const allValuesNotEmpty = arr.every((obj) => {
    
    
  return Object.keys(obj).every((key) => obj[key] !== "");
});
console.log(allValuesNotEmpty); // 输出 false

console print

insert image description here


Method 3: Use every() method and for...in loop

for...inA loop is used to iterate over all enumerable properties of an object. In this example, we use every()the method to check that each object's value is not null. In the test function, we use to for...inloop through all properties of the object and check whether the value corresponding to each property is not an empty string. allValuesNotEmptyThe value of the variable is if all objects' values ​​are non-null, trueotherwise false.

code show as below:

const arr = [
  {
    
     name: "j", age: "" },
  {
    
     name: "k", age: "30" },
  {
    
     name: "a", age: "35" },
];
const allValuesNotEmpty = arr.every((obj) => {
    
    
  for (let key in obj) {
    
    
    if (obj[key] === "") {
    
    
      return false;
    }
  }
  return true;
});
console.log(allValuesNotEmpty); // 输出 false

console print

insert image description here

Guess you like

Origin blog.csdn.net/Shids_/article/details/131411245