0

Here is object that i get from api

{
        status: true,
        code: 200,
        msg: "Successfully",
        response: {
          1609927200: {
            o: "1.2338",
            h: "1.2344",
            l: "1.2333",
            c: "1.23395",
            v: "5436",
            t: 1609927200,
            tm: "2021-01-06 10:00:00",
           },
          1609930800: {
            o: "1.2338",
            h: "1.23495",
            l: "1.2333",
            c: "1.2337",
            v: "5333",
            t: 1609930800,
            tm: "2021-01-06 11:00:00",
          },
          1609934400: {
            o: "1.23375",
            h: "1.23495",
            l: "1.233",
            c: "1.234",
            v: "5636",
            t: 1609934400,
            tm: "2021-01-06 12:00:00",
          },
        }  
  }

And I want turn it to object wtith arrays like this

{
        ohlcv: [
          [1609927200, 1.2338, 1.2344, 1.2333, 1.23395, 5436],
          [1609930800, 1.2338, 1.23495, 1.2333, 1.2337, 5333],
          [1609934400, 1.23375, 1.23495, 1.233, 1.234, 5636],
        ],
      }

With a lot of effort, i ended up with this result with folowing code:

*this.symbol hold the object for the example*

    var res = Object.keys(this.symbol).map((item) => {
        return {
          ohlvc: Object.keys(this.symbol[item]).map((key) => {
            return Object.values(this.symbol[item][key]);
          }),
        };
      })[3];

 { 
    "ohlvc": [ 
    [ "1.2338", "1.2344", "1.2333", "1.23395", "5436", 1609927200, "2021-01-06 10:00:00" ], 
    [ "1.2338", "1.23495", "1.2333", "1.2337", "5333", 1609930800, "2021-01-06 11:00:00" ], 
    [ "1.23375", "1.23495", "1.233", "1.234", "5636", 1609934400, "2021-01-06 12:00:00" ],
        ]
 }

but:

  1. the values should be without quotes ( i already try wit JSON.stringify() but i get very crazy results )

2.and the element in the 5th position which is timestamp should be on first place.

Any help is appreciated

Vecot
  • 21
  • 1
  • 5

3 Answers3

1

You could convert all values to numbers.

const
    object = { status: true, code: 200, msg: "Successfully", response: { 1609927200: { o: "1.2338", h: "1.2344", l: "1.2333", c: "1.23395", v: "5436", t: 1609927200, tm: "2021-01-06 10:00:00" }, 1609930800: { o: "1.2338", h: "1.23495", l: "1.2333", c: "1.2337", v: "5333", t: 1609930800, tm: "2021-01-06 11:00:00" },     1609934400: { o: "1.23375", h: "1.23495", l: "1.233", c: "1.234", v: "5636", t: 1609934400, tm: "2021-01-06 12:00:00" } } },
    keys = ['o', 'h', 'l', 'c', 'v'],
    result = { ohlcv: Object
        .entries(object.response)
        .map(([v, o]) => [v, ...keys.map(k => o[k])].map(Number)) };

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Here is process method, have the key (with desired order of chars in sequence).
Use Object.values, map and + operator (convert to int).

const process = (obj, key = "ohlcv") => ({
  [key]: Object.values(obj.response).map((item) =>
    [new Date(item.tm).getTime()].concat([...key].map((kk) => item[kk]))
  ),
});

const data = {
  status: true,
  code: 200,
  msg: "Successfully",
  response: {
    1609927200: {
      o: "1.2338",
      h: "1.2344",
      l: "1.2333",
      c: "1.23395",
      v: "5436",
      t: 1609927200,
      tm: "2021-01-06 10:00:00",
    },
    1609930800: {
      o: "1.2338",
      h: "1.23495",
      l: "1.2333",
      c: "1.2337",
      v: "5333",
      t: 1609930800,
      tm: "2021-01-06 11:00:00",
    },
    1609934400: {
      o: "1.23375",
      h: "1.23495",
      l: "1.233",
      c: "1.234",
      v: "5636",
      t: 1609934400,
      tm: "2021-01-06 12:00:00",
    },
  },
};


console.log(process(data))
Siva K V
  • 10,561
  • 2
  • 16
  • 29
  • Hello, your example work just fine, but instead "t" on the first place should be "tm" which was my fault. Now that is clear how to be achieved, but how to converted to timestapm, before to be passed in the array. – Vecot Jan 25 '21 at 11:27
  • @Vecot, Just updated the answer. Basically you can use `new Date().getTime()`. – Siva K V Jan 25 '21 at 16:34
1

This function instead will convert into double your numbers, transform the object into arrays and manipulate them to show the timestamp as first element. Just feed it with res variable, I put below a working example.

const data = Object.values(res['response']).map(item => Object.values(item).map((value, index, item) => item.length === index + 1 ? value : parseFloat(value)));

const result = {
    ohlcv: [...data.map(list => [
    ...list.filter(array => array[array.length - 1]),
    ...list.filter(array => !array[array.length - 1])
  ])]
};

Let me know if you have any question :)

const res = {
        status: true,
        code: 200,
        msg: "Successfully",
        response: {
          1609927200: {
            o: "1.2338",
            h: "1.2344",
            l: "1.2333",
            c: "1.23395",
            v: "5436",
            t: 1609927200,
            tm: "2021-01-06 10:00:00",
           },
          1609930800: {
            o: "1.2338",
            h: "1.23495",
            l: "1.2333",
            c: "1.2337",
            v: "5333",
            t: 1609930800,
            tm: "2021-01-06 11:00:00",
          },
          1609934400: {
            o: "1.23375",
            h: "1.23495",
            l: "1.233",
            c: "1.234",
            v: "5636",
            t: 1609934400,
            tm: "2021-01-06 12:00:00",
          },
        }  
  };

const data = Object.values(res['response']).map(item => Object.values(item).map((value, index, item) => item.length === index + 1 ? value : parseFloat(value)));

const result = {
    ohlcv: [...data.map(list => [
    ...list.filter(array => array[array.length - 1]),
    ...list.filter(array => !array[array.length - 1])
  ])]
};

console.log(result);