1

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]});
    }
  }

3 Answers 3

5

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);

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

Comments

1

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.

1 Comment

Thanks for your explanation! I think this is the clearest and very simple to understand. I was thinking too hard when its actually quite simple. Thank you for your help!
0

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
}

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.