【问题标题】:Generate Array of Objects of different Objects Javascript生成不同对象Javascript的对象数组
【发布时间】:2017-06-27 15:35:23
【问题描述】:

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

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

加密数量:

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

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

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

非常感谢您的帮助!

【问题讨论】:

    标签: javascript arrays lodash javascript-objects


    【解决方案1】:

    您可以映射其中一个对象的键以生成一个新的对象数组。您只需确保密钥在每个对象中。

    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】:

      您可以使用哈希映射(例如 'btc-usd' => {name:"Bitcoin",...})来创建新对象。这个 hashmap 可以很容易地转换成一个数组。

      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】:

      这是一个通用函数add,它接受一个字段名称和一个值对象,并将它们映射到一个result 对象,然后该对象可以映射到一个数组中。

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

        首先,创建一个每个缩写代表什么的字典。

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

        然后,用包含相关信息的对象填充一个空数组。在每个对象中,名称将对应于dictionary 对象中的相关键。同时,数量和值将分别对应于amountsprices 对象中的相关键。最后,Id 将对应于key 本身。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-07
          • 2022-07-07
          相关资源
          最近更新 更多