2016-06-23 5 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' работает для меня, thx – Meldum

+0

' _.MMM() 'не работает, когда первая переменная является первым аргументом, а первая переменная является вторым аргументом. – 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>