我有這個陣列,我想用控制臺記錄item值。我怎樣才能做到這一點?我從MongoDB得到這個陣列。我將感謝任何幫助。
{
"_id" : "61462a7bf3c0be993bcfdc3e"/span>,
"item": "journal",
"qty": 25,
"size": {
"h": 14,
"w": 21,
"uom": "cm"。
},
"status": "A"
}
編輯。 我查看了MongoDB的檔案,找到了解決方案。感謝所有回答的人。
uj5u.com熱心網友回復:
你可以試試:
Object.entries(YOUR_OBJECT_NAME) 。 filter(val=>val[0] =="item")
你將得到一個鍵和值對的陣列。然后你可以在控制臺記錄你的值。
更簡單的解決方案是,你直接確定你的鍵。如果你知道你的鍵的名稱是什么,那么你可以這樣做:
console。 log(YOUR_OBJECT_NAME.item)
uj5u.com熱心網友回復:
我認為你可以像這樣簡單地從物件中訪問鍵:
。const obj = {
"_id"/span> : "61462a7bf3c0be993bcfdc3e"/span>,
"item": "journal",
"qty": 25,
"size": {
"h": 14,
"w": 21,
"uom": "cm"。
},
"status": "A"
}
console.log(obj.item);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
另一種方法是,如果鍵的性質是動態的,可以找出鍵并對其進行迭代:
。const obj = {
"_id"/span> : "61462a7bf3c0be993bcfdc3e"/span>,
"item": "journal",
"qty": 25,
"size": {
"h": 14,
"w": 21,
"uom": "cm"。
},
"status": "A"
}
const keys = Object.keys(obj)。
keys.forEach(span class="hljs-params">key => console. log(`Key is ${key} and the value is ${JSON. stringify(obj[key])}`))
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/324686.html
標籤:
