1

The below is my data that I get from an API and I want to group it as time in one index and the other values in another index. Is that possible to split this? I want the array to be in the format as the values in a second array format

Api data

0: (5) [1577080200000, 157.5599, 157.4902, 157.495, 78330]
1: (5) [1577080500000, 157.54, 157.485, 157.5128, 118202]
2: (5) [1577080800000, 157.54, 157.47, 157.48, 130805]

Expected Output

[1577080200000][157.5599, 157.4902, 157.495, 78330]
[1577080500000][157.54, 157.485, 157.5128, 118202]
[1577080800000][157.54, 157.47, 157.48, 130805]

5 Answers 5

2

You could destructure the array and take the first element and the rest for mapping.

var data = [[1577080200000, 157.5599, 157.4902, 157.495, 78330], [1577080500000, 157.54, 157.485, 157.5128, 118202], [1577080800000, 157.54, 157.47, 157.48, 130805]],
    result = data.map(([first, ...rest]) => [[first], rest]);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

2

I don't see why would you need one element (first element of the arrays) inside the array.

If you really want 2 separate arrays, one for the first element and one for the rest then just add an extra array wrapper around [x[0]]

const arr = [
  [1577080200000, 157.5599, 157.4902, 157.495, 78330],
  [1577080500000, 157.54, 157.485, 157.5128, 118202],
  [1577080800000, 157.54, 157.47, 157.48, 130805],
]

const newArr = arr.map(x => [x[0],[...x.filter((y, i) => i !== 0)]
]);
console.log(newArr);

Comments

0

You could use splice:

const arr = [1577080200000, 157.5599, 157.4902, 157.495, 78330]
const traverseArr = [arr.splice(0,1),arr]

console.log(traverseArr)

1 Comment

you are mutating the data.
0

Your results array that you mentioned is not a 2 dimensional array, the correct format of 2-dim array is:

[[1577080800000], [157.54, 157.47, 157.48, 130805]]

However, you can get your expected result in a two-dimensional array with the following code:

function splitArray(arr: any[]) {
    const first = arr.slice(0, 1);
    const tail = arr.slice(1);
    return [first, tail];
}

and then call the function like this:

splitArray([1577080200000, 157.5599, 157.4902, 157.495, 78330]);

Comments

0

You can use flatMap method to flat array which will be created by the function:

const result = arr.flatMap(([a, b, c, d])=> [[a], [b, c, d]]);

An example:

const arr = [
  [1577080200000, 157.5599, 157.4902, 157.495, 78330],
  [1577080500000, 157.54, 157.485, 157.5128, 118202],
  [1577080800000, 157.54, 157.47, 157.48, 130805],
]

const result = arr.flatMap(([a, b, c, d])=> [[a], [b, c, d]]);
console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.