Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

Last time, we listed all the methods of arrays. Today, we will take a look at the advanced use of arrays.

Everybody, let’s move.

Go to a null value

Array.prototype.filter is used to filter data.

Just pass in a Boolean, as we’ll see later in the print, which is also a function that converts the value to a Boolean, true values are retained, and non-true values are discarded. Of course, you need to know exactly what type the array elements are.

Accidents can occur in the following situations:

  1. A purely numeric array containing zeros
  2. Array of mixed types, numbers, objects, null, undefined, empty strings, etc
  3. Other situations
var arr = [{name:"tom".age: 18}, { name: "pik"},undefined];

const newArr = arr.filter(Boolean)


/ / print Boolean
console.log(Boolean)
ƒ Boolean() {[native code]}
Copy the code

Again, in certain circumstances, in certain functions.

Generate random number sets or sequence generators

Generate random groups of numbers, of course you can use a for loop, the code will be relatively more, array here has a clever trick.

One line of code gets the random number of the qualified data:

const createRandomValues = Array.from({length:10}, (v,i) = > Math.random());
// [0.25940121194082666, 0.8386585479250679, 0.26516411790325556, 0.6017479968140447, 
/ / 0.5730002277371962, 0.8710995725057937, 0.18703989792580744,
/ / 0.7930248062519736, 0.4589973173040094, 0.2985306917392727]
Copy the code

Let’s abstract it a little bit more. It’s a universal data generator. It’s that simple.

const createRandomValues = (creator, length = 10) = > Array.from({length}, creator)

const values = createRandomValues(Math.random)
 / / [0.4296594057567271, 0.175596004994798, 0.9947572646883405, 0.8956806916112903,
 / / 0.6601653932078315, 0.0020614027979746208, 0.3662092630842637, 0.39965167430505866,
 / / 0.4124746706358795, 0.6975065159343525]
Copy the code

Based on this, we make a sequence generator:

const createRandomValues = (creator, length = 10) = > Array.from({length}, creator);
const createRange = (start, stop, step) = >
createRandomValues((_, i) = > start + (i * step), (stop - start) / step + 1)

createRange (1.100.3) // [1, 4, 7, 10]
Copy the code

Of course, array. from can also be used to copy arrays and turn class arrays into arrays.

Empty array

And that brings us to array.prototye.splice,

Empty the array, do you like to assign to a new [] directly, there are better options, this does not create new data, it is still the same she.

var arr = [1.2.3];
arr.splice(0);
console.log(arr); / / []
Copy the code

It is also possible to set the length property to 0.

var arr = [1.2.3];
arr.length = 0
console.log(arr); / / []
Copy the code

Array shallow copy

The way a Array. Prototype. Slice

var arr = [{a:1}, {a:2}]
var arr2 = arr.slice(0);

console.log(arr); // [{a:1}, {a:2}]
console.log(arr2); // [{a:1}, {a:2}]

console.log(arr == arr2);
Copy the code

Way 2 Array. The from

var arr = [{a:1}, {a:2}]
var arr2 = Array.from(arr);

console.log(arr); // [{a:1}, {a:2}]
console.log(arr2); // [{a:1}, {a:2}]

console.log(arr == arr2);
Copy the code

Of course, extension operators can also work.

summary

Today we learned about nullating, generating random data and sequences, emptying arrays, shallow copies of arrays.

Did you harvest anything today?