Filter after JSON using underscore

I have a collection like this:

    var collection={
     "Today": [{
    "id": "1",
    "name": "iPod Nano",
    "image": "img/items/item1.jpg",
    "special": 0
}, {
    "id": "2",
    "name": "Samsung TV",
    "image": "img/items/item2.jpg",
    "special": 1
}],
"Monday 11 August 2014": [{
    "id": "3",
    "name": "Nikon Camera",
    "image": "img/items/item3.jpg",
    "special": 0

}, {
    "id": "4",
    "name": "iPad 1000",
    "image": "img/items/item4.jpg",
    "special": 0

}],

"Monday 30 August 2014 ": [{

    "id": "5",

    "name": "ar of Gold",
    "mage": "img / items / item5.jpg ",
    "special ": 1

}, {
    "id ": "6 ",
    " name ": "Buzz Light Year ",
    "image ": " img / items / item6.jpg ",
    "special ": 1

}]

};

I would like to filter the collection and stay only special == 1:

I tried:

 var Specials=_.reduce(collection,function(memo,item){
                var result = _.filter(item,function(c){
                    return c.special==0 ;
                });
                if(result.length > 0 ){
                    var newResult = {};
                    newResult.deals = result;
                    newResult.id = item.id;
                    memo = _.union(memo,newResult);
                }
                return memo;
            },[]); 
            var jsonText=JSON.stringify(Specials);

            console.log("=========>" + jsonText);

But did not get what I expected.

why? I would like the result to be as follows:

var filtered={
"Today": [{
    "id": "2",
    "name": "Samsung TV",
    "image": "img/items/item2.jpg",
    "special": 1
}],

"Monday 30 August 2014 ": [{

    "id": "5",

    "name": "ar of Gold",
    "mage": "img / items / item5.jpg ",
    "special ": 1

}, {
    "id ": "6 ",
    " name ": "Buzz Light Year ",
    "image ": " img / items / item6.jpg ",
    "special ": 1

}]

};
+3
source share
2 answers

As I recall, shortening has a slightly different functionality,

You can use each + reject if you need to keep the same JSON fiddle structure

_.each(collection, function(items, key){

    var filtered = _.reject(items, function(item){ 
        return item.special < 1 
    });

    if(filtered.length){
        specials[key] = filtered;
    }
});
+2
source

You can try:

   var collection = { 
  "Today": [
    {
    "id": "1",
    "name": "iPod Nano",
    "image": "img/items/item1.jpg",
     "special": 0
    }, 
    {
    "id": "2",
    "name": "Samsung TV",
    "image": "img/items/item2.jpg",
    "special": 1
    }
],

"Monday 11 August 2014": [
    {
    "id": "3",
    "name": "Nikon Camera",
    "image": "img/items/item3.jpg",
     "special": 0
    }, 
    {
    "id": "4",
    "name": "iPad 1000",
    "image": "img/items/item4.jpg",
     "special": 0
    }
]

};

for(var i in collection){
  collection[i] = _.filter(collection[i], function(item){
    return item.special == 0;
  });
}
console.log(collection);

The results will be grouped by day, if you want to combine, you must write this

var res = [];
for(var j in collection){
  for(var i = 0; i < collection[j].length; i++)
    res.push(collection[j][i]);
}

console.log(res);

And demo

+1
source

All Articles