为了账号安全,请及时绑定邮箱和手机立即绑定

JS过滤后台获取的原始数据

JS过滤后台获取的原始数据

holdtom 2019-02-28 18:31:01
后台获取到的ajax的JSON数组,原始数据 [{"CostTime": "310", "FromStation": "上海"},{"CostTime": "336", "FromStation": "北京"},{"CostTime": "310", "FromStation": "上海"},{"CostTime": "336", "FromStation": "北京"},{"CostTime": "310", "FromStation": "上海"},{"CostTime": "336", "FromStation": "北京"},{"CostTime": "310", "FromStation": "上海"},{"CostTime": "336", "FromStation": "北京"},{"CostTime": "310", "FromStation": "上海"},{"CostTime": "310", "FromStation": "上海"},{"CostTime": "336", "FromStation": "北京"}]把FromStation为北京的数据过滤后生成新的数据[{"CostTime": "336", "FromStation": "北京"},{"CostTime": "336", "FromStation": "北京"},{"CostTime": "336", "FromStation": "北京"},{"CostTime": "336", "FromStation": "北京"},{"CostTime": "336", "FromStation": "北京"}]用JS或者JQ过滤具体方式是怎么样的。谢谢各位大牛指点下!
查看完整描述

3 回答

?
慕田峪7331174

TA贡献1828条经验 获得超13个赞

nArr = json.filter((item) => {

   return item.FromStation == '北京'

})


查看完整回答
反对 回复 2019-03-05
?
慕姐8265434

TA贡献1813条经验 获得超2个赞

`var sourceData = [

{"CostTime": "310", "FromStation": "上海"},

{"CostTime": "336", "FromStation": "北京"},

{"CostTime": "310", "FromStation": "上海"},

{"CostTime": "336", "FromStation": "北京"},

{"CostTime": "310", "FromStation": "上海"},

{"CostTime": "336", "FromStation": "北京"},

{"CostTime": "310", "FromStation": "上海"},

{"CostTime": "336", "FromStation": "北京"},

{"CostTime": "310", "FromStation": "上海"},

{"CostTime": "310", "FromStation": "上海"},

{"CostTime": "336", "FromStation": "北京"}

];

var currentData = sourceData.filter(x => {


return x.FromStation === '北京';

});

console.log(currentData); // 你要的结果

`


查看完整回答
反对 回复 2019-03-05
?
神不在的星期二

TA贡献1963条经验 获得超6个赞

es6采用楼上的方法;

es5写法:


nArr = json.filter(function(item){

    return item.FromStation === '北京';

});

不过建议做下简单的方法封装,将过滤关键词作为变量传入方便随时更换关键词


function filterKey(key){

    return json.filter(function(item){

        return item.FromStation === key;

    })

}


查看完整回答
反对 回复 2019-03-05
  • 3 回答
  • 0 关注
  • 865 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信