[英]Return key and value from object if value equals
當typeof value
等於某個值時,我可以只返回具有多個鍵的 object 中的鍵/值對嗎?
var recs = {
"user_name": "C_51",
"num1": 51,
"num2": 101,
"num3": 151
},
{
"metric": 4461996.0,
"title": "v_revenue"
}
Object.values(recs).forEach(k => {
if (typeof k == 'string') {
//then show:
{"user_name": "C_51"},
{"title": "v_revenue"}
}
});
有沒有辦法做到這一點?
你的代碼……有點亂。 但是你想要做什么的核心想法並不難。
如果你有:
const recs = {
"user_name": "C_51",
"num1": 51,
"num2": 101,
"num3": 151
};
你可以做...
const entries = Object.entries(recs);
const entryObjects = entries.map(name, value) => ({ [name]: value });
然后 entryObjects 將是由recs
的“條目”組成的對象數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.