我有以下代码:snapshot.val()
对象来自firebase数据库并使用map函数获取名称。
database.ref('/destinations').once('value', function (snapshot) {
const locations = snapshot.val();
const destinations = Object.keys(locations).map(key =>
locations[key].name
)
console.log(destinations);
// returns ["California", "Nevada"]
dispatch(
get_destinations({
...destinations
})
);
})
我想重新格式化结果,因此它看起来像这样:
const destinations = [
{ label: 'California', value: 'california' },
{ label: 'Nevada', value: 'nevada' },
]
答案 0 :(得分:3)
const destinations = Object.keys(locations).map(key =>
({
label: locations[key].name,
value: locations[key].name.toLowerCase()
})
)