2016-09-28 70 views
1

我想使用underscoreJs操縱JavaScript對象,並有問題這樣做。深入採摘使用Underscore.JS

這是我的例子

var data = { 
 
    "label": "SomeName", 
 
    "parent": [{ 
 
    "id": "parentId", 
 
    "resources": [{ 
 
     "name": "ID1NAME", 
 
     "calls": [ 
 
     "user_get", "user2_post", "user3_delete" 
 
     ] 
 
    }, { 
 
     "name": "ID2", 
 
     "calls": [ 
 
     "employee1_get", "employee2_delete", "employee3_update" 
 
     ] 
 
    }] 
 
    }] 
 
}; 
 
var res = _(data).chain(). 
 
    pluck('parent'). 
 
    flatten(). 
 
    findWhere(function(item){ 
 
    item === "user_get" 
 
    }). 
 
    value(); 
 
    
 
console.log(res);

使用一個元件,其是data.parent.calls[]的一部分(例如: 「user_get」)我想以提取它的父對象,即data.parent[0]

我上面試過但總是弄不明白。我很感激這方面的幫助。

+0

你有沒有機會將JSON包裝到數組中?由於Underscore正在使用集合(它包含一個包含所有類型對象的數組),因此將JSON寫入數組可以解決您的問題。 https://codepen.io/anon/pen/WGroRm?editors=0011 – Aer0

+0

@torazburo感謝您的建議。我確定知道解析和JSON的含義。我可能錯誤地使用瞭解析這個詞,但是我打算將它稱爲JSON文檔是正確的,因爲我一直在處理JSON文檔。我通過跳過將JSON文檔解析爲Javascript對象的部分給出了一個示例。 此外,在適當的尊重,操縱你使用的詞是不正確的,提取會很好。我尊重他人:) –

回答

1

一個你遇到的問題是你的_.pluck使用。如果在對象上執行_.pluck,它將檢查對象的鍵,試圖檢索您指定的屬性作爲第二個參數(在此例中爲的父')。 'label'是一個字符串,'父'是一個數組,因此您得到的數組結果爲[undefined, undefined]。其餘的則會出錯。

一種解決方案可以如下:

function findCallIndexInParent(call, parent) { 
    return _.chain(parent) 
      .pluck('resources') 
      .flatten() 
      .findIndex(function (obj) { 
       return _.contains(obj.calls, call); 
      }) 
      .value(); 
} 

function findCall(call, data) { 
    var parent = data.parent; 
    return parent[findCallIndexInParent(call, parent)]; 
} 

console.log(findCall('user_get', data)); 

findCall僅僅是將數據的父屬性傳遞給findCallIndexInParent(將檢索其中呼叫是索引)的便利方法和返回所需對象與父類數組。

Lodash(下劃線的一個分支)提供了一種方法來獲得一個對象的屬性,在這裏真的很方便(可惜,下劃線沒有)。

findCallIndexInParent的解釋如下:

  1. 鏈父列表
  2. 摘去資源陣列
  3. 作爲拔毛地圖,它返回所以需要一個扁平化列表的列表。
  4. 查找其調用包含包含呼叫內的對象的呼叫
  5. 返回的值(指數)的元素的索引。

這是fiddle。希望能幫助到你。

+0

感謝您的明確解釋。感謝你的時間和幫助。 –

1

這似乎是個伎倆。

function findByCall(data, call) { 
 
    return _.find(data.parent, function(parent) {   //From data.parent list, find an item that 
 
    return _.some(parent.resources, function(resource) {//has such parent.resource that it 
 
     return _.includes(resource.calls, call);   //includes the searched resource.calls item 
 
    }); 
 
    }); 
 
} 
 

 
//Test 
 

 
var data = { 
 
    "label": "SomeName", 
 
    "parent": [{ 
 
    "id": "parentId", 
 
    "resources": [{ 
 
     "name": "ID1NAME", 
 
     "calls": [ 
 
     "user_get", "user2_post", "user3_delete" 
 
     ] 
 
    }, { 
 
     "name": "ID2", 
 
     "calls": [ 
 
     "employee1_get", "employee2_delete", "employee3_update" 
 
     ] 
 
    }] 
 
    }] 
 
}; 
 

 
console.log(findByCall(data, 'user_get'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>

+0

我感謝你的幫助。 –

1

如果我理解正確,您想獲取parent數組中具有指定調用資源的元素的索引。

data = { 
 
    "label": "SomeName", 
 
    "parent": [{ 
 
    "id": "parentId", 
 
    "resources": [{ 
 
     "name": "ID1NAME", 
 
     "calls": [ 
 
     "user_get", "user2_post", "user3_delete" 
 
     ] 
 
    }, { 
 
     "name": "ID2", 
 
     "calls": [ 
 
     "employee1_get", "employee2_delete", "employee3_update" 
 
     ] 
 
    }] 
 
    }] 
 
} 
 

 
// find the index of a parent 
 
const index = _.findIndex(data.parent, parent => 
 
    // that has any (some) resources 
 
    _.some(parent.resources, resource => 
 
     // that contains 'user_get' call in its calls list 
 
     _.contains(resource.calls, 'user_get') 
 
    ) 
 
) 
 

 
console.log(index) // 0
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

如果你想找到實際的父對象,如果你想找到匹配這個調用所有父對象使用find代替findIndex

,用filter代替findIndex