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