Arrays are the cornerstone of almost all data. If you have two or more elements of the same type, you will most likely end up with an array. Since arrays are almost everywhere, knowing how to manipulate them is one of your core skills, no matter what they contain. This is true in almost all areas of development. Whether you're transforming, filtering, or inspecting data, array methods are key.
I don’t expect developers to memorize every function and functionality of the language or Web API, but I do expect you to find one or more ways to transform (map), eliminate (filter), and transform (reduce) ) array.
This series of articles will discuss different array methods and how we use them. I'd love to know what methods you'd like to learn about or discuss, so if there's any in particular that you'd like to cover, please let me know.
Let’s look at some array prototype methods and the functionality they provide. The following table lists the method signatures, including callback details. It shows us the return type, whether the response contains a record for each input, and whether it will run a callback for each entry in the array.
方法 | 返回值 | 一对一 | 对所有元素运行 |
---|---|---|---|
.map((value, index, array) => *) | 数组 | 是 | 是 |
.filter((value, index, array) => Boolean) | 数组 | 否 | 是 |
.reduce((accumulator, value, index, array) => *, optionalInitial) | * | 否 | 是 |
.find((value, index, array) => Boolean) | */undefined | 否 | 否 |
.some((value, index, array) => Boolean) | 布尔值 | 否 | 否 |
.every((value, index, array) => Boolean) | 布尔值 | 否 | 否 |
.forEach((value, index, array) => undefined) | undefined | N/A | 是 |
.sort((value1, value2) => Math.sign) | 数组 | 是 | **更多?** |
.flat(optionalDepth) | 数组 | 否 | 是 |
.flatMap((values, index, array) => *) | 数组 | 否 | 是 |
Performance will not be a primary consideration in this effort. We touch on it sometimes, but for most common data sizes there's usually no noticeable performance impact. In general, you should focus on code readability and maintainability before performance becomes a consideration. Working on devices with limited resources or processing extremely large data sets imposes some unique limitations. If you're interested in discussing performance issues further, please let me know!
In this series of articles, we will break down the individual array methods and discuss how to use them. There are many common, creative and "clever" ways to use these, and we'll try to cover some of the things you should avoid for the sake of readability and maintainability.
The above is the detailed content of JavaScript: Arrays are Everywhere. For more information, please follow other related articles on the PHP Chinese website!