0

I have 2 arrays:

0: {id: 2, name: "TMA"}
1: {id: 3, name: "Hibbernate"}
0: {id: 1, name: "FB.DE"}
1: {id: 2, name: "TMA"}
2: {id: 3, name: "Hibbernate"}
3: {id: 4, name: "Event.it A"}
4: {id: 5, name: "Projket 2"}
5: {id: 6, name: "Projekt 1"}

I want to compare them and delete the objects with the id 2 and 3 cause both arrays have them and thats the similarity.

This is my Code so far:

const projectListOutput = projectsOfPersonArray.filter(project => data.includes(project));
 console.log(projectListOutput);

But every time i run this projectListOutput is empty.

asked Jun 5, 2020 at 20:06
3
  • are both arrays sorted? Commented Jun 5, 2020 at 20:15
  • When you mean bay sorted with the sort() function, then yes. Commented Jun 5, 2020 at 20:19
  • Which array is projectsOfPersonArray and which is data? Because if the short one is projectsOfPersonArray, then you've filtered both elements out... Commented Jun 5, 2020 at 20:19

5 Answers 5

2

When using includes dont compare objects, Just build data as array of strings. Remaining code is similar to what you have.

arr1 = [
 { id: 2, name: "TMA" },
 { id: 3, name: "Hibbernate" },
];
arr2 = [
 { id: 1, name: "FB.DE" },
 { id: 2, name: "TMA" },
 { id: 3, name: "Hibbernate" },
 { id: 4, name: "Event.it A" },
 { id: 5, name: "Projket 2" },
 { id: 6, name: "Projekt 1" },
];
const data = arr1.map(({ id }) => id);
const result = arr2.filter(({ id }) => !data.includes(id));
console.log(result);

answered Jun 6, 2020 at 10:25
Sign up to request clarification or add additional context in comments.

Comments

0

Your data array probably does not contain the exact same object references than projectsOfPersonArray. Look at the code below:

[{ foo: 'bar' }].includes({ foo: 'bar' });
// false

Objects look equal, but they don't share the same reference (= they're not the same). It's safer to use includes with primitive values like numbers or strings. You can for example check the ids of your objects instead of the full objects.

answered Jun 5, 2020 at 20:18

Comments

0

You compare different objects, so every object is unique.

For filtering, you need to compare all properties or use a JSON string, if the order of properties is equal.

var exclude = [{ id: 2, name: "TMA" }, { id: 3, name: "Hibbernate" }],
 data = [{ id: 2, name: "TMA" }, { id: 3, name: "Hibbernate" }, { id: 1, name: "FB.DE" }, { id: 2, name: "TMA" }, { id: 3, name: "Hibbernate" }, { id: 4, name: "Event.it A" }, { id: 5, name: "Projket 2" }, { id: 6, name: "Projekt 1" }],
 result = data.filter(project =>
 !exclude.some(item => JSON.stringify(item) === JSON.stringify(project))
 );
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

answered Jun 5, 2020 at 20:15

Comments

0

You can do something similar to the next:

const source = [{
 id: 1,
 name: "FB.DE"
 },
 {
 id: 2,
 name: "TMA"
 },
 {
 id: 3,
 name: "Hibbernate"
 },
 {
 id: 4,
 name: "Event.it A"
 },
 {
 id: 5,
 name: "Projket 2"
 },
 {
 id: 6,
 name: "Projekt 1"
 }
]
const toRemove = [{
 id: 2,
 name: "TMA"
 },
 {
 id: 3,
 name: "Hibbernate"
 }
]
/**create object where keys is object "id" prop, and value is true**/
const toRemoveMap = toRemove.reduce((result, item) => ({
 ...result,
 [item.id]: true
}), {})
const result = source.filter(item => !toRemoveMap[item.id])

You can make function from it:

function removeArrayDuplicates (sourceArray, duplicatesArray, accessor) {
 const toRemoveMap = duplicatesArray.reduce((result, item) => ({
 ...result,
 [item[accessor]]: true
 }), {});
 return sourceArray.filter(item => !toRemoveMap[item[accessor]])
}
removeArrayDuplicates(source, toRemove, 'id')

Or even better, you can make it work with a function instead of just property accessor:

function removeDuplicates (sourceArray, duplicatesArray, accessor) {
 let objectSerializer = obj => obj[accessor];
 if(typeof accessor === 'function') {
 objectSerializer = accessor;
 }
 const toRemoveMap = duplicatesArray.reduce((result, item) => ({
 ...result,
 [objectSerializer(item)]: true
 }), {});
 return sourceArray.filter(item => !toRemoveMap[objectSerializer(item)])
}
removeDuplicates(source, toRemove, (obj) => JSON.stringify(obj))
answered Jun 5, 2020 at 20:17

Comments

0

This function will help you merge two sorted arrays

var arr1 = [
 { id: 2, name: 'TMA' },
 { id: 3, name: 'Hibbernate' },
]
var arr2 = [
 { id: 1, name: 'FB.DE' },
 { id: 2, name: 'TMA' },
 { id: 3, name: 'Hibbernate' },
 { id: 4, name: 'Event.it A' },
 { id: 5, name: 'Projket 2' },
]
function mergeArray(array1, array2) {
 var result = []
 var firstArrayLen = array1.length
 var secondArrayLen = array2.length
 var i = 0 // index for first array
 var j = 0 // index for second array
 while (i < firstArrayLen || j < secondArrayLen) {
 if (i === firstArrayLen) { // first array doesn't have any other members
 while (j < secondArrayLen) { // we copy rest members of first array as a result
 result.push(array2[j])
 j++
 }
 } else if (j === secondArrayLen) { // second array doesn't have any other members
 while (i < firstArrayLen) { // we copy the rest members of the first array to the result array
 result.push(array1[i])
 i++
 }
 } else if (array1[i].id < array2[j].id) {
 result.push(array1[i])
 i++
 } else if (array1[i].id > array2[j].id) {
 result.push(array2[j])
 j++
 } else {
 result.push(array1[i])
 i++
 j++
 }
 }
 return result
}
console.log(mergeArray(arr1,arr2));
answered Jun 5, 2020 at 20:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.