0

I'm working on an project that uses React with hooks and typescript. I'm currently trying to get data from arrays inside an array that I get from my json response. Data what i log after data handling looks like this:

[
    [
        "2021-09-07",
        43
    ],
    [
        "2021-09-08",
        47
    ],
    [
        "2021-09-09",
        52
    ]
]

So the question is, how can i get the numerical values seen in the json, to an array?

EDIT: Got it working now. Had to add this:

const numbers = data.map((item: any) => item[1]);
  console.log(numbers);

Thanks alot @Ryan Le

2
  • Show in more detail in what format you need to receive data Commented Sep 7, 2021 at 11:17
  • I just want the data like this: [43, 47, 52] Commented Sep 7, 2021 at 11:17

2 Answers 2

1

You would map all the numbers from the original array like so:

type Data = [string, number][];

const data: Data = [
  ["2021-09-07", 43],
  ["2021-09-08", 47],
  ["2021-09-09", 52]
];

const result = data.map(item => item[1]);

console.log(result);

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

3 Comments

Hey, i tried your solution but im getting this error: Parameter 'item' implicitly has an 'any' type.ts(7006)
Ah, you are on typescript, I updated my answer
Yes, unfortunately I am. I got it working now, thanks alot!
0

If by numerical values you mean the second element of each array such as (43 , 47 , 52) you can simply map to a new array like this

const oldArray = [
  [
      "2021-09-07T08:53:34.4067874+00:00",
      43
  ],
  [
      "2021-09-07T09:53:34.4067881+00:00",
      47
  ],
  [
      "2021-09-07T10:53:34.4067884+00:00",
      52
  ]
]
const newArray = oldArray.map((el)=>
  el[1])
console.log(newArray)

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.