메모장



underscore difference between two objects javascript



<script type="text/javascript" src="//s.zigbang.com/js/underscore/underscore.min.js"></script>
<script>
var people = [
{
name: 'Dave',
age: 26
},
{
name: 'Frank',
age: 23
}
];

var people1 = [
{
name: 'Dave',
age: 26
},
{
name: 'Frank',
age: 23
},
{
name: 'Frank1111',
age: 29
}
];

//_.differenceWith(people, people1, _.isEqual);

var difference = function(array,key){
var rest = Array.prototype.concat.apply(Array.prototype, Array.prototype.slice.call(arguments, 1));

var containsEquals = function(obj, target) {
if (obj == null) return false;
return _.any(obj, function(value) {
return _.isEqual(value[rest[2]], target[rest[2]]);
});
};

return _.filter(array, function(value){
return ! containsEquals(rest, value);
});
};

console.log(difference(people1, people,'age'));
</script>

덧글

댓글 입력 영역