This is my first day to participate in the “Gold Digging Day New Plan · April More text challenge”, click to see the details of the activity.
Hello, I’m komori du, in the work, we often deal with all kinds of data type is one of the most common kind of array, the array of the operation, I believe you are familiar with, for the operation of the array, there are many ways to achieve the same effect, so how to use the most simple way to achieve the effect? Let’s take a look at some of the tips for using arrays one by one
1. Array deduplication
Array decrement is the most common operation, so when you want to decrement an array, what’s the first thing that comes to mind?
Recursion? Sort? filter….
There’s actually a better way to do this, which is to use Set
Special attention:
- To use Set, you must declare it with new
- New Set() returns an object instead of an Array, so we need to convert array.from () to an Array, and of course we can expand the operator… It is also recommended to use…
let arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.0.0.'a'.'a'];
console.log([...new Set(arr)])
// [1, 'true', true, 15, false, undefined, null, NaN, 'NaN', 0, 'a']
Copy the code
Of course, you can’t get rid of {} with this method because the addresses point to different things, but it’s enough for most cases.
2. Intersection, union and difference sets
For these three, in the actual development is not very common, relatively common is the intersection, usually using and interface pass
For example, a table has multiple fields such as ID, name and time, but it can be queried by ID and name. Generally speaking, the interface will not query by name, but by ID. That is to say, only one field is passed to the interface, that is, the intersection of ID and name
What should we do to solve this situation? It is impossible to iterate through two arrays and then extract the same element. This method is cumbersome and feels low. Is there a better way to solve this problem?
It’s easy to keep using Set
let a = new Set([1.2.3])
let b = new Set([2.3.4])
/ / and set
console.log(... newSet([...a, ...b])) // Set(4) {1, 2, 3, 4}
/ / intersection
console.log([...new Set([...a].filter(v= > b.has(v)))]) // Set(2) {2, 3}
/ / difference set
console.log([...new Set([...a].filter(v= >! b.has(v)))])// Set(1) {1}
Copy the code
3. Array stitching
What is the method of most group concatenation? In older projects or older front ends, the most commonly used is concat, which has been replaced by the expansion operator
let a = [1.2]
let b = [3.4]
let c = [5.6]
let res = [...a, ...b, ...c]
console.log(res) // [1, 2, 3, 4, 5, 6]
Copy the code
4. Maximum and minimum values
Math should be the first thing that comes to mind when looking for maximums and minimums
const arr = [1.2.3]
/ / Max
Math.max(... arr)/ / 3
/ / the minimum
Math.min(... arr)/ / 1
Copy the code
5. Sum
For summation, we can do it with loops, and other than that we can do it with reduce,
const sum = arr= > arr.reduce((a, b) = > a + b, 0);
console.log(sum([1.2.3.4])) / / 10
Copy the code
6. Array flattening
Array flattening is a sad topic for me. In an interview, the interviewer asked me how to flatten an array. I asked him what is array flattening, and the interviewer said awkwardly: [1, 2, [3, 4] how to change it into [1, 2, 3, 4]
The answer I gave was to iterate, and then typeof to determine if it was an array, and if it was, continue, and if it wasn’t, return. The interviewer asked me, “What if I continue to stack?” I said to use recursion, and the interviewer asked me, don’t you think it’s too much trouble?
In fact, what is the investigation? The investigation is just flat. As a result, I was embarrassed to talk with the interviewer for a long time.
let arr = [1.2[3.4]].flat()
console.log(arr) // [1, 2, 3, 4]
let arr = [1.2[3[4]]].flat(2)
console.log(arr) // [1, 2, 3, 4]
let arr = [1.2[3[4[5]]]].flat(Infinity)
console.log(arr) // [1, 2, 3, 4, 5]
Copy the code
debugging
Console.table () console.table()
If you’re familiar with conole.log(), console.error(), etc., what does console.table() type?
Try console.table([1, 2, 3])
End
The special method of arrays has been completed. If you have any better tips, please discuss them in the comments section. Click like 👍🏻 to support you.