Skip to content Skip to sidebar Skip to footer

Get Count Of Elements In Javascript Object Where Key=option

This is a javascript object. How can I get the count of elements with condition gallery='Abstract' and gallery='Game'. window.paintings = { 1: { id: 1, name: 'Abstract 1', gall

Solution 1:

If you want to iterate over the object:

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++;
  }
}

Solution 2:

Just a sample output. Modify it as per your requirement.

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

Solution 3:

Try this:

varAbstractCount=0;
varGameCount=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 inwindow.paintings){
    if(window.paintings[keys].gallery == 'Abstract'){
      AbstractCount++;
    }
    elseif(window.paintings[keys].gallery == 'Game'){
        GameCount++;    
    }
}
alert(AbstractCount);
alert(GameCount);

Solution 4:

The most straightforward way is using native functions:

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

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

Solution 5:

Try use jquery grep.

Change your object to an array do it, like this.

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

Post a Comment for "Get Count Of Elements In Javascript Object Where Key=option"