2016-06-23 11 views
0
私はlodashを経由して、これを作りたい

比較:lodash:二つのオブジェクト

first = { one: 1, two: 2, three: 3 } 

second = { three: 3, one: 1 } 

_.CustomisEqual(first, second); 
// → true 

third = { one: 1, two: 2, three: 3 } 

fourth = { three: 4, one: 1 } 

_.CustomisEqual(third, fourth); 
// → false 

しかし、通常の_.isEqual比較のこのメソッドをサポートしない、lodashを経由してこの比較を行うための方法はありますか?

+2

あなたはそのように動作します関数を記述する方法を求めていますか?何を試しましたか?あなたは['isMatch'](https://lodash.com/docs#isMatch)関数をチェックしましたか? –

+0

確かに、 'isMatch'は私のために働いています。 – Meldum

+0

" second "変数が最初の引数で、" first "変数が2番目の引数である場合、' _.isMatch() 'は動作しません。 – ryeballar

答えて

1

function CustomisEqual(objOne, objTwo) { 
 
    return !!_([objOne]).filter(objTwo).size(); 
 
} 
 

 
first = { one: 1, two: 2, three: 3 } 
 
second = { three: 3, one: 1 } 
 

 
var resOne = CustomisEqual(first, second); 
 

 
console.log('resOne ', resOne); 
 
// → true 
 

 
third = { one: 1, two: 2, three: 3 } 
 
fourth = { three: 4, one: 1 } 
 

 
var resTwo = CustomisEqual(third, fourth); 
 

 
console.log('resTwo ', resTwo); 
 
// → false
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js"></script>

0

基本的にこの:

// mock lodash 
 
var _ = {} 
 

 
// create custom lodash method 
 
_.CustomisEqual = function (a, b) { 
 
    
 
    // get array of the keys in object 
 
    var aKeys = Object.keys(a) 
 
    var bKeys = Object.keys(b) 
 
    // get the array of the keys with the smallest length 
 
    var minKey = (aKeys.length > bKeys.length ? bKeys : aKeys) 
 
    // get the obejct with the smallest and greatest length 
 
    var minObj = (aKeys.length > bKeys.length ? b : a) 
 
    var maxObj = (a === minObj ? b : a) 
 
    // get the length from the smallest length array of the keys 
 
    var minLen = Math.min(aKeys.length, bKeys.length) 
 
    
 
    // iterate through the smallest object 
 
    for (var i = 0; i < minLen; i++) { 
 
    var currentKey = minKey[i] 
 
    // ignore values that are in one but not the other 
 
    if (maxObj[currentKey] !== undefined && 
 
     // compare defined values of both objects 
 
     maxObj[currentKey] !== minObj[currentKey]) { 
 
     // return false if they don't match 
 
     return false 
 
    } 
 
    } 
 
    
 
    // otherwise, they do match, so return true 
 
    return true 
 
} 
 

 
var first = { one: 1, two: 2, three: 3 } 
 
var second = { three: 3, one: 1 } 
 

 
console.log(_.CustomisEqual(first, second)) // → true 
 

 
var third = { one: 1, two: 2, three: 4 } 
 
var fourth = { three: 3, one: 1 } 
 

 
console.log(_.CustomisEqual(third, fourth)) // → false

私はそれが役に立てば幸い!

0

私は解決策を下回る役に立つかもしれないと思います。

var first = { one: 1, two: 2, three: 3 }; 
 
var second = { three: 3, one: 1 }; 
 
var third = { one: 1, two: 2, three: 3 }; 
 
var fourth = { three: 4, one: 1 }; 
 

 
function customIsEquals(first,second) { 
 
    var ret = true; 
 
    _.forEach(second,function(value, key){ 
 
     if(first[key]!==value){ 
 
     ret = false; 
 
     } 
 
    }); 
 
    return ret; 
 
} 
 

 
_.mixin({ 'customIsEquals': customIsEquals }); 
 

 
console.log(_.customIsEquals(first,second)); 
 
console.log(_.customIsEquals(third,fourth));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>

関連する問題