Reputation: 1199
I want to know how many website in MY JSON Data
[{"name":"Lenovo Thinkpad 41A4298","website":"google"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"},
{"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"},
{"name":"Lenovo Thinkpad 41A424448","website":"google"},
{"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"},
{"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}]
I want to output like
var webList="google,yahoo,ebay,rediff";
OR
webList[0]="google";
webList[1]="yahoo";
webList[2]="ebay";
webList[3]="rediff";
Upvotes: 0
Views: 205
Reputation: 62488
Here is what you want:
var data=[{"name":"Lenovo Thinkpad 41A4298","website":"google"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"},
{"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"},
{"name":"Lenovo Thinkpad 41A424448","website":"google"},
{"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"},
{"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}];
var webList = new Array();
$.each(data,function(index,item){
if($.inArray(item.website, webList) == -1)
webList.push(item.website);
console.log(webList);
})
you can access this way the item you want or iterate like i did on json array:
console.log(webList[0]);
Upvotes: 2
Reputation: 2303
You could do it this way:
http://jsfiddle.net/DianaNassar/C97DJ/1/
var data = [{"name":"Lenovo Thinkpad 41A4298","website":"google"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"},
{"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"},
{"name":"Lenovo Thinkpad 41A424448","website":"google"},
{"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"},
{"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}];
var uniqueNames = [];
for(i = 0; i< data.length; i++){
if(uniqueNames.indexOf(data[i].website) === -1){
uniqueNames.push(data[i].website);
}
}
for(i = 0; i< uniqueNames.length; i++){
alert(uniqueNames[i]);
}
Upvotes: 1
Reputation: 4076
If you need remove the duplicate check the array before push with $.inArray:
var data=[{"name":"Lenovo Thinkpad 41A4298","website":"google"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"},
{"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"},
{"name":"Lenovo Thinkpad 41A424448","website":"google"},
{"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"},
{"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}];
var webList = new Array();
$.each(data,function(index,item){
if ($.inArray(item.website, webList)==-1) {
webList.push(item.website);
}
});
console.log(webList);
Upvotes: 2