3 回答
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); // 你要的结果
`
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;
})
}
添加回答
举报