0

i know this is kind of weird question but, i was creating a guard to prevent duplicate values in array i write some part and got a little help from stackoverflow but i can't understand code meaning properly

so i created Object with null prototype and iterated for loop over it to detect duplicate values (i know Set constructor is much easier but i am doing it in my server-side code and since older browsers does not support Set it would be dangerous to use Set). here is my code

var duplicateTagsGuard = Object.create(null);

    for(var co = 0; co < tags.length; co++){
      let val = tags[co];

      if(val in duplicateTagsGuard){
        return res.status(409).send({
          message: ''
        })
      }

       duplicateTagsGuard[val] = true

    }

and the part i cant understand is duplicateTagsGuard[val] = true

so if we split my code step by step and explain it would be like

1.first create null Object

2.iterate for loop on it and declare variable val and make it equal to every element in tags array

3.then check if that val is in duplicateTagsGuard object and if it is use return statement to prevent continuing for loop and if it is not then we are adding val's value to object but i don't know how it is implemented with that part of code (duplicateTagsGuard[val] = true) if anyone can explain i will be glad

ilia1
  • 111
  • 3
  • 13

2 Answers2

0

The code is creating a dictionary of val. Basically, when you iterate through the tags array, it checks whether the item in the array (accessed by tags[co]) is already present in the dictionary duplicateTagsGuard. If it has been encountered before, it will perform a certain action.

At the end of the loop, it simply injects the item into the dictionary. The dictionary therefore keep track of whether an item has been encountered before in the for loop.

The injection is done by using the item as the key in the dictionary, since it is easier to look it up (you simply have to do item in dictionary, which is basically val in duplicateTagsGuard in the actual implementation of the code). It does not matter what value you use, so a true value is used as a placeholder.

Terry
  • 63,248
  • 15
  • 96
  • 118
  • yeah, i know that but i want to know what does object[value] means – ilia1 Apr 13 '19 at 13:37
  • It means you're creating a new entry in the object with the key of whatever the value of `value` is. E.g. `duplicateTagsGuard['lorem'] = true` will mean that invoking `duplicateTagsGuard.lorem` will return `true`. – Terry Apr 13 '19 at 13:40
  • 1
    @ilia1 [It's bracket notation for property access](https://stackoverflow.com/a/4244912/1048572) – Bergi Apr 13 '19 at 13:41
  • and equating it to true creating a new entry? – ilia1 Apr 13 '19 at 13:41
0

first create null Object

It is not creating a null object but it is creating an object with null as the prototype check the Object.create docs:

var duplicateTagsGuard = Object.create(null);
console.log(`duplicateTagsGuard is an empty object:`);
console.log(duplicateTagsGuard);
console.log(`Prototye of duplicateTagsGuard is null: `);
console.log(Object.getPrototypeOf(duplicateTagsGuard));

iterate for loop on it and declare variable val and make it equal to every element in tags array

This part is correct every time the loop runs a new variable is created with for block scope and is assigned the value of the current coth index value of the tags array.

then check if that val is in duplicateTagsGuard object and if it is use return statement to prevent continuing for loop and if it is not then we are adding val's value to object but i don't know how it is implemented with that part of code (duplicateTagsGuard[val] = true)

It is checking whether val is a property of the duplicateTagsGuard object, if it is already present then the return is used to return the response else it is adding that property to the duplicateTagsGuard object with the bracket notation [propertyName] and assigning it's value as true.

var duplicateTagsGuard = Object.create(null); //creating a new empty object with prototype as null
let val = "hello"; //creating a new variable
duplicateTagsGuard[val] = true; //adding the property with the value of the variable val
console.log(val in duplicateTagsGuard); //checking if the added property is present in the object
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
  • 1
    thank you! "first create null Object", i know that's not correct :D but before i said it is object with null prototype (exactly what you said) but still thank you – ilia1 Apr 13 '19 at 13:44
  • @ilia1 welcome bud, if you are satisfied with the explanation please accept the answer which helped you the most! – Fullstack Guy Apr 13 '19 at 13:50
  • okay i did it :D Object.create() docs helped me also and the last question i got is if i assign property as true it adds that property to object? – ilia1 Apr 13 '19 at 14:13
  • @ilia1 yes it does and sets its value as `true`. – Fullstack Guy Apr 13 '19 at 14:37
  • oh god! i thought value must be 'true' if i wanted code to work so if i equate duplicateTagsGuard[val] = 'exists' it still works, thanks! – ilia1 Apr 13 '19 at 14:46