lodash在数组元素上检查对象属性
我想使用lodash在特定数组元素上找到对象的属性。
可以说我正在击中API并获得响应,将其设置为React状态“ PersonsDetails”(其初始值为null),所以现在现在
let personsDetails=[{name:'king',id:2},{name:'queen',id:3}] //sometimes i get 1 object and sometimes 2
我想访问第二个对象的“名称”属性,我
personsDetails && personsDetails[1] && personsDetails[1].name
这样做了通过使用lodash访问它的任何简短语法?如果值不存在,
则据我所知,我需要null null _get属性仅适用于对象
I want to use lodash to find a property of an object at a specific array element.
Lets say I am hitting an api and getting response which sets it to the react state "personsDetails" (whose initial value was null), so now it becomes
let personsDetails=[{name:'king',id:2},{name:'queen',id:3}] //sometimes i get 1 object and sometimes 2
now when i want to access "name" property of 2nd object i do
personsDetails && personsDetails[1] && personsDetails[1].name
So is there any shorted syntax of accessing it via using lodash ? If the values doesnt exist i need null
As far as i know _get property works with object only so here i am dealing with array and then object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您仍然可以使用
获取
并将默认值设置为null
或可以将
nth
与一起使用
,并将默认值设置为null
You can still use
get
and set the default asnull
or can use
nth
along withget
and set the default asnull