Created
April 16, 2017 17:22
-
-
Save mzdv/3c8e721435b90bb5df80a7a91e262367 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var x = [{ | |
id: 1, | |
data: 'ab', | |
ordering: 1 | |
}, { | |
id: 2, | |
data: 'cd', | |
ordering: 1 | |
}]; | |
var y = [{ | |
id: 1, | |
data: 'hello', | |
ordering: 2 | |
}, { | |
id: 2, | |
data: 'cd', | |
ordering: 1 | |
}, { | |
id: 3, | |
data: 'pera', | |
ordering: 1 | |
}, { | |
id: 4, | |
data: 'jovica', | |
ordering: 1 | |
}]; | |
if (x.length > y.length) { | |
longer = x; | |
shorter = y; | |
} else { | |
longer = y; | |
shorter = x; | |
} | |
var final = []; | |
var longerIds = longer.map((e) => { | |
return e.id; | |
}); | |
var shorterIds = shorter.map((e) => { | |
return e.id; | |
}); | |
var diffed = _.difference(longerIds, shorterIds); | |
var equalized = _.intersection(longerIds, shorterIds); | |
if (diffed.length > 0) { | |
longer.forEach((e, i) => { | |
if (diffed.indexOf(e.id) > -1) { | |
final.push(e); | |
}; | |
}); | |
} | |
longer.forEach((e) => { | |
if (equalized.indexOf(e.id) > -1) { | |
if (e.ordering > _.find(shorter, { 'id': e.id }).ordering) { | |
final.push(e); | |
} else { | |
final.push(second); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment