-6

1 - Into the following code snippet my array contains following items some of the items are duplicate and all are the arrays within array

arr[0] = ["Family Practice"]
arr[1] = ["Family Practice", "General Practice"]
arr[2] = ["Family Practice", "General Practice", "Geriatrics"]
arr[3] = ["Family Practice"]

How can I remove duplicate and return unique items in array .

2 - When adding the items into the array using push for ex.

function(items) {
    arr.push(items)
}

Here every time items are getting changed the array items are also getting changed. How can I array make immutable without any modification in the array irrespective if items change or not.

charlietfl
  • 170,828
  • 13
  • 121
  • 150
Sachin
  • 155
  • 1
  • 7
  • Show what you've tried. – Jordan Soltman Apr 01 '19 at 01:15
  • 1
    Questions should be limited to one issue only. Take a few minutes to read through [ask] and [Stack Overflow question checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) As for #1 this is easily researched and basic research is expected before asking. #2 needs more code context and a better problem description along with a [mcve] – charlietfl Apr 01 '19 at 01:18
  • Add all items as keys into a map. Its keys are the array without duplicate – Ruan Mendes Apr 01 '19 at 01:20

2 Answers2

0

You can check if an element exists prior to using push. Lots of ways to approach this, but an easy solution might be:

Array.prototype.pushUnique = function pushUnique(item) {
  if (this.indexOf(item) === -1) {
    this.push(item);
  }
}

// Then use it...
const arr = [];

arr.pushUnique("Family Practice");
// arr = ["Family Practice"]

arr.pushUnique("General Practice");
// arr = ["Family Practice", "General Practice"]

arr.pushUnique("Family Practice");
// arr = ["Family Practice", "General Practice"]

Not sure if that answers what you're after, but in general best bet is to just use indexOf or includes prior to adding to the array.

Eric T
  • 944
  • 1
  • 11
  • 26
  • [Don't modify objects you don't own.](https://stackoverflow.com/q/14034180/1218980) – Emile Bergeron Apr 01 '19 at 01:42
  • @EmileBergeron yeah would agree if I was saying to update an existing method, but even in the post you shared it states "Changing the behaviour of an object that will only be used by your own code is fine." This fits that description. Optionally you can also subclass it but my comment towards the end about "just use indexOf or includes" is pretty much the answer. – Eric T Apr 02 '19 at 14:36
  • `Array.prototype` is not only used in your own code. Modifying the native prototypes is never a good idea unless you're writing a polyfill. – Emile Bergeron Apr 02 '19 at 14:45
  • Can you think of a situation where values on `Array.prototype` which aren't expected by other libraries to be there would be modified? If so, I'll happily edit or remove my answer accordingly. If this were for OSS then I'd mostly agree with you, but doesn't seem to be. – Eric T Apr 03 '19 at 17:22
  • Let's say `pushUnique` gets added by the language but with a different behaviour. Or another library has the great idea to modify `Array.prototype` and adds a `pushUnique` function, found on Stack Overflow! You see the pattern. – Emile Bergeron Apr 03 '19 at 17:25
0

Here's a way that you can do this. Of course, some would say you could extend the Array prototype so this would be everywhere, but that would be your choice. This solution does not modify existing types.


var arr  = [];

    arr[0] = ["Family Practice"]
    arr[1] = ["Family Practice", "General Practice"]
    arr[2] = ["Family Practice", "General Practice", "Geriatrics"]
    arr[3] = ["Family Practice"]

function makeUunique (collection) {
    return collection.reduce(function(rv,item) {
        if(!rv.includes(item)) {
            rv.push(item);
        }
        return rv;
    },[]);
};

var unique = makeUunique(arr.reduce(function(rv,item) {
    rv = rv.concat(item);
    return rv;
},[]));

console.log(unique);

alwayslearning
  • 268
  • 2
  • 9
  • I know that you base your answer with assumption that the array is pre-populated but I think it would be better if we validate duplicates before originally adding it in the main array as the above solution shall be costly if the array has quite number of values. – Michel Hanna Apr 01 '19 at 02:01