Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.