我有小菜一碟。例如
var Fruits={
"Banana": {
"Price": "50",
"quility": "good"
},
"Mango": {
"Price": "100",
"quility": "best"
},
"Orange": {
"Price": "80",
"quility": "good"
},
"Apple": {
"Price": "150",
"quility": "best"
}
}
并且,包含要提取的Fruits名称的数组。例如
var SpecificFruits = ["Banana", "Mango"]
基于上面的数组,我想提取水果对象并想要如下结果。
var results={
"Banana": {
"Price": "50",
"quility": "good"
},
"Mango": {
"Price": "100",
"quility": "best"
"quility": "best"
}
}
任何帮助/建议,将不胜感激。
最佳答案
您只需执行以下操作即可实现此目的:
let result = {}
SpecificFruits.forEach((fruit) => {
if(Fruits[fruit]) {
result[fruit] = Fruits[fruit]
}
})