Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用下划线过滤JSON后面的内容_Javascript_Backbone.js_Underscore.js - Fatal编程技术网

Javascript 使用下划线过滤JSON后面的内容

Javascript 使用下划线过滤JSON后面的内容,javascript,backbone.js,underscore.js,Javascript,Backbone.js,Underscore.js,我有这样的收藏: 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

我有这样的收藏:

    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

}]

};
我希望筛选集合并仅保留特殊==1:

我试过:

 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);
但是没有得到我所期望的

为什么??我希望结果是:

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

}]

};
您可以通过以下方式进行尝试:

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

结果将按天分组,如果要合并,必须编写以下内容

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

console.log(res);
var res=[];
for(集合中的var j){
对于(var i=0;i

而且

我记得reduce还有一些其他功能

如果需要保持相同的JSON结构,则可以使用每个+拒绝

\每个(集合、功能(项、键){
var filtered=\拒绝(项,函数(项){
返回项目。特殊<1
});
如果(过滤的长度){
特价[键]=已过滤;
}
});

你的json无效)你说你想在
c.special==1上过滤,但你的代码在
c.special==0上过滤。。。哪一个是正确的?以下是我得到的:{“今天”:[],“2014年8月11日星期一”:[],“2014年8月30日星期一”:[]如果现在有必需的对象,Filter函数将返回空数组。谢谢。fuserthrowerori think return item.special==1;as I need==1 filtered.如果没有找到数据,是否可以删除空标记,例如,如果今天没有特殊,不在结果JSON中显示今天如果我只保留包含特殊的日期会怎么样。并删除所有其他没有特殊标记的日期标记。我已经在fiddle中更新了代码。或者您可以使用@undefinedHi提供的代码,这正是我所期望的。谢谢我已经学习了如何通过代码生成新的JSON数据。
var res = [];
for(var j in collection){
  for(var i = 0; i < collection[j].length; i++)
    res.push(collection[j][i]);
}

console.log(res);
_.each(collection, function(items, key){

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

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