Please explain.

1) What is Object.keys(obj)

2) obj[str] = true; - and what prevents another value from taking its place.

 function unique(arr) { var obj = {}; for(var i=0; i<arr.length; i++) { var str = arr[i]; obj[str] = true; // запомнить строку в виде свойства объекта } return Object.keys(obj); // или собрать ключи перебором для IE<9 } var strings = ["кришна", "кришна", "харе", "харе", "харе", "харе", "кришна", "кришна", "8-()"]; alert( unique(strings) ); // кришна, харе, 8-() 

    2 answers 2

    1. Object.keys () (or with translation )
    2. It doesn't matter what the value is, the main thing is the keys of the object. Because each key is unique, then all subsequent ones will only overwrite the value of the existing one.

    PS By the way, here's another implementation:

     var arr = ["кришна", "кришна", "харе", "харе", "харе", "харе", "кришна", "кришна", "8-()"]; function uniqueVal(value, index, self) { return self.indexOf(value) === index; } console.log( arr.filter( uniqueVal) ); // ["кришна", "харе", "8-()"] 
    • those. object (key: value). and the key of the object is always unique and since it has already been recorded, is it just that it will lose? - msim 5:56 pm
    • one
      @msim, you can say that it ignores, although if more precisely, it will still overwrite the value of the existing key. In any case, a key with the same name will not be created ( at the same level of nesting, of course ). - Deonis
    • I do not want to kill your faith in the human mind. But you give the uniqueVal function to the created array with the filter property. but the functions do not give any parameters - msim
    • 2
      @msim, and she needs it? It goes without parameters)) Seriously, look at this method [filter ()] [1] [1]: learn.javascript.ru/array-iteration#filter - Deonis
    • For an example with a Krishna, it would be possible to link to the source - it may be useful to the questioner. - justyx
     let beepbeep = []; let imjeep = Array.from(new Set(beepbeep)); console.log(imjeep); 
    • 2
      Please try to leave a little more detailed answers. you can add an answer by clicking edit - aleksandr barakin