2015-04-17 34 views
0

這是一個javascript對象。
我怎樣才能得到條件的元素數gallery =「摘要」gallery =「遊戲」獲取javascript對象中元素的數量,其中key = option

window.paintings = { 
    1: { id: 1, name: 'Abstract 1', gallery: 'Abstract', src:'Image64.jpg' }, 
    2: { id: 2, name: 'Abstract 2', gallery: 'Abstract', src:'Image65.jpg' }, 
    3: { id: 3, name: 'Abstract 3', gallery: 'Abstract', src:'Image66.jpg' }, 
    4: { id: 1, name: 'Game 1', gallery: 'Game', src:'Image66.jpg' }, 
    5: { id: 2, name: 'Game 2', gallery: 'Game', src:'Image66.jpg' }, 
}; 
+1

這是一個JavaScript ** **對象,而不是陣列。 – VisioN

+0

它是一個JavaScript對象而不是數組。 –

+0

哦!我爲此感到抱歉。謝謝@VisioN和Vigneswaran Marimuthu –

回答

2

如果你想遍歷對象:

p = { 
    1: { id: 1, name: 'Abstract 1', gallery: 'Abstract', src:'Image64.jpg' }, 
    2: { id: 2, name: 'Abstract 2', gallery: 'Abstract', src:'Image65.jpg' }, 
    3: { id: 3, name: 'Abstract 3', gallery: 'Abstract', src:'Image66.jpg' }, 
    4: { id: 1, name: 'Game 1', gallery: 'Gme', src:'Image66.jpg' }, 
    5: { id: 2, name: 'Game 2', gallery: 'Game', src:'Image66.jpg' }, 
}; 
var count = 0; 
for (var key in p) { 
    if (p.hasOwnProperty(key)) { 
    if(p[key].gallery === "Abstract" || p[key].gallery === "Game") 
     count++; 
    } 
} 
-1

for循環使用,以迭代這個數組...

編輯:但我注意到,這不是一個數組,但它是一個Javascript對象。數組是可識別的,因爲括號([])是直接的,而它包含大括號({}) 因此,下面的代碼片段不適用於此。

var count = 0; 

for (var i = 0; i < window.paintings.length; i++) { 
    var item = window.paintings[i]; 
    if (...) { 
    count = count + 1; 
    } 
} 

像這樣的東西應該工作:

for (var property in object) { 
    if (object.hasOwnProperty(property)) { 
     // do your logic here 
    } 
} 
+0

請檢查問題。它是JavaScript對象。 –

+0

感謝您指出了這一點。我已經更新了我的答案 –

1

只是一個樣本輸出。根據您的要求修改它。

var paintings = { 
 
    1: { id: 1, name: 'Abstract 1', gallery: 'Abstract', src:'Image64.jpg' }, 
 
    2: { id: 2, name: 'Abstract 2', gallery: 'Abstract', src:'Image65.jpg' }, 
 
    3: { id: 3, name: 'Abstract 3', gallery: 'Abstract', src:'Image66.jpg' }, 
 
    4: { id: 1, name: 'Game 1', gallery: 'Game', src:'Image66.jpg' }, 
 
    5: { id: 2, name: 'Game 2', gallery: 'Game', src:'Image66.jpg' }, 
 
}; 
 

 
var count = 0; 
 
for (var property in paintings) { 
 

 
    if (paintings.hasOwnProperty(property)) { 
 
     
 
     if (paintings[property]['gallery'] == 'Abstract' || paintings[property]['gallery'] == 'Game') { 
 
     count++; 
 
     } 
 
     
 
    } 
 

 
} 
 

 
alert(count);

0

試試這個:

var AbstractCount=0; 
var GameCount=0; 
window.paintings = { 
    1: { id: 1, name: 'Abstract 1', gallery: 'Abstract', src:'Image64.jpg' }, 
    2: { id: 2, name: 'Abstract 2', gallery: 'Abstract', src:'Image65.jpg' }, 
    3: { id: 3, name: 'Abstract 3', gallery: 'Abstract', src:'Image66.jpg' }, 
    4: { id: 1, name: 'Game 1', gallery: 'Game', src:'Image66.jpg' }, 
    5: { id: 2, name: 'Game 2', gallery: 'Game', src:'Image66.jpg' }, 
}; 
for(keys in window.paintings){ 
    if(window.paintings[keys].gallery == 'Abstract'){ 
     AbstractCount++; 
    } 
    else if(window.paintings[keys].gallery == 'Game'){ 
     GameCount++;  
    } 
} 
alert(AbstractCount); 
alert(GameCount); 
0

的最直接方式是使用原生的功能:

// This is a reusable function to count elements in object 
// based on some function 
var countElements = function(obj, condition) { 
    return Object.keys(obj).filter(function(key) { 
     if (obj.hasOwnProperty(key)) { 
      var item = obj[key]; 
      return condition(obj[key]); 
     } 
    }).length; 
}; 

var count = countElements(window.paintings, function(item) { 
    // Return true from here if you want to count particular element 
    return item.gallery == 'Abstract' || item.foo == 'Game'; // you can add more conditions here. 
}); 
alert(count); // Alerts "3" 
0

嘗試使用jquery grep。

把你的對象改成一個數組,就像這樣。

var arr = [ 
    { id: 1, name: 'Abstract 1', gallery: 'Abstract', src:'Image64.jpg' }, 
    { id: 2, name: 'Abstract 2', gallery: 'Abstract', src:'Image65.jpg' }, 
    { id: 3, name: 'Abstract 3', gallery: 'Abstract', src:'Image66.jpg' }, 
    { id: 1, name: 'Game 1', gallery: 'Game', src:'Image66.jpg' }, 
    { id: 2, name: 'Game 2', gallery: 'Game', src:'Image66.jpg' } 
]; 


var x = $.grep(arr, function(n, i) { 
    return (n.gallery == 'Abstract' || n.gallery == 'Game'); 
}); 


$("#result").html(x.length); 

Jsfiddle here