将JSON对象数组映射到字符串 [英] Map JSON objects array to strings

查看:139
本文介绍了将JSON对象数组映射到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试利用Google SpreadSheets(Google脚本)中的地图功能,使用API​​从Bittrex获取我的帐户硬币余额.这是我的JSON对象:

I'm trying to utilize map function in Google SpreadSheets (Google Script) to get my account coin balances from Bittrex using API. Here is my JSON object:

({success:true,
  message:"",
  result:[
      {Currency:"BTC",
       Balance:0.01,
       Available:0.01,
       Pending:0,
       CryptoAddress:null},
      {Currency:"ETH",     
       Balance:1.0,
       Available:1.0,
       Pending:0,
       CryptoAddress:null}
    ]}
})

理想情况下,我想根据结果和基础行中的键(使用每个对象的数据)自动填充标题行. 我看到了spme解决方案如何将其用于每种或更复杂的方法.但是我想这可以通过映射来完成.这是我如何映射第一行,但不知道如何映射值:

Ideally I would like to populate header row automatically, based on Keys in result and underlying rows using data from each object. I saw spme solutions how to do that using for each or more complicated way. But I guess this can be done by just mapping. Here is how I mapped top row, but don't know how to map values:

var headerRow = Object.keys(json.result[0]);

Google SpreadSheet中的预期输出为

Expected output in Google SpreadSheet is

____________________________________________________________    
| Currency | Balance | Available | Pending | CryptoAddress |
|__________________________________________________________|
| BTC      | 0.01    | 0.01      | 0       | null          | 
| ETH      | 1.0     | 1.0       | 0       | null          |
____________________________________________________________

推荐答案

我将执行以下操作.因此,您有了密钥,然后就可以检索该值.

I would do the following. So you have the key and then you can retrieve the value.

Object.keys(json.result[0]).map((val) => { console.log(json.result[0][val])})

将其包装在foreach中以对每个结果执行相同的操作.

Wrap that within a foreach to do the same for each result.

希望有帮助

这篇关于将JSON对象数组映射到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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