scorezel789
scorezel789

Reputation: 163

How to loop a 2D array and store objects?

Im trying to store objects into the array. I tried using the 2D array loop and im not getting the right results that i want. Can i know what is wrong and how do i fix it? Its printing 15 times instead of 5 times just like how i want the output stated below.

Input:

var places = ['Place A', 'Place B', 'Place C', 'Place D', 'Place E'];
var data = [];
var results = [
  [ '1.3784294469999998', '103.9424764', '519498' ],
  [ '1.325617641', '103.67841059999999', '629117' ],
  [ '1.3835802030000002', '103.7707748', '670445' ],
  [ '1.322595392', '103.8136987', '258748' ],
  [ '1.3061957990000002', '103.828509', '238884' ]
]

Output I want:

[
  [ places: 'Place A', lat: '1.3784294469999998', lon: '103.9424764', zip: '519498' ],
  [ places: 'Place B', lat: '1.325617641', lon: '103.67841059999999', zip: '629117' ],
  [ places: 'Place C',lat: '1.3835802030000002', lon: '103.7707748', zip: '670445' ],
  [ places: 'Place D',lat: '1.322595392', lon: '103.8136987', '258748' ],
  [ places: 'Place E',lat: '1.3061957990000002', lon: '103.828509', zip: '238884' ]
]   

Code:

  for (i = 0; i < results.length; i++) {
    for (j = 0; j < results[i].length; j++) {
      data.push({ places: places[i], lat: results[i][j], lon: results[i][j], zip: [i][j]});
    }
  }

Upvotes: 1

Views: 68

Answers (3)

Eli Johnson
Eli Johnson

Reputation: 1631

Put your code in a function and return the results like so:

function combine() {
  let data = []
  for (i = 0; i < results.length; i++) {
    for (j = 0; j < results[i].length; j++) {
      data.push({ places: places[i], lat: results[i][j], lon: results[i][j], zip: [i][j]});
    }
  }
  return data
}

Upvotes: 0

Phy
Phy

Reputation: 248

What is happening in your code

Currently with your for loop for every element in places it will go over every nested element in results. This is how you get your 15 entries (5 placed times 3 results)

Solution:

for (i = 0; i < results.length; i++) {
    data.push({ places: places[i], lat: results[i][0], lon: results[i][1], zip: [i][2]});
}

This will loop over all your places and add the results in. It is almost what you had, but without the nested for loop and j replaced for the index of lat, lon and zip respectively.

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 370729

You need a single loop, not a nested loop:

var places = ['Place A', 'Place B', 'Place C', 'Place D', 'Place E'];
var data = [];
var results = [
  [ '1.3784294469999998', '103.9424764', '519498' ],
  [ '1.325617641', '103.67841059999999', '629117' ],
  [ '1.3835802030000002', '103.7707748', '670445' ],
  [ '1.322595392', '103.8136987', '258748' ],
  [ '1.3061957990000002', '103.828509', '238884' ]
];
for (i = 0; i < results.length; i++) {
  data.push({ places: places[i], lat: results[i][0], lon: results[i][1], zip: results[i][2]});
}
console.log(data);

Or, perhaps more readably with .map and destructuring:

var places = ['Place A', 'Place B', 'Place C', 'Place D', 'Place E'];
var results = [
  [ '1.3784294469999998', '103.9424764', '519498' ],
  [ '1.325617641', '103.67841059999999', '629117' ],
  [ '1.3835802030000002', '103.7707748', '670445' ],
  [ '1.322595392', '103.8136987', '258748' ],
  [ '1.3061957990000002', '103.828509', '238884' ]
];
const data = results.map(
  ([lat, lon, zip], i) => ({ places: places[i], lat, lon, zip })
);
console.log(data);

Upvotes: 5

Related Questions