0
for (var i = 1; i < address.length; i++) {
    var markers = [{"name":"marker"+i, "address":address[0]}];
}

after loop i want my json markers look like

name: marker1, address: aaaa
name: marker2, address: abcd
name: marker3, address: adf

help me fix my code. thanks in advance, im new in json and i don't know how to initialize it with empty value and insert the value in for loop.

Mritunjay
  • 25,338
  • 7
  • 55
  • 68
  • `markers` is a string. `markers[0]` returns first character of that string. And a character (which in turn is a string as well) doesn't have a `myName` attribute. – zerkms Jul 03 '14 at 03:28

2 Answers2

1

Ok, so this is very easy.

var markers = [];

// This still needs address to be an array, also still don't understand why you don't begin at the start of the array
for (var i = 1; i < address.length; i++) {
    markers.push({
        name: "marker" + i,
        address: address[i]
    });
}

// If you do want to start at the begininning of the array, but still want it to start with "markers1"
for (var i = 0; i < address.length; i++) {
    markers.push({
        name: "marker" + (i + 1),
        address: address[i]
    });
}

Assuming address is already a valid javascript array, this will give you an array that looks like this:

[
    { name: "marker1", address: "whatever was here" },
    { name: "marker2", address: "whatever was there" }
]

Last edit: Deleted stuff that doens't have much to do with the newly edited question.

Maverick
  • 4,449
  • 4
  • 36
  • 46
  • no. i have a lot of code that need to pass in JSON i just put address to simplify it. expect there are variable address and name.. how can i insert it in json. thanks –  Jul 03 '14 at 03:33
  • @user3799958 that's nowhere near enough information. What sort of information do you have? Your question iterates through an array of strings. What data are you starting with and what do you want to end with? – Maverick Jul 03 '14 at 03:37
  • @user3799958: how about encoding it using `JSON.stringify` afterwards? You are not required to assemble it manually – zerkms Jul 03 '14 at 03:37
  • If all you want to do is turn the json string given by `address =;`, that is the correct thing to do. I'll update my answer in case that's what you want. – Maverick Jul 03 '14 at 03:38
  • still error,even i only put markers.push({ name: "marker" + (i + 1) }); my code is stop in executing after this line..i don't know whats wrong in my code –  Jul 03 '14 at 05:48
  • Put up a jsfiddle with your exact code and I'll take a look. The code I posted works perfectly you can check it here: http://jsfiddle.net/5jxwS/ – Maverick Jul 03 '14 at 05:49
0
    var address = ['aaaa', 'abcd', 'adf'];
    var markers = [];
      for (var i = 0; i < address.length; i++) {
        markers.push({
            name: "marker" + (+i + 1),
            address: address[i]
        });
    }
    var jsonMarkers = JSON.stringify(markers);
    // And jsonMarkers will look like :

    //"[{"name":"marker1","address":"aaaa"},{"name":"marker2","address":"abcd"},{"name":"marker3","address":"adf"}]"
abdulbasit
  • 1,856
  • 15
  • 17
  • `var x in y` is NOT a valid or correct way to access an array. `var x in y` is used to access the properties of an object. As a side effect, because in javascript arrays are really objects, this will sometimes work a bit for some arrays. It is not reliable and should NEVER be used for this, however. It is no way anything like a `foreach` construct. – Maverick Jul 03 '14 at 04:54
  • @MrN00b Can you please post a case where it is not reliable. – abdulbasit Jul 03 '14 at 04:58
  • 1
    http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea – Maverick Jul 03 '14 at 05:05
  • 1
    @MrN00b Thanks dear for the useful info. – abdulbasit Jul 03 '14 at 05:13
  • No worries @abdulbasit :). – Maverick Jul 03 '14 at 05:42