最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to Separate values from json array using jquery.? - Stack Overflow

programmeradmin1浏览0评论

This is my json

{
  "data": [
    [
      "1",
      "Skylar Melovia"
    ],
    [
      "4",
      "Mathew Johnson"
    ]
  ]
}

this is my code jquery Code

for(i=0; i<= contacts.data.length; i++) {
    $.each(contacts.data[i], function( index, objValue ){
        alert("id "+objValue);
    });
}

i got the data in my objValue but i want to store separately in array of id and name it looks like this look at my code is bellow

var id=[];
var name = [];
for(i=0; i<= contacts.data.length; i++){
    $.each(contacts.data[i], function( index, objValue ) {
        id.push(objValue[index]); // This will be the value "1" from above JSON
        name.push(objValue[index]); // This will be the value "Skylar Melovia"   from above JSON
    });
}

How can i do this.

This is my json

{
  "data": [
    [
      "1",
      "Skylar Melovia"
    ],
    [
      "4",
      "Mathew Johnson"
    ]
  ]
}

this is my code jquery Code

for(i=0; i<= contacts.data.length; i++) {
    $.each(contacts.data[i], function( index, objValue ){
        alert("id "+objValue);
    });
}

i got the data in my objValue but i want to store separately in array of id and name it looks like this look at my code is bellow

var id=[];
var name = [];
for(i=0; i<= contacts.data.length; i++){
    $.each(contacts.data[i], function( index, objValue ) {
        id.push(objValue[index]); // This will be the value "1" from above JSON
        name.push(objValue[index]); // This will be the value "Skylar Melovia"   from above JSON
    });
}

How can i do this.

Share Improve this question edited May 24, 2013 at 12:24 Tyzoid 1,07813 silver badges31 bronze badges asked May 24, 2013 at 12:13 AadiAadi 1,1613 gold badges18 silver badges33 bronze badges 1
  • Its working as you want right? Define the problem better. – Grzegorz Kaczan Commented May 24, 2013 at 12:17
Add a ment  | 

3 Answers 3

Reset to default 3
 $.each(contacts.data, function( index, objValue )
 {
    id.push(objValue[0]); // This will be the value "1" from above JSON
    name.push(objValue[1]); // This will be the value "Skylar Melovia"   from above JSON

 });

Edit, alternative usage :

 $.each(contacts.data, function()
 {
    id.push(this[0]); // This will be the value "1" from above JSON
    name.push(this[1]); // This will be the value "Skylar Melovia"   from above JSON
 });

The $.each will iterate over contacts.data which is :

[
    //index 1
    [
      "1",
      "Skylar Melovia"
    ],
    //index=2
    [
      "4",
      "Mathew Johnson"
    ]

]

The anomnymous function you give with signature function(index,Objvalue) will be applied on each element with index it's index in the contact.data array and objValue its value. For index=1 you will have :

objValue=[
          "1",
          "Skylar Melovia"
        ]

Then you can access objValue[0] and objValue[1].

EDIT (in response to Dutchie432 ment and answer ;) ): Faster way to do it without jQuery, $.each is nicer to write and read but here you use plain old JS :

for(i=0; i<contacts.data.length; i++){
    ids.push(contacts.data[i][0];
    name.push(contacts.data[i][1];
}

Perhaps I am not understanding fully, but I think you are looping through the data items, and then looping through the contained values as well. I think what you want is just to loop through the data items and pull values 0 and 1 respectively.

Also, I believe you want the less than (<) operator in your loop as opposed to the less than or equal to (<=)

for(i=0; i<contacts.data.length; i++){
    ids.push(contacts.data[i][0];
    name.push(contacts.data[i][1];
}

Remove the outer for loop. $.each already iterates over the data array. data[i] is not an array, so $.each cannot iterate over it.

http://jsfiddle/ExplosionPIlls/4p5yh/

You could also use the for loop instead of $.each, but not both.

发布评论

评论列表(0)

  1. 暂无评论