How to get specific information inside a Json with Node.JS?

3
{
"pair": "BTCBRL",
"bids": [
    [2257.89, 0.20752212, 90852987],
    [2257.88, 1.01201126, 90800395],
    [2249.98, 0.05052466, 90806289]
],
"asks": [
    [2272.14, 2.3648572, 90803493],
    [2279.63, 0.08722052, 90840584],
    [2279.75, 0.04118941, 90823262]
]
}

I have in my code a function that returns this JSON, I would like to take only the first values of the bids and the asks, that is, the values (2257.89, 2257.88, among others), how to proceed?

In the case, this JSON call orderbook, so if I get orderbook.bids, it returns this way:

[
[2257.89, 0.20752212, 90852987],
[2257.88, 1.01201126, 90800395],
[2249.98, 0.05052466, 90806289]
],

But I actually only want the values 2257.89, 2257.88 and 2249.98!

    
asked by anonymous 31.01.2017 / 16:50

1 answer

2

You can use Array # Map to map only the first item of each array :

var obj = {
  "pair": "BTCBRL",
  "bids": [
    [2257.89, 0.20752212, 90852987],
    [2257.88, 1.01201126, 90800395],
    [2249.98, 0.05052466, 90806289]
  ],
  "asks": [
    [2272.14, 2.3648572, 90803493],
    [2279.63, 0.08722052, 90840584],
    [2279.75, 0.04118941, 90823262]
  ]
};

var novoObj = Object.assign({}, obj);

['bids', 'asks'].forEach(function(item,i) {
  novoObj[item] = novoObj[item].map(a => a[0]);
});

console.log(novoObj);
.as-console-wrapper {
  top: 0;
  max-height: 100%!important
}
    
31.01.2017 / 16:52