JavaScripter
JavaScripter

Reputation: 4842

filter following 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'd like to filter collection and remain 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 didn't get what I've expected.

why? I'd like result to be this:

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

}]

};

Upvotes: 3

Views: 129

Answers (2)

Evgeniy
Evgeniy

Reputation: 2921

As i remember reduce has a bit other functionality,

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

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

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

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

Upvotes: 2

Farkhat Mikhalko
Farkhat Mikhalko

Reputation: 3645

You can try by 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
    }
]

};

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 merge 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

Upvotes: 1

Related Questions