So this is the first technical post I am writing. It is mostly destined to my future self, but also, I am not really satisfied with the answer to t...
For further actions, you may consider blocking this person and/or reporting abuse
Merge both aaray using
Let newSet=[...set1,...set2]
Then apply this funtiin...
function unique(array, propertyName) {
return array.filter((e, i) => array.findIndex(a => a[propertyName] === e[propertyName]) === i);
}
Thanks for your reply.
Could you details more the process you use? Why would you use findIndex?
I tried to use your method in my use case, but it doesn't remove all the values from the smaller list in the total one
I will edit my post to make it more clearer with the expected result
I have used findindex to find current index property value to match with passed property to make it unique
Doesn't findIndex returns the first value that match the provided testing function? In need all the elements that match the predicate, not only the first one.
a more concise way would be to use the new ES6 spread operator and Set
merge your 2 arrays then
let mergedArray = array1.concat(array2)
const unique = [...new Set(mergedArray.map(item => item.label))];
I'm no expert just tried.
Not tested.
Store all the various props of arrayToSubtract in an Object.
var referenceObj = {}
arrayToSubstract.forEach((item) => referenceObj[item[propertyOfReference]] = true;);
//Reference object keys should not be in arrayTotal
var outputArr = arrayTotal.filter((item) => !referenceObj[item[propertyOfReference]);
Interesting way to resolve the problem, it should work :)
First of all:
Your solution seems to work, only 2 things I can think of to improve (besides not defining functions then calling them once as mentioned above):
A revised version would look like this:
Thanks you are right, it's a good way to improve it.