I haven't mastered JavaScript and I want to do the same thing that is in this question (How to merge two arrays in Javascript and de-duplicate items) but with arrays of objects and filtering duplicates based on a unique ID attribute.
I already did it with one of the answers of that question but it is inefficient.
Here is my solution:
var mezclaArreglo = function (array) {
var a = array.concat();
for(var i=0; i<a.length; ++i) {
for(var j=i+1; j<a.length; ++j) {
if(a[i].hid === a[j].hid)
a.splice(j--, 1);
}
}
return a;
};
var old =
[{ hid: 'MTQzNTc1OTcyMzk1ODI3OTMyMjI3NDcyNzc0Njg0NDI5',
number: '1',
payload: { style: 'WebView', type: 'type1' }},
{ hid: 'MTQzNTc1OTczMDA1MDgwMTcwNzg3NjM4MDUzMjk3OTk3',
number: '2',
payload: { style: 'WebView', type: 'type1' }},
{ hid: 'MTQzNTc1OTczNDMxNzQ1NDI2NzUwOTA0ODgwNDY0MDc3',
number: '3',
payload: { style: 'WebView', type: 'type1' }}
];
var newA =
[{ hid: 'MTQzNTc1OTczNDMxNzQ1NDI2NzUwOTA0ODgwNDY0MDc3',
number: '3',
payload: { style: false, type: false }},
{ hid: 'MTQzNTc1OTc0NzcxNDM1MDUyMzA5MzQ4MjQ3OTQ1MzA5',
number: '4',
payload: { style: 'WebView', type: 'type1' }}
];
// RESULT ARRAY
[{ hid: 'MTQzNTc1OTcyMzk1ODI3OTMyMjI3NDcyNzc0Njg0NDI5',
number: '1',
payload: { style: 'WebView', type: 'type1' }},
{ hid: 'MTQzNTc1OTczMDA1MDgwMTcwNzg3NjM4MDUzMjk3OTk3',
number: '2',
payload: { style: 'WebView', type: 'type1' }},
{ hid: 'MTQzNTc1OTczNDMxNzQ1NDI2NzUwOTA0ODgwNDY0MDc3',
number: '3',
payload: { style: 'WebView', type: 'type1' }},
{ hid: 'MTQzNTc1OTc0NzcxNDM1MDUyMzA5MzQ4MjQ3OTQ1MzA5',
number: '4',
payload: { style: 'WebView', type: 'type1' }}
];
I need the duplicated objects to be eliminated from the new array and not from the old array, in the most efficient way possible.
Maybe the solution is to use the filter method like in this answer? (https://stackoverflow.com/a/23080662/4275425) How can I implement that for my problem?