-1

I'm struggling to understand how should objects be accessed if they are at the same position in an array?

For example: An array that consists of 3 arrays. Each of those 3 arrays hold 48 objects that are identical. I only need 1 object from each of those arrays. Array[0] gets me those 3 objects but they are at the same position thus when i'm loading the array[0] into datasets for a chart it doesn't show them at the same time, but updating over each other.

I'd appreciate any help. Thanks!

console.log(graphDatasetsArray[0]) 

output :

Object { label: "obj1", data: (48) […], fill: false, borderColor: "#ff0000", backgroundColor: "#ff0000", pointBackgroundColor: "#ff0000" }

Object { label: "obj2", data: (48) […], fill: false, borderColor: "#ff0000", backgroundColor: "#ff0000", pointBackgroundColor: "#ff0000" }

Object { label: "obj3", data: (48) […], fill: false, borderColor: "#ff0000", backgroundColor: "#ff0000", pointBackgroundColor: "#ff0000" }

How could I put these 3 objects into a new array, so that I can access them by [0],[1],[3] etc. ?

alienwife
  • 70
  • 1
  • 11
  • I don't understand the question, what's the expected output? – user202729 Aug 17 '18 at 06:57
  • `Array[0] gets me those 3 objects`, you probably mean those 3 arrays. `They are at the same position` No, they're not. They're at position 0, 1 and 2. You can access them using `mainArray[0]`, `mainArray[1]`, `mainArray[2]`. – Jeremy Thille Aug 17 '18 at 06:57
  • You are definitely talking about 2D arrays: [2D arrays in JavaScript](https://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript) – Melvin Abraham Aug 17 '18 at 06:59
  • The output in console are 3 objects as I stated above. typeof(array[]) – alienwife Aug 17 '18 at 07:05
  • returns object. If I save the array[0] in a new variable and then try to access each object with [0],1,2 it logs undefined. I'll check out the 2D arrays. Thanks! – alienwife Aug 17 '18 at 07:15

1 Answers1

0

What you have is an multidimensional array that means your array looks like this

var array = [[{ob1},{ob2}],[{ob3},{ob4}],[{ob5},{ob6}]]

To access the first object of the first array you have to get it the following way

array[0][0]

Which would print ob1. To access the first element of the third array you just have to write

array[2][0]

I added a snippet as a showcase

var array = [[{obj:1},{obj:2}],[{obj:3},{obj:4}],[{obj:5},{obj:6}]];
console.log(array[0][0]);
console.log(array[2][0]);

Edit: Objects to array example

var array = [{data: [{obj: 1}]},{data: [{obj: 2}]},{data: [{obj: 3}]}];
var parsedArray = array.map(function(array){
  return array.data[0];
})
console.log(parsedArray)
console.log(parsedArray[0])
console.log(parsedArray[1])
console.log(parsedArray[2])
Sven
  • 357
  • 4
  • 10
  • Yeah I tried this before and it just returns - undefined. I do believe the problem is me not understanding the structure of the data i'm looping through. Thanks for the reply tho! – alienwife Aug 17 '18 at 07:21
  • Yes if you look at your code you want to access one of the 48 objects which are in the property data in an array containing 3 objects. Which would result in `array[0].data[0]` -->`array[0]` The first element in the array (the object containing the property data) --> `.data` to access the property data of it (which is an array of 48 elements) --> the first element `[0] ` – Sven Aug 17 '18 at 07:33
  • you are correct. I can access the data array within the objects. but i dont need to access those. I need those 3 objects to be separate. so i can access the objects separately through an array with [0][1][2] etc. Sorry for being unclear. – alienwife Aug 17 '18 at 08:08
  • no problem you could write a map function to gain an array. Ill add it to my answer – Sven Aug 17 '18 at 09:17