0

I have checked other solutions but none fit the criterion of my problem

This solution does not have the ability to dynamically check each node

Problem summarized

I wish to create an algorithm that is able to check an object that has nodes of different data types, for duplicated objects in nodes that are specifically of the datatype array.

I have the following dataset:

task = {
  content: "lorem....",
  customer: [
    { id: 1, name: "hello" },
    { id: 2, name: "sup" },
  ],
  end: "2020-08-13 10:09:48",
  project: [{ id: 1 }, { id: 1 }, { id: 2 }],
  vendor: [{ id: 2 }, { id: 2 }, { id: 3 }],
};

I wish to be able to dynamically check which of the objects (or nodes? and the algo has to recognize that it is an array) has duplicates, and reduce them to be in this form:

task = {
  content: "lorem....",
  customer: [
    { id: 1, name: "hello" },
    { id: 2, name: "sup" },
  ],
  end: "2020-08-13 10:09:48",
  project: [{ id: 1 }, { id: 2 }],
  vendor: [{ id: 2 }, { id: 3 }],
};

EDIT

The algorithm needs to be able to handle a dynamic number of nodes (example 1), however , the duplicates will only happen 1 level down (Thanks for pointing out).

example 1 (there is 1 less node here ) :

task = {
  content: "lorem....",
  customer: [
    { id: 1, name: "hello" },
    { id: 2, name: "sup" },
  ],
  end: "2020-08-13 10:09:48",
  project: [{ id: 1 }, { id: 2 }],
};
8
  • "algo has to recognize that it is an array" - see Array.isArray Commented Aug 13, 2020 at 14:25
  • Hello , thanks for the comment , im aware of such a function to detect if its an array , however i havent have a clue on how to apply this to get the results i want Commented Aug 13, 2020 at 14:26
  • "I have checked other solutions but non fits the criterion of my problem" - please provide some links and why it did not work Commented Aug 13, 2020 at 14:26
  • Can you show what you've tried thus far, and where you've run into problems? Commented Aug 13, 2020 at 14:27
  • @JanStránský Hey i have made an edit to the post , the link is one of the links that i have visited. In general stackoverflow has many questions and answers that solves 1 part of my problem which is to filter out duplicates in an array , however i need the algo to be able to dynamically check each node as the dataset may or may not change the number of nodes Commented Aug 13, 2020 at 14:29

1 Answer 1

1

Here is my proposed solution to remove duplicate elements from any array in the task object:

const uniq = array => {
  const map = {};
  const result = [];
  for (let i = 0; i < array.length; i++) {
    // since elements can be objects, need to do a deep comparison.
    const element = JSON.stringify(array[i]);
    if (map[element] === undefined) {
      map[element] = true;
      result.push(array[i]);
    }
  }
  return result;
}

const task = {
  content: "lorem....",
  customer: [
    { id: 1, name: "hello" },
    { id: 2, name: "sup" },
  ],
  end: "2020-08-13 10:09:48",
  project: [{ id: 1 }, { id: 1 }, { id: 2 }],
  vendor: [{ id: 2 }, { id: 2 }, { id: 3 }],
};

for (const key in task) {
  if (Array.isArray(task[key])) {
    task[key] = uniq(task[key])
  }
}

console.log('deduped:', task);

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks this works perfectly for me , appreciate the help , im having a better understanding of JS everyday
for (let i = 0; i < array.length; i++) { ... array[i] ... } I prefer of solution: for (let e of array) { ... e ... } (not sure if it is worth editing the answer)
using foreach would probably be more idiomatic, but I'm just gonna leave it since the vanilla for loop demonstrates what's going on under the hood.

Your Answer

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.