生成不同对象Javascript的对象数组 [英] Generate Array of Objects of different Objects Javascript

查看:82
本文介绍了生成不同对象Javascript的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有包含一种数据的严格对象: 价格:

I have severel Objects containing one sort of data: Prices:

'btc-usd' : 2640, 'ltc-usd': 40, ...

加密数量:

'btc-usd': 2.533, 'ltc-usd': 10.42, ...

如何获取这些对象并创建对象数组,如:

How can I take these Objects and create an Array of Objects like:

[ { name: 'Bitcoin', amount: 2.533, value: 2640, id: 'btc-usd' },
 { name: 'Litecoin', amount: 10.42, value: 40, id: 'ltc-usd' }, ...
]

非常感谢您的帮助!

推荐答案

您可以映射其中一个对象的键以生成新的对象数组.您只需要确保密钥在所有这些对象中即可.

You could map the keys of one of the objects to produce a new array of objects. You just have to make sure, that the key is in every of these objects.

const names = {
    'btc-usd' : 'Bitcoin', 
    'ltc-usd': 'Litecoin', 
    ...
}

const prices = {
    'btc-usd' : 2640, 
    'ltc-usd': 40, 
    ...
}

const amounts = {
    'btc-usd': 2.533, 
    'ltc-usd': 10.42, 
    ...
}

const cryptos = Object.keys(names).map((key, index) => ({
    name: names[key],
    amount: amounts[key] , 
    value: prices[key]},
    id: key
}));

这篇关于生成不同对象Javascript的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆