首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSON数据获取公共数据列表: Jquery或Javascript

JSON数据获取公共数据列表: Jquery或Javascript
EN

Stack Overflow用户
提问于 2014-05-18 13:47:52
回答 3查看 195关注 0票数 0

我想知道我的JSON数据中有多少网站

代码语言:javascript
复制
[{"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"}]

我想输出像

代码语言:javascript
复制
var webList="google,yahoo,ebay,rediff";

代码语言:javascript
复制
webList[0]="google";
webList[1]="yahoo";
webList[2]="ebay";
webList[3]="rediff";
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-05-18 14:01:38

如果需要删除重复的内容,请在使用$.inArray推送前检查该数组

代码语言:javascript
复制
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);

现场演示

票数 2
EN

Stack Overflow用户

发布于 2014-05-18 13:52:12

这是你想要的:

代码语言:javascript
复制
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数组上所做的那样进行迭代:

代码语言:javascript
复制
console.log(webList[0]);

小提琴演示

票数 2
EN

Stack Overflow用户

发布于 2014-05-18 14:09:50

你可以这样做:

http://jsfiddle.net/DianaNassar/C97DJ/1/

代码语言:javascript
复制
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]);      
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23722614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档