如何检查对象数组中的项目是否为空或不使用每一项? [英] How to check items in an array of objects is empty or not using every?

查看:34
本文介绍了如何检查对象数组中的项目是否为空或不使用每一项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象数组,如果价格为空,我想发送一条消息之前要发送一条消息.

I have an array of objects and before I want to send a request I want to appear a message if the price is empty.

所以我使用了 .every ,但是它总是返回False,总是"empty or not"

So I used .every but it's return false always "empty or not"

样本数据

const services = [{
    "id": 12,
    "price": "10"
  },
  {
    "id": 12,
    "price": ""
  },
  {
    "id": 12,
    "price": "99"
  },
]

const isEmpty = (value) => value === undefined || value === null || value === '';

const doEmptyAction = services.every((item) => isEmpty(item.price));

console.log(doEmptyAction); // always false

推荐答案

every()不会对没有值的数组元素执行该函数.使用some():

every() does not execute the function for array elements without values. Use some():

const services = [{
    "id": 12,
    "price": "10"
  },
  {
    "id": 12,
    "price": ""
  },
  {
    "id": 12,
    "price": "99"
  },
]

const isEmpty = (value) => value === undefined || value === null || value === '';

const doEmptyAction = services.some((item) => isEmpty(item.price));

console.log(doEmptyAction);

这篇关于如何检查对象数组中的项目是否为空或不使用每一项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆