javascript - 使用 node.js 从 json 数据中过滤值

标签 javascript jquery node.js

这里是json数据

var myArray = [{  
        id: '71',   
        os: 'VM-WIN7-64'
    }, {    
        id: '45',
        mode: 'weekly',
        os: 'VM-WIN7-32'
    }, { 
        id: '37',   
        os: 'VM-WIN7-64',
    }, { 
        id: '67',
        mode: 'daily',
        os: 'VM-WIN7-32-1',
    }];

从这里我如何才能过滤 mode:daily, mode:weekly 如下。我必须删除除 mode:daily``mode 之外的其他值:每周

var myArray = [{    
        id: '45',
        mode: 'weekly',
        os: 'VM-WIN7-32'
    }, { 
        id: '67',
        mode: 'daily',
        os: 'VM-WIN7-32-1',
    }];

最佳答案

使用 Array#filter方法。

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

myArray = myArray.filter(function(o){
    return (o.mode === 'weekly' || o.mode === 'daily');
});

JSFIDDLE

关于javascript - 使用 node.js 从 json 数据中过滤值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20991169/

相关文章:

javascript - 在 for 循环中从数组中获取随机项,然后从数组中删除

javascript - 将表单数据和操作发送到 wordpress admin-ajax.php

javascript - AJAX请求附加到不同的DIV

javascript - 使用请求库将网站的 HTML 获取到变量中

javascript - Bower 管理的依赖项是否应该 checkin 存储库

javascript - 创建条形图后如何更改其背景颜色?

jQuery 的 event.stopPropagation() 导致 Rails 出现问题 :remote => true

node.js - 如何在 babel-node 调试器中获得正确的变量名称?

javascript - 如何在模态视图中访问对象

javascript在按下回车时设置一个变量值