0

I'm working on a project, where I have to get 2 json objects to different arrays for use in the app. I want it to be done dynamically, because in the future there might be one, two, three or possibly four objects inside this json array. I know how to get the objects with hard coding the list indexes and pushing them into array, but i dont know how to do it dynamically.

Here is the JSON data that im working with:

[
  {
    equipment: "",
    values: [
      {
        id: "123456",
        timestamp: [
          
        ]
      },
      {
        id: "12345",
        timestamp: [
          
        ]
      }
    ]
  }
]

So, tl:dr is: How do i get both "values" objects to their own arrays? Also im working with react and typescript.

1
  • can you please share some code what you have done till now? and an example of the final result you need. for me its difficult to understand what actually you need. Commented Oct 15, 2021 at 7:43

1 Answer 1

1

Use map since your data seems to be an array...

let listOne = [];
let listTwo = [];

data.map(object => object.values).forEach(valuesArray => {
 listOne.push( valuesArray[0]);
 listTwo.push( valuesArray[1]);
})

console.log(listOne, listTwo) 
Sign up to request clarification or add additional context in comments.

2 Comments

Hey! I tried your solution, but im getting same amount of objects returned as with my previous solution that only returns the first object from the json.
I have modified the code to seperate the values into two lists

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.