我有一個以.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