LEARNING JAVASCRIPT - Trang 162

every

returns

true

if every element in the array passes the criteria, and

false

other‐

wise.

every

will stop looking and return

false

if it finds an element that doesn’t

match the criteria; otherwise, it will have to scan the entire array:

const

arr

=

[

4

,

6

,

16

,

36

];

arr

.

every

(

x

=>

x

%

2

===

0

);

// true; no odd numbers

arr

.

every

(

x

=>

Number

.

isInteger

(

Math

.

sqrt

(

x

)));

// false; 6 is not square

Like all of the methods in this chapter that accept a method,

some

and

every

accept a

second parameter that allows you to specify the value of

this

when the function is

invoked.

The Fundamental Array Operations: map and filter

Of all the array operations, the ones you’ll find the most useful are

map

and

filter

.

It’s really quite remarkable what you can accomplish with these two methods.

map

transforms the elements in the array. To what? That’s the beauty: it’s up to you. Do

you have objects that contain numbers, but you really just need the numbers? Easy.

Does your array contain functions, and you need promises? Easy. Whenever the array

is in one format and you need it in another, use

map

. Both

map

and

filter

return

copies, and do not modify the original array. Let’s see some examples:

const

cart

=

[ {

name

:

"Widget"

,

price

:

9.95

}, {

name

:

"Gadget"

,

price

:

22.95

}];

const

names

=

cart

.

map

(

x

=>

x

.

name

);

// ["Widget", "Gadget"]

const

prices

=

cart

.

map

(

x

=>

x

.

price

);

// [9.95, 22.95]

const

discountPrices

=

prices

.

map

(

x

=>

x

*

0.8

);

// [7.96, 18.36]

const

lcNames

=

names

.

map

(

String

.

toLowerCase

);

// ["widget", "gadget"]

You may be wondering how

lcNames

is working: it doesn’t look like the others. All of

the methods we’re discussing that take functions, including

map

, don’t care how you

pass the function in. In the case of

names

,

prices

, and

discountPrices

, we’re con‐

structing our own custom function (using the arrow notation). For

lcNames

, we’re

using a function that already exists,

String.toLowerCase

. This function takes a single

string argument and returns the lowercased string. We could as easily have written

names.map(x

⇒ x.toLowerCase())

, but it’s important to understand that a function

is a function, no matter what form it takes.
When the function you provide gets called, it gets called for each element with three

arguments: the element itself, the index of the element, and the array itself (which is

seldom useful). Consider this example where we have our items and corresponding

prices in two separate arrays, and we want to combine them:

const

items

=

[

"Widget"

,

"Gadget"

];

const

prices

=

[

9.95

,

22.95

];

const

cart

=

items

.

map

((

x

,

i

)

=>

({

name

:

x

,

price

:

prices

[

i

]}));

// cart: [{ name: "Widget", price: 9.95 }, { name: "Gadget", price: 22.95 }]

138 | Chapter 8: Arrays and Array Processing

Liên Kết Chia Sẽ

** Đây là liên kết chia sẻ bới cộng đồng người dùng, chúng tôi không chịu trách nhiệm gì về nội dung của các thông tin này. Nếu có liên kết nào không phù hợp xin hãy báo cho admin.