简体   繁体   English

如果对象包含真键,则返回布尔真值,不返回值

[英]Return boolean true value if object contains true key, not return the value

I'm really stuck on how to return a simple true/false IF my object contains a key with a true value.如果我的对象包含一个具有真值的键,我真的很困惑如何返回一个简单的真/假。 I do not want to return the key or value itself, just an assertion that it does contain a true value.不想返回键或值本身,只是一个说法,它包含一个真正的价值。

Eg例如

var fruits = { apples: false, oranges: true, bananas: true }

There's a true value in this object.这个对象有一个真正的价值。 I don't care which one is true... I just want to be able to return true because there is a true value.我不在乎哪个是真的……我只想能够返回true因为有一个真值。

My current solution returns ["oranges", "bananas"] not true我目前的解决方案返回["oranges", "bananas"]true

Object.keys(fruits).filter(function(key) {
    return !!fruits[key]
})

You can use Object.values (keys aren't important here) to produce an array of the object's values to call Array#includes on:您可以使用Object.values (此处的键不重要)生成对象值的数组以调用Array#includes

 const fruits = {apples: false, oranges: true, bananas: true}; console.log(Object.values(fruits).includes(true)); // test the sad path console.log(Object.values({foo: false, bar: 42}).includes(true));

If Object.keys is permitted but Object.values and includes aren't, you can use something like Array#reduce :如果允许Object.keys但不允许Object.valuesincludes ,您可以使用类似Array#reduce东西:

 var fruits = {apples: false, oranges: true, bananas: true}; console.log(Object.keys(fruits).reduce((a, e) => a || fruits[e] === true, false));

If you don't have access to anything (or don't like that the reduce approach above doesn't short-circuit), you can always write a function to iterate through the keys to find a particular target value (to keep the function reusable for other targets than true ):如果你无权访问任何东西(或者不喜欢上面的reduce方法不会短路),你总是可以编写一个函数来遍历键以找到特定的目标值(以保持函数可重用于除true之外的其他目标):

 function containsValue(obj, target) { for (var key in obj) { if (obj[key] === target) { return true; } } return false; } var fruits = {apples: false, oranges: true, bananas: true}; console.log(containsValue(fruits, true));

Try with Array.prototype.some() :尝试使用Array.prototype.some()

The some() method tests whether at least one element in the array passes the test implemented by the provided function. some()方法测试数组中是否至少有一个元素通过了由提供的函数实现的测试。

 var fruits = { apples: false, oranges: true } var r = Object.keys(fruits).some(function(key) { return !!fruits[key] }) console.log(r);

Though, instead of Object.keys() , it is better to use Object.values() to iterate the objects value directly:虽然,而不是Object.keys() ,最好使用Object.values()直接迭代对象

The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well). Object.values()方法返回给定对象自己的可枚举属性值的数组,其顺序与for...in循环提供的顺序相同(区别在于 for-in 循环枚举原型链中的属性同样)。

 var fruits = { apples: false, oranges: true } var r = Object.values(fruits).some(f => f) console.log(r);

正如您想知道是否有任何值是true

Object.values(fruits).includes(true)

You could check with Boolean as callback for Array#some .您可以检查Boolean作为Array#some回调。

 const has = o => Object.values(o).some(Boolean); var a = {}, b = { oranges: true, apples : false }, c = { oranges: false, apples : false }; [a, b, c].forEach(o => console.log(has(o)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM