2017-03-07 12 views
0

私は2つのオブジェクトリテラルをそれぞれのプロパティから区別する方法を見つけようとしています。 私は、しかし、これは理想的なようではありません2 ._forEach()Lodash differenceBy alternative

_.forEach(forms, form => { 
    _.forEach(this.sections, section => { 
     if(form.section === section.name){ 
      section.inProgress = true; 
     } 
    }); 
}); 

を使用してこの作業を取得することができます。私は_.differenceBy

_.differenceBy(forms, this.sections, 'x'); 

で試してみましたが、私はアレイの1つおよびその他におけるsectionnameであることで確認したい「名前」としてプロパティ名によって区別傾けます。

私はそれがこのためにlodashで何かを

_.differenceBy(forms, this.sections, 'section' === 'name'); 

のようなものを使用したいですか?

+0

あなたは一つの完全な入力と出力のフォーマットをお願いできますか?それは非常に混乱しています – RaR

+0

@RaR何が混乱している..私はちょうど2つのオブジェクトリテラルを別のオブジェクトのプロパティ(セクション)とは異なるプロパティ(名前) –

答えて

1

あなたは、各アレイ内の右側のオブジェクトのプロパティを参照するコンパレータと_.differenceWith()を使用することができます。

var forms = [{ 'name': 'somethingA' }, { 'name': 'somethingB' }]; 
 
var sections = [{ section: 'somethingB' }]; 
 

 
var result = _.differenceWith(forms, sections, function(arrValue, othValue) { 
 
    return arrValue.name === othValue.section; 
 
}); 
 

 
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>