Cause 1.

Array API is a platitudes of the topic, this note is also the community has knowledge, just to learn, understand the knowledge of the stage comb, overcome the weak links.

Before sorting, I was always confused about the two methods of slice and splice. Forgive me for the scene in which only map, filter and forEach needed to delete elements in the array once, so I had to find the code or interview it in the console.

Beyond that, there is the Reduce method, which has had such a reputation since its birth that it seems to have no problem doing anything other methods can do. However, the number of parameters does not make it my first choice for rapid development. Sometimes I prefer to let sum outside the loop and use for loop accumulation, rather than using reduce’s beautiful API, which is really wrong. Take this opportunity to learn about it together.

2. Points clearspliceslice

I’m also curious about how developers distinguish between Slice and splice, and I’ve collected four methods so far.

2.1. Whether there are side effects

The first group of friends told me that the two methods are most clearly defined. It seems that I did not lay a good foundation at the beginning of learning. If I had made a clear distinction between the two at the beginning of learning, I would not have forgotten it in the past 20 years.

Splice will change the array

Slice does not alter the original array

It’s time to learn English

The second kind of friends represented by Digyou call on me to study English well, thank you digyou!

Splice v. splice

Slice v. cut

2.3. To be honest, I only useslice

What’s splice? I’ve never used it. I only use slice.

This is simply not speaking of martial virtue, only one need not distinguish.

2.4. Method names with more arguments are longer

This method is my own in order to consolidate the memory of their own thought.

As far as I’m concerned, I know that one of the methods has 2 arguments, and 2 argument methods can only be used for interception.

slice(start, end)

  • The starting position
  • End position

splice(start, length, ... item)

  • The starting position
  • Delete the length
  • Concatenated element

3. Take a lookreduce

The first time he approached Reduce, he knew he could do summation. I vaguely remember you can get prev, next, index, whatever.

At that time, I was wondering why prev was followed by next. How could I add up the current in the middle? Now seriously look at just know at that time oneself have how not careful.

There are two ways to write common tutorials online

  • reduce((cur, next, index, arr) => {})
  • reduce((prev, cur, index, arr) => {}, initialValue)

I didn’t differentiate it carefully, I copied and pasted it every time, so IT was prev Next

In fact, both of these notations contain only one layer of the reduce function.

First of all, the two parameters clearly exposed a problem. Prev cur Next was considered to be a consistent object, and the cumulative operation was not considered. A certain attribute of the object in the array might be used, for example:

[{x: 1}, {x: 2}].reduce((prev, cur) => prev + cur.x, 0)
Copy the code

Secondly, initialValue’s Reduce ((Prev, cur, index, ARR) => {}, initialValue) considers that the first fetch is not an element in the array, so it is named prev.

Reduce ((cur, Next, index, ARr) => {}), which has no initialValue, considers that the first fetch is the first one in the array, so it is named cur

Here we borrow the definition of reduce parameter in MDN

arr.reduce(callback(accumulator, currentValue, index? , array?) , initialValue)Copy the code

Perhaps accumulator is the most accurate definition of the first parameter.

The Reduce function has many more powerful features, but there are many articles in the community about them

4. To summarize

To their own understanding, but not familiar with the knowledge of the comb, can review the old know new, solid foundation.

Today’s note is here, I wish you a happy life, smooth coding 💗!