最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Filter values from a json data using node.js - Stack Overflow

programmeradmin4浏览0评论

Here is the json data

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',
    }];

From this how can i filter only mode:daily, mode:weekly as below.I have to remove other values except mode:daily``mode:weekly

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

Here is the json data

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',
    }];

From this how can i filter only mode:daily, mode:weekly as below.I have to remove other values except mode:daily``mode:weekly

var myArray = [{    
        id: '45',
        mode: 'weekly',
        os: 'VM-WIN7-32'
    }, { 
        id: '67',
        mode: 'daily',
        os: 'VM-WIN7-32-1',
    }];
Share Improve this question edited Feb 28, 2014 at 10:28 Abbas 14.4k6 gold badges42 silver badges75 bronze badges asked Jan 8, 2014 at 9:09 SushSush 1,4579 gold badges27 silver badges51 bronze badges 1
  • possible duplicate of remove duplicates from json array and bine its id's using node.js – Ilan Frumer Commented Jan 8, 2014 at 9:14
Add a ment  | 

1 Answer 1

Reset to default 6

Use the Array#filter method.

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

发布评论

评论列表(0)

  1. 暂无评论