我有一个以.json看起来像这一点,但它实际上是方式要大得多你如何用lodash /下划线来做到这一点?
[
{
"enterprise" : [
{
"id" : "743512",
"name" : "Hellen Quesada",
"role" : "P. Software Engineer",
"picture" : "http://i276.photobucket.com/albums/kk35/Sicable/200x200/06.png",
"skype" : "he.quesada",
"email" : "[email protected]",
"account" : "Digitas, Flag",
"subDept" : "Enterprise",
"location" : "Offshore: Costa Rica"
},
{
"id" : "743587",
"name" : "Jonathan Chavez",
"role" : "P. Creative Engineer",
"picture" : "http://i276.photobucket.com/albums/kk35/Sicable/200x200/06.png",
"skype" : "jonathanchavesb",
"email" : "[email protected]",
"account" : "Digitas, Flag",
"subDept" : "Enterprise",
"location" : "Offshore: Costa Rica"
},
{
"id" : "749058",
"name" : "Rodrigo Ovares",
"role" : "Creative Engineer",
"picture" : "http://i276.photobucket.com/albums/kk35/Sicable/200x200/06.png",
"skype" : "rodrigo.ovares.arroyo",
"email" : "[email protected]",
"account" : "Digitas, Flag",
"subDept" : "Enterprise",
"location" : "Offshore: Costa Rica"
},
{
"id" : "750684",
"name" : "Juan Morera",
"role" : "Software Engineer",
"picture" : "http://i276.photobucket.com/albums/kk35/Sicable/200x200/06.png",
"skype" : "morerajuan",
"email" : "[email protected]",
"account" : "Digitas, Flag",
"subDept" : "Enterprise",
"location" : "Offshore: Costa Rica"
}
]
,我遍历像这样
$.map(JSON.parse(getData), function(items) {
$.map(items, function(item) {
$.map(item, function(data) {
if (id === data.id) {
// SOMETHING HAPPENS HERE
cardTemplate.push('<li><span class="name-role" id="name">' +
'<span><img class="card-picture" src="'+ data.picture +'"/></span>' +
'<div class="main-info-card">' +
data.name + '<br>' +
data.role + '<br>' +
'Employee N.' + data.id +
'</div>' +
'</span></li>' +
'<li><p>Email: <strong>'+ data.email +'</strong></p></li>' +
'<li><p>Skype: <strong>'+ data.skype +'</strong></p></li>' +
'<li><p class="team"> '+ data.account +' <br> <strong>Enterprise</strong></p></li>' +
'<li><strong> '+ data.location +'</strong></li>');
}
});
});
});
一个朋友就问我使用lodash和我我正在阅读一些像map,forEach,each等一些概念,但我想知道如何用更少的嵌套来改进上面的代码,比如_.flatten或_.deepFlatten,但是我遇到的问题是我没有获取这个概念很好。
你能帮我给我一个例子或更好的解决方案,以避免使用$ .map,因为我在上面做的。
到目前为止,它工作正常,我需要改进我编码的方式。
应该如何,结果是什么样子? –
@NinaScholz查看我在代码中进行'id'比较时粘贴的代码。 – NietzscheProgrammer
一件事就是不要使用'map',你计划使用地图。似乎你只是想在这里使用每个循环... – Neal