我想知道我的JSON数据中有多少网站
[{"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="google,yahoo,ebay,rediff";或
webList[0]="google";
webList[1]="yahoo";
webList[2]="ebay";
webList[3]="rediff";发布于 2014-05-18 14:01:38
如果需要删除重复的内容,请在使用$.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);现场演示
发布于 2014-05-18 13:52:12
这是你想要的:
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);
})您可以通过这种方式访问您想要的项,或者像我在json数组上所做的那样进行迭代:
console.log(webList[0]);小提琴演示
发布于 2014-05-18 14:09:50
你可以这样做:
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]);
}https://stackoverflow.com/questions/23722614
复制相似问题