0

I have an existing multidimensional associative array with this structure. It has 3 elements. Each of the subarrays has 3 elements.

{"id":"723419","lat":33.217,"lon":-92.817,"dist":1437.15733534053},
{"id":"723424","lat":33.567,"lon":-91.717,"dist":1902.4556686060116},
{"id":"722447","lat":32.383,"lon":-94.717,"dist":2317.6870313059217},

I want to create another (temp) array with 3 elements, and loop through the array, adding an element to each subarray so that each subarray will have 4 elements:

{"id":"723419","lat":33.217,"lon":-92.817,"dist":1437.15733534053,"elev":abc},
{"id":"723424","lat":33.567,"lon":-91.717,"dist":1902.4556686060116,"elev":def},
{"id":"722447","lat":32.383,"lon":-94.717,"dist":2317.6870313059217,"elev":ghi},

so far, I've tried to loop through my temp array

 for (var i in tempArray) { //loop through locations returned with elevation data
     multiArray[i]['elev']=...
 }

and treated it as an object (even though I declared the multi array as an Array()

 for (var i in tempArray) { //loop through locations returned with elevation data
      multiArray.i.append(elev)=...
 }

Both ways, JS complains that multiArray[i] doesn't exist. From what I know about JS, this should work.

Thanks for the help

2
  • 2
    You are looping through tempArray but trying to access the multiArray. And your list is like this:- [{"id":"723419","lat":33.217,"lon":-92.817,"dist":1437.15733534053}, {"id":"723424","lat":33.567,"lon":-91.717,"dist":1902.4556686060116}, {"id":"722447","lat":32.383,"lon":-94.717,"dist":2317.6870313059217}] ? For arrays use simple for loop not for in loop. for in loop is best for objects not arrays. Commented Jun 5, 2013 at 10:40
  • JavaScript is not PHP. Do not use for .. in to loop through arrays in JavaScript, it's the Wrong Thing (use a regular for loop from i = 0 to array.length instead). Also, those are objects, not associative arrays. Whether you use Array() or [] to declare it does not make any technical difference, but you should always use []. Commented Jun 5, 2013 at 10:42

1 Answer 1

1

i made simple demo that has predefined values and then create another array that get it's value from the first array then add the new element elev

var arr = [{
    "id": "723419",
        "lat": 33.217,
        "lon": -92.817,
        "dist": 1437.15733534053
}, {
    "id": "723424",
        "lat": 33.567,
        "lon": -91.717,
        "dist": 1902.4556686060116
}, {
    "id": "722447",
        "lat": 32.383,
        "lon": -94.717,
        "dist": 2317.6870313059217
}];
var tempArray = new Array();
for (var item in arr) {    
    tempArray.push(arr[item]);
    tempArray[item]["elev"] = "value"; //write here your value
}

JSFIDDLE

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

2 Comments

for .. in is not the proper way to iterate an array in JavaScript, because it lists any members that might have been added to the array prototype. Please use the correct form for (i = 0; i<arr.length; i++).
Between ebram tharwat's and @pvnarula's answers, I figured it out. However, if the two arrays are the same length, does it matter which array I'm looping through? i still has the same slot in the array

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.