Generate Array of Objects of different Objects Javascript

喜你入骨 提交于 2019-12-25 09:27:54

问题


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

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

Amount of Crypto:

'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' }, ...
]

Thanks a lot for your help!


回答1:


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
}));



回答2:


You could use a hash map (e.g. 'btc-usd' => {name:"Bitcoin",...}) to create new objects. This hashmap can be easily converted to an array.

var input={
  value:{'btc-usd' : 2640, 'ltc-usd': 40},
  amount:{'btc-usd': 2.533, 'ltc-usd': 10.42},
  name:{"btc-usd":"Bitcoin","ltc-usd":"Litecoin"}
};

var hash={};
for(key in input){
   var values=input[key];
   for(id in values){
      if(!hash[id]) hash[id]={id:id};
      hash[id][key]=values[id];
    }
}

var output=Object.values(hash);

http://jsbin.com/fadapafaca/edit?console




回答3:


Here's a generalized function, add, that accepts a field name and an object of values and maps them into a result object which can then be mapped into an array.

const amounts = {btc: 123.45, eth: 123.45};
const names = {btc: 'Bitcoin', eth: 'Etherium'};

const result = {};

const add = (field, values) => {
  Object.keys(values).forEach(key => {
    // lazy initialize each object in the resultset
    if (!result[key]) {
      result[key] = {id: key};
    }

    // insert the data into the field for the key
    result[key][field] = values[key];
  });
}

add('amount', amounts);
add('name', names);

// converts the object of results to an array and logs it
console.log(Object.keys(result).map(key => result[key]));



回答4:


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

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

First, create a dictionary of what each abbreviation stands for.

const dictionary = {
  'btc': 'Bitcoin',
  'ltc': 'Litecoin'
};

Then, populate an empty array with objects containing the relevant information. In each of these objects, the name would correspond to the relevant key within the dictionary object. At the same time, the amount and value would correspond to the relevant key within the amounts and prices objects respectively. Finally, the Id would correspond to the key itself.

const money = [];

for(let coin in prices) {
  money.push({
    name: dictionary[coin.substr(0, coin.indexOf('-'))],
    amount: amounts[coin],
    value: prices[coin],
    id: coin
  });
}

console.log(money);


来源:https://stackoverflow.com/questions/44784387/generate-array-of-objects-of-different-objects-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!