Set method

MDN link

Objects allow you to store a unique value of any type, either a primitive value or an object reference

new Set([1.2.3.4.5])  // [1, 2, 3, 4, 5]
// It automatically deweights
/ / the only value
new Set([1.2.3.4.5.5.5.5])  // [1, 2, 3, 4, 5]
/ / the original value
new Set([1.2.'1'.'1'.null.null ,undefined.undefined]) // [1, 2, '1', null, undefined]
// Object reference
var a = {} 
new Set([a , a, a])  // [a]

// The problem is that different objects will not be weighted
var a = {}
var b = {}
// It will not weigh
new Set([a , b])  // [a, b]
Copy the code

From (new Set(array))). Another way to write this is […new Set(array)]. This will try to turn this thing into an array,… That means deconstruct it and put it in an array

The subscript of the object is de-weighted

var a = [???] // The value inside is not known

function uniq(array){????? }// The contents are not known

/ / request
uniq(a) // Pass a to your uniq to get a new array, and the new array is guaranteed to have no duplicate elements
Copy the code

For example,

Var a = [1, 2, 3, 3, 4, 4]

  1. Let’s start by writing a function that has a return value that returns a new array
function uniq(array){
  var result = []
  return result
}
Copy the code
  1. The requirement for this is to pass a to my function, which is going to array it
function uniq(array){
  var result = []
  
  var hash = {}  // Create an empty object
  
  for(let i = 0; i < array.length; i++){  // Iterate over the array
  // I hash true when I find 1 in the array
  // In the hash object, the values of the array are used as subscripts, so duplicates are removed
  hash.push[array[i]] = true   
  }
  // I'm going to turn it into an array
  for(let key in hash){ // for... In gets all the keys of the current object
    result.push(key)
  }
 
  return result
}
console.log(uniq(a)) // Print ['1', '2', '3', '4']
Copy the code

disadvantages

  1. Question 1
  • The value number in the array becomes a string
  1. Question 2
  • What if there were no numbers in the middle of the array? For example: [1, 2, 3, 4, ‘5’, 5]

    How to distinguish? It will print [‘1’, ‘2’, ‘3’, ‘4’, ‘5’]

    There’s no way to tell if it’s a number or a string

    For example:

     var hash = {}
     hash[4] = 'digital'  // Print out {4: 'number '}
     hash['4'] = 'String' // Are there two values? No, it prints {4: 'string '}
    Copy the code
  1. Question 3
  • If I have an object like [1, 2, 3, 4, {name: ‘object’}, 5]

    It prints [1, 2, 3, 4, “[object object]”, 5]

    Because the subscript of this method only supports strings, when I index this object it will become, okay?

     var hash = {}
     hash[{name: 'object'}] = true  // Print {[object object]: true}
    Copy the code

    Why object object because all objects that become strings are strings that look like this

    var a = {}
    a.toString() // [object Object]
    Copy the code

    When we see that the subscript of the object is an object, we call the toString() method of the object, and it looks like this

conclusion

The biggest problem with subscripting objects is that you can count only strings (you can’t distinguish numbers from strings), not objects

How to answer an interview question?

The first thing we should do is see if the requirements can use ES6 syntax and if not, I’m going to go with a less than perfect algorithm, which looks like this

  • So we’re going to iterate through the array, and we’re going to store the value of the array as the subscript of the object, and we’re going to store it every time we encounter a value, and then if we find the same value, we’re going to ignore it, if it’s a new value we’re going to set it to true, and we’re going to iterate over that true and put it in the new array and return, This is a less than perfect way to undo an array. Why is it less than perfect? First, it can’t distinguish between a number and a string. If the array has an object in it, our de-duplicate method will fail. The reason is that our object only supports subscripts of strings. If we could use ES6’s Set, it would be easy, we just need new Set(array) and get the value from array.from ().