假設我想獲取此物件串列中不為 null 的 key 的第一個值:
const arr = [
{
"key": null,
"anotherkey": 0
},
{
"another": "ignore"
},
{
"bool": True,
"key": "this!"
}
]
是否有一些單線使用 Ramda 來做到這一點?我是用 f??or 回圈做的。
uj5u.com熱心網友回復:
與拉姆達:
R.find(R.prop("key"))(arr);
prop函式將回傳key每個元素的值。將從這些元素中find回傳第一個truthy元素。
uj5u.com熱心網友回復:
您可以使用Array.find查找陣列中key屬性為truthy的第一項,然后獲取該key屬性。
const arr = [{
"key": null,
"anotherkey": 0
},
{
"another": "ignore"
},
{
"bool": true,
"key": "this!"
}
]
const res = arr.find(e => e.key).key;
console.log(res)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/366122.html
上一篇:如何在具有相同onclick功能的不同HTML元素之間切換?
下一篇:BMI計算器JavaScript
