如何将此对象转换为数组?

时间:2018-05-18 20:26:38

标签: javascript arrays object local-storage

{
  "ethereum": {
    "balance":"2",
    "value":1382.4,
    "id":"ethereum",
    "name":"Ethereum",
    "symbol":"ETH",
    "rank":"2",
    "price_usd":"691.204",
    "24h_volume_usd":"2420600000.0",
    "percent_change_1h":"0.02",
    "percent_change_24h":"0.51",
    "percent_change_7d":"0.98",
    "percentage":14.34
  },
  "bitcoin": {
    "balance":"1",
    "value":8255.95,
    "id":"bitcoin",
    "name":"Bitcoin",
    "symbol":"BTC",
    "rank":"1",
    "price_usd":"8255.96",
    "24h_volume_usd":"6128880000.0",
    "percent_change_1h":"0.02",
    "percent_change_24h":"0.43",
    "percent_change_7d":"-3.49",
    "percentage":85.66
  }
} 

上面的对象已从下面的数组转换,然后保存到localStorage。

我尝试做的是重新创建以下数组:

[
  {
    24h_volume_usd: "6124340000.0",
    balance: "1",
    id: "bitcoin",
    name: "Bitcoin",
    percent_change_1h: "-0.1",
    percent_change_7d: "-3.46",
    percent_change_24h: "0.47",
    percentage: 85.66,
    price_usd: "8256.98",
    rank: "1",
    symbol: "BTC",
    value: 8256.98
  },
  {
    4h_volume_usd: "2420170000.0",
    balance: "2",
    id: "ethereum",
    name: "Ethereum",
    percent_change_1h: "-0.07",
    percent_change_7d: "0.95",
    percent_change_24h: "0.49",
    percentage: 14.34,
    price_usd: "691.074",
    rank: "2",
    symbol: "ETH",
    value: 1382.14
  }
]

数组到对象逻辑

export const calculatePercentage = (portfolio, coin) => {
  portfolio.push(coin);

  const addValue = c => c.value;
  const values = R.chain(addValue, portfolio);
  const total = values.reduce((acc, val) => acc + val);

  const updatedPortfolio = portfolio.map((c) => {
    c.percentage = round((c.value / total) * 100);
    return c;
  });

  const moonPortfolio = arrayToObject(updatedPortfolio);

  // Local Storage saved here:
  window.localStorage.setItem('moonPortfolio', JSON.stringify(moonPortfolio));

  return updatedPortfolio;
};

4 个答案:

答案 0 :(得分:4)

您可以使用Object.values方法获取数组中对象的所有值:



const object = {
  "ethereum": {
    "balance":"2",
    "value":1382.4,
    "id":"ethereum",
    "name":"Ethereum",
    "symbol":"ETH",
    "rank":"2",
    "price_usd":"691.204",
    "24h_volume_usd":"2420600000.0",
    "percent_change_1h":"0.02",
    "percent_change_24h":"0.51",
    "percent_change_7d":"0.98",
    "percentage":14.34
  },
  "bitcoin": {
    "balance":"1",
    "value":8255.95,
    "id":"bitcoin",
    "name":"Bitcoin",
    "symbol":"BTC",
    "rank":"1",
    "price_usd":"8255.96",
    "24h_volume_usd":"6128880000.0",
    "percent_change_1h":"0.02",
    "percent_change_24h":"0.43",
    "percent_change_7d":"-3.49",
    "percentage":85.66
  }
} 

const array = Object.values(object)
console.log(array)




答案 1 :(得分:3)

Object.values正是您要找的。

Object.values()方法返回给定对象自身的可枚举属性值的数组,其顺序与for...in提供的顺序相同



var o = {
  "ethereum": {
    "balance":"2",
    "value":1382.4,
    "id":"ethereum",
    "name":"Ethereum",
    "symbol":"ETH",
    "rank":"2",
    "price_usd":"691.204",
    "24h_volume_usd":"2420600000.0",
    "percent_change_1h":"0.02",
    "percent_change_24h":"0.51",
    "percent_change_7d":"0.98",
    "percentage":14.34
  },
  "bitcoin": {
    "balance":"1",
    "value":8255.95,
    "id":"bitcoin",
    "name":"Bitcoin",
    "symbol":"BTC",
    "rank":"1",
    "price_usd":"8255.96",
    "24h_volume_usd":"6128880000.0",
    "percent_change_1h":"0.02",
    "percent_change_24h":"0.43",
    "percent_change_7d":"-3.49",
    "percentage":85.66
  }
};

 var x = Object.values(o);
 
 console.log(x)




答案 2 :(得分:2)

以下代码也可用于获取包含对象的数组。

// Object to string conversion (just to test)
var stringifiedObj = JSON.stringify({
  "ethereum":{
    "balance":"2",
    "value":1382.4,
    "id":"ethereum",
    "name":"Ethereum",
    "symbol":"ETH",
    "rank":"2",
    "price_usd":"691.204",
    "24h_volume_usd":"2420600000.0",
    "percent_change_1h":"0.02",
    "percent_change_24h":"0.51",
    "percent_change_7d":"0.98",
    "percentage":14.34
  },
  "bitcoin": {
     "balance":"1",
     "value":8255.95,
     "id":"bitcoin",
     "name":"Bitcoin",
    "symbol":"BTC",
    "rank":"1",
    "price_usd":"8255.96",
    "24h_volume_usd":"6128880000.0",
    "percent_change_1h":"0.02",
    "percent_change_24h":"0.43",
    "percent_change_7d":"-3.49",
    "percentage":85.66
  }
});

var obj = JSON.parse(stringifiedObj); // string formed object to real object conversion

var arr = []; // Empty array
for(var key in obj){
    arr.push(obj[key]); // Push item(object) into array
}

console.log(arr);

/*
[ { balance: '2',
value: 1382.4,
id: 'ethereum',
name: 'Ethereum',
symbol: 'ETH',
rank: '2',
price_usd: '691.204',
'24h_volume_usd': '2420600000.0',
percent_change_1h: '0.02',
percent_change_24h: '0.51',
percent_change_7d: '0.98',
percentage: 14.34 },
{ balance: '1',
value: 8255.95,
id: 'bitcoin',
name: 'Bitcoin',
symbol: 'BTC',
rank: '1',
price_usd: '8255.96',
'24h_volume_usd': '6128880000.0',
percent_change_1h: '0.02',
percent_change_24h: '0.43',
percent_change_7d: '-3.49',
percentage: 85.66 } ]
*/

感谢。

答案 3 :(得分:1)

也可以在解析过程中完成:



var j = '{"ethereum":{"balance":"2","value":1382.4,"id":"ethereum","name":"Ethereum","symbol":"ETH","rank":"2","price_usd":"691.204","24h_volume_usd":"2420600000.0","percent_change_1h":"0.02","percent_change_24h":"0.51","percent_change_7d":"0.98","percentage":14.34},"bitcoin":{"balance":"1","value":8255.95,"id":"bitcoin","name":"Bitcoin","symbol":"BTC","rank":"1","price_usd":"8255.96","24h_volume_usd":"6128880000.0","percent_change_1h":"0.02","percent_change_24h":"0.43","percent_change_7d":"-3.49","percentage":85.66}}';

var a = [];
JSON.parse(j, function(k, v) {
  if (v.id) a.push(v);
  else return v;
})

console.log( a );