2016-03-17 36 views
-1

我有一個以.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,因爲我在上面做的。

到目前爲止,它工作正常,我需要改進我編碼的方式。

+0

應該如何,結果是什麼樣子? –

+0

@NinaScholz查看我在代碼中進行'id'比較時粘貼的代碼。 – NietzscheProgrammer

+0

一件事就是不要使用'map',你計劃使用地圖。似乎你只是想在這裏使用每個循環... – Neal

回答

1

像這樣應該工作::-)

概念 each
let json = JSON.parse(jsonData); 

json.forEach(function (itemLists) { 
    _.each(itemLists, function (items, index) { 
     var filteredItems = _.where(items, {id: id}); 
     cartTemplate.push.apply(cartTemplate, _.map(filteredItems, function(data) { 
      return '<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>'; 
     })); 
    }); 
}); 
+0

可以使用'_.each(json,function()...)'而不是'json.forEach(function()...' – Neal

+0

是的,但是我得到'_.where'函數的錯誤don' t知道爲什麼 – NietzscheProgrammer

+0

錯誤是什麼? – Neal

0

功能lodash是一樣jquery例如

我注意到,您的陣列由僅包含一個字段(enterprise)一個元素,因此您可以優化迭代:

_.each(yourArray[0].enterprise, function(data) { 
    console.log(data.id); 
}); 
相關問題