2014-05-18 85 views
0

我想知道在我JSON數據JSON數據獲取的公共數據列表:jQuery的或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"}] 

多少網站,我想輸出像

var webList="google,yahoo,ebay,rediff"; 

OR

webList[0]="google"; 
webList[1]="yahoo"; 
webList[2]="ebay"; 
webList[3]="rediff"; 

回答

2

如果您需要刪除重複的檢查推前陣與$.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); 

Live demo

2

這是你想要的:

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]); 

Fiddle DEMO

+0

伊赫桑我想一個網站的名稱只有一次......不重複..your控制檯輸出:谷歌,谷歌,雅虎,谷歌,易趣,ebay,rediff,yahoo – Me7888

+0

你必須從數組結尾過濾 –

+0

看到edit和fiddle也更新了 –

1

你可以這樣來做:

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]);  
}