首页 > 解决方案 > 从数组中选择特定的 JSON 数据

问题描述

var arr = [{"jack": "27483127"}, {"kenz": "17182134"}];

如何选择“kenz”的值?

标签: javascriptarraysjsonvariables

解决方案


我假设你不知道 kenz 可能在这个数组中。


const foundItem = arr.find(x=>x.hasOwnProperty('kenz')) //to capture the object
const foundItem = arr.find(x=>x.hasOwnProperty('kenz'))?.kenz //to capture the property


相关阅读:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty


推荐阅读