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

遍历json数据,对同一属性数据进行分组?

遍历json数据,对同一属性数据进行分组?

HUX布斯 2019-04-13 08:36:20
{"module_list":[{"content":{"module-type":"list-1","module":"list","type":"1","name":"职位列表","logo1":"imgurl1","title1":"baidu1","desc1":"hahahha1","url1":"www.baidu.com1","logo2":"imgurl2","title2":"baidu2","desc2":"hahahha2","url2":"www.baidu.com2","logo3":"imgurl3","title3":"baidu3","desc3":"hahahha3","url3":"www.baidu.com3","logo4":"","title4":"","desc4":"","url4":"","jobs":[{"name":"产品经理","salary":"10k-15k","url":"www.baidu.com","desc":"广州","mark":"group:1"},{"name":"产品经理","salary":"10k-15k","url":"www.baidu.com","desc":"广州","mark":"group:2"},{"name":"产品经理","salary":"10k-15k","url":"www.baidu.com","desc":"广州","mark":"group:3"},{"name":"产品经理","salary":"10k-15k","url":"www.baidu.com","desc":"广州","mark":"group:4"},{"name":"产品经理","salary":"10k-15k","url":"www.baidu.com","desc":"广州","mark":"group:1"},{"name":"产品经理","salary":"10k-15k","url":"www.baidu.com","desc":"广州","mark":"group:1"},{"name":"产品经理","salary":"10k-15k","url":"www.baidu.com","desc":"广州","mark":"group:4"},{"name":"产品经理","salary":"10k-15k","url":"www.baidu.com","desc":"广州","mark":"group:4"},{"name":"产品经理","salary":"10k-15k","url":"www.baidu.com","desc":"广州","mark":"group:3"},{"name":"产品经理","salary":"10k-15k","url":"www.baidu.com","desc":"广州","mark":"group:2"},{"name":"产品经理","salary":"10k-15k","url":"www.baidu.com","desc":"广州","mark":"group:2"}]},}]}json存储了一些职位信息,怎么根据mark的值,对这些信息进行分组,例如mark值为group:1的放到一个分组里,,mark值为group:2的放到一个分组里,然后在前端显示在不同模块里。该怎么对数据进行遍历?遍历之后怎么根据不同的mark值放到不同的数组里呢publicstaticfunctionbuildList($module){$jobList2="";if($module['jobs']){foreach($module['jobs']as$job){$jobList2.=
查看完整描述

2 回答

?
慕哥6287543

TA贡献1831条经验 获得超10个赞

varmap={};
for(vari=0,l=jobs.length;ivarkey=jobs[i]['mark'];
map[key]=map[key]||(map[key]=[]);
map[key].push(jobs[i]);
}
returnmap;
这样返回的map就是一个key,value
{
group1:[job1,job2],
group2:[job3,job4]
}
应该就是你想要的
                            
查看完整回答
反对 回复 2019-04-13
?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

循环jobs数组:
varjobsSortObject={};
for(vari=0;ivarjob=jobs[i],
mark=job.mark,
jobItem=jobsSortObject[mark];
if(jobItem){
jobsSortObject[mark].push(job);
}else{
jobsSortObject[mark]=[job];
}
}
jobsSortObject就会是这个样子:
{
'group:1':[{},{},{}],
'group:2':[{}]
}
                            
查看完整回答
反对 回复 2019-04-13
  • 2 回答
  • 0 关注
  • 262 浏览
慕课专栏
更多

添加回答

举报

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