检查是否有任何特定的键在对象的JavaScript数组中有一个值 [英] Check if any of the specific key has a value in JavaScript array of object

查看:166
本文介绍了检查是否有任何特定的键在对象的JavaScript数组中有一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查是否有任何特定的键在对象的JavaScript数组中有一个值。

I want to Check if any of the specific key has a value in JavaScript array of object.

myArray = [ {file:null}, {file:hello.jpg}, {file:null}] ;

密钥文件具有值所以返回 true 否则 false
如何以编程方式检查?

The key file has value so return true otherwise false. How to check this programmatically?

推荐答案

由于 null 是一个错误的值,您可以使用双重否定检查它是否包含一个值或它是空的( null )。

Since null is a falsy value you could use double negation to check if it contains a value or it's empty (null).

let myArray = [ {file:null}, {file:'hello.jpg'}, {file:null}];

const check = arr => arr.map(v => !!v.file);

console.log(check(myArray));

这篇关于检查是否有任何特定的键在对象的JavaScript数组中有一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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