[英]get object that is inside array
{"joker":{"baseMapId":"1536abe5e5504e5db380ccfaa9b6fd8d","customBaseMap":"","zoomn":"5","orient":"0","centLon":"-93.69999999999843","centLat":"38.64999999999935","baseMapLabels":"true"}}
{"batman":{"baseMapId":"1536abe5e5504e5db380ccfaa9b6fd8d","customBaseMap":"","zoomn":"5","orient":"0","centLon":"-93.69999999999843","centLat":"38.64999999999935","baseMapLabels":"true"}}
{"merman":{"baseMapId":"1536abe5e5504e5db380ccfaa9b6fd8d","customBaseMap":"","zoomn":"5","orient":"0","centLon":"-93.69999999999843","centLat":"38.64999999999935","baseMapLabels":"true"}}
我的數據如上所示; 例如,我如何遍歷並獲取蝙蝠俠的數據。
我試過data['batman']
和data[0][batman]
- 我想要的結果是得到整個像,即如下:
{"batman":{"baseMapId":"1536abe5e5504e5db380ccfaa9b6fd8d","customBaseMap":"","zoomn":"5","orient":"0","centLon":"-93.69999999999843","centLat":"38.64999999999935","baseMapLabels":"true"}}
首先,你的共享結構是錯誤的。 它不是一個數組,而是一個一個寫入的 3 個對象,因此您必須將它們放入適當的數組結構中。
let arr = [{
"joker": {
"baseMapId": "1536abe5e5504e5db380ccfaa9b6fd8d",
"customBaseMap": "",
"zoomn": "5",
"orient": "0",
"centLon": "-93.69999999999843",
"centLat": "38.64999999999935",
"baseMapLabels": "true"
}
}, {
"batman": {
"baseMapId": "1536abe5e5504e5db380ccfaa9b6fd8d",
"customBaseMap": "",
"zoomn": "5",
"orient": "0",
"centLon": "-93.69999999999843",
"centLat": "38.64999999999935",
"baseMapLabels": "true"
}
}, {
"merman": {
"baseMapId": "1536abe5e5504e5db380ccfaa9b6fd8d",
"customBaseMap": "",
"zoomn": "5",
"orient": "0",
"centLon": "-93.69999999999843",
"centLat": "38.64999999999935",
"baseMapLabels": "true"
}
}]
如下所示,可以通過Array.find()
搜索這樣的結構。
arr.find(el => Object.keys(el)[0] === 'batman' )
const arr = [{ "joker": { "baseMapId": "1536abe5e5504e5db380ccfaa9b6fd8d", "customBaseMap": "", "zoomn": "5", "orient": "0", "centLon": "-93.69999999999843", "centLat": "38.64999999999935", "baseMapLabels": "true" } }, { "batman": { "baseMapId": "1536abe5e5504e5db380ccfaa9b6fd8d", "customBaseMap": "", "zoomn": "5", "orient": "0", "centLon": "-93.69999999999843", "centLat": "38.64999999999935", "baseMapLabels": "true" } }, { "merman": { "baseMapId": "1536abe5e5504e5db380ccfaa9b6fd8d", "customBaseMap": "", "zoomn": "5", "orient": "0", "centLon": "-93.69999999999843", "centLat": "38.64999999999935", "baseMapLabels": "true" } }] const result = arr.find(x => 'batman' in x); console.log(result)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.