Skip to content

Instantly share code, notes, and snippets.

@mzdv
Created April 16, 2017 17:22
Show Gist options
  • Save mzdv/3c8e721435b90bb5df80a7a91e262367 to your computer and use it in GitHub Desktop.
Save mzdv/3c8e721435b90bb5df80a7a91e262367 to your computer and use it in GitHub Desktop.
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