Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
如何限制要推送到数组JqueryJs的值的数量_Jquery_Arrays - Fatal编程技术网

如何限制要推送到数组JqueryJs的值的数量

如何限制要推送到数组JqueryJs的值的数量,jquery,arrays,Jquery,Arrays,我有一个json对象,如下所示。我有一个名为category的数组,我试图将一些值放入其中。 以下是Js: var data={ products : [ { type: 'fos', name: 'retek' }, { type: 'testta', name: 'item' }, { type: 'nyedva', name: 'blabla' } ] }; var categories = []; 现在,我想将除一个值之外

我有一个json对象,如下所示。我有一个名为category的数组,我试图将一些值放入其中。 以下是Js:

var data={
    products : [
        { type: 'fos', name: 'retek' },
        { type: 'testta', name: 'item' },
        { type: 'nyedva', name: 'blabla' }
    ]
};
var categories = [];
现在,我想将除一个值之外的所有值推送到categories数组中: 以下是我尝试过的:

$.each(data.products, function(index, mon) {
    if (mon.type == 'testta') {
        //dont push the array
    } else {
        //push it to array
    }
});
但是这项工作..有什么想法可以实现吗?? 谢谢

像这样试试看

$.each(data.products, function(index, mon) {   
    if (mon.type !== 'testta') {
        categories.push(mon)
    }
});
console.log(categories);
你试过这个吗:

$.each(data.products, function(index, mon){
    if(mon.type !== 'testta'){
        categories.push(mon)
    }
});

我不知道为什么这不起作用,但通常这是一种你可以用来做的工作:

... 我不明白。“但是这项工作……”是的,这项工作会成功的<代码>类别。推送(周一)是这里唯一缺少的东西。
var categories = $.grep(data.products, function(mon) {
    return mon.type != 'testta';
});