Bản đồ JavaScript

Phương thức

[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
29 tạo một mảng mới với kết quả gọi một hàm cho mọi phần tử mảng

Thí dụ

let numbers = [2, 4, 6, 8, 10];

// function to return the square of a number
function square(number) {
  return number * number;
}

// apply square() function to each item of the numbers list let square_numbers = numbers.map(square);

console.log(square_numbers); // Output: [ 4, 16, 36, 64, 100 ]

map() Cú pháp

Cú pháp của phương thức

[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
29 là

arr.map(callback(currentValue), thisArg)

Ở đây, arr là một mảng


bản đồ () Tham số

Phương thức

[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
29 nhận

  • gọi lại - Hàm được gọi cho mọi phần tử mảng. Các giá trị trả về của nó được thêm vào mảng mới. Nó đưa vào
    • currentValue - Phần tử hiện tại được truyền từ mảng
  • thisArg (tùy chọn) - Giá trị sử dụng như
    [
      42.42640687119285,
      44.721359549995796,
      54.772255750516614,
      70.71067811865476,
      22.360679774997898,
      89.44271909999159
    ]
    [
      'J', 'a', 'v', 'a',
      'S', 'c', 'r', 'i',
      'p', 't'
    ]
    [
       74,  97, 118,  97,
       83,  99, 114, 105,
      112, 116
    ]
    32 khi thực hiện gọi lại. Theo mặc định, nó là
    arr.map(callback(currentValue), thisArg)
    0

map() Giá trị trả về

  • Trả về một mảng mới với các phần tử là giá trị trả về từ hàm
    arr.map(callback(currentValue), thisArg)
    1 cho mỗi phần tử

ghi chú

  • [
      42.42640687119285,
      44.721359549995796,
      54.772255750516614,
      70.71067811865476,
      22.360679774997898,
      89.44271909999159
    ]
    [
      'J', 'a', 'v', 'a',
      'S', 'c', 'r', 'i',
      'p', 't'
    ]
    [
       74,  97, 118,  97,
       83,  99, 114, 105,
      112, 116
    ]
    29 không thay đổi mảng ban đầu
  • [
      42.42640687119285,
      44.721359549995796,
      54.772255750516614,
      70.71067811865476,
      22.360679774997898,
      89.44271909999159
    ]
    [
      'J', 'a', 'v', 'a',
      'S', 'c', 'r', 'i',
      'p', 't'
    ]
    [
       74,  97, 118,  97,
       83,  99, 114, 105,
      112, 116
    ]
    29 thực thi
    arr.map(callback(currentValue), thisArg)
    1 một lần cho từng phần tử mảng theo thứ tự
  • [
      42.42640687119285,
      44.721359549995796,
      54.772255750516614,
      70.71067811865476,
      22.360679774997898,
      89.44271909999159
    ]
    [
      'J', 'a', 'v', 'a',
      'S', 'c', 'r', 'i',
      'p', 't'
    ]
    [
       74,  97, 118,  97,
       83,  99, 114, 105,
      112, 116
    ]
    29 không thực thi
    arr.map(callback(currentValue), thisArg)
    1 đối với các phần tử mảng không có giá trị


ví dụ 1. Ánh xạ các phần tử mảng bằng chức năng tùy chỉnh

const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]

đầu ra

[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]

ví dụ 2. map() cho các phần tử đối tượng trong mảng

________số 8

đầu ra

[
  { name: 'Adam', netEarning: 4500 },
  { name: 'Noah', netEarning: 7000 },
  { name: 'Fabiano', netEarning: 1800 },
  { name: 'Alireza', netEarning: 4600 }
]

Ghi chú.

[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
29 gán
arr.map(callback(currentValue), thisArg)
0 cho mảng mới nếu hàm
arr.map(callback(currentValue), thisArg)
1 trả về
arr.map(callback(currentValue), thisArg)
0 hoặc không có gì

Trong bài viết này, chúng ta sẽ thảo luận về đối tượng Map được cung cấp bởi ES6. Bản đồ là một tập hợp các phần tử trong đó mỗi phần tử được lưu trữ dưới dạng một cặp Khóa, giá trị. Đối tượng bản đồ có thể chứa cả đối tượng và giá trị nguyên thủy dưới dạng khóa hoặc giá trị. Khi chúng ta lặp lại đối tượng bản đồ, nó sẽ trả về cặp khóa, giá trị theo cùng thứ tự như đã chèn

cú pháp.  

arr.map(callback(currentValue), thisArg)
3

Bây giờ, hãy tạo một số Bản đồ bằng đối tượng Bản đồ

Javascript




arr.map(callback(currentValue), thisArg)
42

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
44

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
46

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
48

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
20

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
22
arr.map(callback(currentValue), thisArg)
23______224
arr.map(callback(currentValue), thisArg)
25

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
27

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
29

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
31

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
33

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
36
arr.map(callback(currentValue), thisArg)
37
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
20

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
23

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
25

_______143____427

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
29

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
22
arr.map(callback(currentValue), thisArg)
42
arr.map(callback(currentValue), thisArg)
24
arr.map(callback(currentValue), thisArg)
25

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
46
arr.map(callback(currentValue), thisArg)
47
arr.map(callback(currentValue), thisArg)
48
arr.map(callback(currentValue), thisArg)
49
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
20

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
46
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
23
arr.map(callback(currentValue), thisArg)
48
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
25
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
20

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
46
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
29
arr.map(callback(currentValue), thisArg)
48
[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
31
[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
32

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
33

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
36
[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
38
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43
const employees = [
    { name: "Adam", salary: 5000, bonus: 500, tax: 1000 },
    { name: "Noah", salary: 8000, bonus: 1500, tax: 2500 },
    { name: "Fabiano", salary: 1500, bonus: 500, tax: 200 },
    { name: "Alireza", salary: 4500, bonus: 1000, tax: 900 },
];

// calculate the net amount to be given to the employees
const calcAmt = (obj) => {
    newObj = {};
    newObj.name = obj.name;
    newObj.netEarning = obj.salary + obj.bonus - obj.tax;
    return newObj;
};

let newArr = employees.map(calcAmt);

console.log(newArr);
31

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43
const employees = [
    { name: "Adam", salary: 5000, bonus: 500, tax: 1000 },
    { name: "Noah", salary: 8000, bonus: 1500, tax: 2500 },
    { name: "Fabiano", salary: 1500, bonus: 500, tax: 200 },
    { name: "Alireza", salary: 4500, bonus: 1000, tax: 900 },
];

// calculate the net amount to be given to the employees
const calcAmt = (obj) => {
    newObj = {};
    newObj.name = obj.name;
    newObj.netEarning = obj.salary + obj.bonus - obj.tax;
    return newObj;
};

let newArr = employees.map(calcAmt);

console.log(newArr);
35

arr.map(callback(currentValue), thisArg)
43
const employees = [
    { name: "Adam", salary: 5000, bonus: 500, tax: 1000 },
    { name: "Noah", salary: 8000, bonus: 1500, tax: 2500 },
    { name: "Fabiano", salary: 1500, bonus: 500, tax: 200 },
    { name: "Alireza", salary: 4500, bonus: 1000, tax: 900 },
];

// calculate the net amount to be given to the employees
const calcAmt = (obj) => {
    newObj = {};
    newObj.name = obj.name;
    newObj.netEarning = obj.salary + obj.bonus - obj.tax;
    return newObj;
};

let newArr = employees.map(calcAmt);

console.log(newArr);
37

arr.map(callback(currentValue), thisArg)
43
const employees = [
    { name: "Adam", salary: 5000, bonus: 500, tax: 1000 },
    { name: "Noah", salary: 8000, bonus: 1500, tax: 2500 },
    { name: "Fabiano", salary: 1500, bonus: 500, tax: 200 },
    { name: "Alireza", salary: 4500, bonus: 1000, tax: 900 },
];

// calculate the net amount to be given to the employees
const calcAmt = (obj) => {
    newObj = {};
    newObj.name = obj.name;
    newObj.netEarning = obj.salary + obj.bonus - obj.tax;
    return newObj;
};

let newArr = employees.map(calcAmt);

console.log(newArr);
39

arr.map(callback(currentValue), thisArg)
43____931

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
22
[
  { name: 'Adam', netEarning: 4500 },
  { name: 'Noah', netEarning: 7000 },
  { name: 'Fabiano', netEarning: 1800 },
  { name: 'Alireza', netEarning: 4600 }
]
34
arr.map(callback(currentValue), thisArg)
24
arr.map(callback(currentValue), thisArg)
25

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
46____939
arr.map(callback(currentValue), thisArg)
400

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
46
arr.map(callback(currentValue), thisArg)
403
arr.map(callback(currentValue), thisArg)
404

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
46
arr.map(callback(currentValue), thisArg)
407
arr.map(callback(currentValue), thisArg)
408

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
33

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
36
arr.map(callback(currentValue), thisArg)
414
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
417

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
420

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
422

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
424

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
426

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
22
arr.map(callback(currentValue), thisArg)
429
arr.map(callback(currentValue), thisArg)
24
arr.map(callback(currentValue), thisArg)
25

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
46

_______1434____546____1436____548

arr.map(callback(currentValue), thisArg)
438
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
20

arr.map(callback(currentValue), thisArg)
434
arr.map(callback(currentValue), thisArg)
46
arr.map(callback(currentValue), thisArg)
49
arr.map(callback(currentValue), thisArg)
48
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
25
[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
32

arr.map(callback(currentValue), thisArg)
26
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
20

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
46

arr.map(callback(currentValue), thisArg)
434
arr.map(callback(currentValue), thisArg)
46
arr.map(callback(currentValue), thisArg)
452
arr.map(callback(currentValue), thisArg)
48
arr.map(callback(currentValue), thisArg)
454
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
20

arr.map(callback(currentValue), thisArg)
434
arr.map(callback(currentValue), thisArg)
46
arr.map(callback(currentValue), thisArg)
458
arr.map(callback(currentValue), thisArg)
48
arr.map(callback(currentValue), thisArg)
460
[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
32

arr.map(callback(currentValue), thisArg)
26
[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
32

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
33

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
36
arr.map(callback(currentValue), thisArg)
469
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
472

arr.map(callback(currentValue), thisArg)
473

đầu ra.  

Bản đồ JavaScript
Bản đồ JavaScript

Của cải.  

Bản đồ. nguyên mẫu. kích thước – Nó trả về số phần tử hoặc cặp khóa-giá trị trong bản đồ

phương pháp.   

1. Bản đồ. nguyên mẫu. set() – Nó thêm key và value vào Map Object.  

cú pháp.  

arr.map(callback(currentValue), thisArg)
4

2. Bản đồ. nguyên mẫu. has() – Nó trả về một giá trị boolean tùy thuộc vào việc khóa được chỉ định có tồn tại hay không

cú pháp.  

arr.map(callback(currentValue), thisArg)
2

3. Bản đồ. nguyên mẫu. get() – Nó trả về giá trị của key tương ứng

cú pháp.  

arr.map(callback(currentValue), thisArg)
3

4. Bản đồ. nguyên mẫu. xóa () - Nó xóa cả khóa cũng như giá trị khỏi bản đồ.  

cú pháp.  

arr.map(callback(currentValue), thisArg)
2

5. Bản đồ. nguyên mẫu. clear() – Loại bỏ tất cả các phần tử khỏi đối tượng Map

cú pháp.  

arr.map(callback(currentValue), thisArg)
4

Hãy sử dụng tất cả các phương pháp được mô tả ở trên.  

Thí dụ.  

Javascript




arr.map(callback(currentValue), thisArg)
42

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
476

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
478

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
22
arr.map(callback(currentValue), thisArg)
23______224
arr.map(callback(currentValue), thisArg)
483

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
486

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
488
arr.map(callback(currentValue), thisArg)
436
arr.map(callback(currentValue), thisArg)
48
arr.map(callback(currentValue), thisArg)
49
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
488
arr.map(callback(currentValue), thisArg)
438
arr.map(callback(currentValue), thisArg)
48
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
25
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
488
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
29
arr.map(callback(currentValue), thisArg)
48
[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
31
arr.map(callback(currentValue), thisArg)
204

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
206____1452____548
arr.map(callback(currentValue), thisArg)
460
arr.map(callback(currentValue), thisArg)
204

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
206____1454____548
arr.map(callback(currentValue), thisArg)
458
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
44

arr.map(callback(currentValue), thisArg)
43____2221

arr.map(callback(currentValue), thisArg)
43____2223

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
225

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
227

arr.map(callback(currentValue), thisArg)
43____2229

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
20

arr.map(callback(currentValue), thisArg)
26

arr.map(callback(currentValue), thisArg)
43____2234

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43____2237

_______143____336____2240

arr.map(callback(currentValue), thisArg)
241

arr.map(callback(currentValue), thisArg)
242
arr.map(callback(currentValue), thisArg)
243____629
arr.map(callback(currentValue), thisArg)
245

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43____2248

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
36____2251
arr.map(callback(currentValue), thisArg)
241

arr.map(callback(currentValue), thisArg)
242
arr.map(callback(currentValue), thisArg)
243
arr.map(callback(currentValue), thisArg)
255
arr.map(callback(currentValue), thisArg)
245

arr.map(callback(currentValue), thisArg)
26

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
260

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43____2263

_______143____336____2266

arr.map(callback(currentValue), thisArg)
241

arr.map(callback(currentValue), thisArg)
242
arr.map(callback(currentValue), thisArg)
269
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
29
arr.map(callback(currentValue), thisArg)
245

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43____2274

_______143____336____2277

arr.map(callback(currentValue), thisArg)
241

arr.map(callback(currentValue), thisArg)
242
arr.map(callback(currentValue), thisArg)
269
arr.map(callback(currentValue), thisArg)
255
arr.map(callback(currentValue), thisArg)
245

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
285

arr.map(callback(currentValue), thisArg)
43

_______143____2288

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
290

arr.map(callback(currentValue), thisArg)
43____2292

_______143____336____2295

arr.map(callback(currentValue), thisArg)
242
arr.map(callback(currentValue), thisArg)
297
arr.map(callback(currentValue), thisArg)
298
arr.map(callback(currentValue), thisArg)
299
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
29
arr.map(callback(currentValue), thisArg)
245

arr.map(callback(currentValue), thisArg)
26

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
304

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
306

_______143____336____2240

arr.map(callback(currentValue), thisArg)
241

arr.map(callback(currentValue), thisArg)
242
arr.map(callback(currentValue), thisArg)
243____629
arr.map(callback(currentValue), thisArg)
245

arr.map(callback(currentValue), thisArg)
43

_______143____3317

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
36
arr.map(callback(currentValue), thisArg)
295
arr.map(callback(currentValue), thisArg)
241

arr.map(callback(currentValue), thisArg)
242
arr.map(callback(currentValue), thisArg)
323
arr.map(callback(currentValue), thisArg)
298____2299
arr.map(callback(currentValue), thisArg)
255
arr.map(callback(currentValue), thisArg)
245

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43____3331

arr.map(callback(currentValue), thisArg)
43____3333

_______143____3335

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43____3338

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
20

arr.map(callback(currentValue), thisArg)
473

đầu ra.  

Bản đồ JavaScript

6. Bản đồ. nguyên mẫu. các mục () - Nó trả về một đối tượng lặp có chứa cặp khóa/giá trị cho từng phần tử có trong đối tượng Bản đồ.  

cú pháp.  

const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
2

7. Bản đồ. nguyên mẫu. keys() – Nó trả về một đối tượng iterator chứa tất cả các khóa có trong Map Object.  

cú pháp.  

[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
3

8. Bản đồ. nguyên mẫu. values() – Nó trả về một đối tượng iterator chứa tất cả các giá trị có trong Map Object.  

cú pháp.  

const employees = [
    { name: "Adam", salary: 5000, bonus: 500, tax: 1000 },
    { name: "Noah", salary: 8000, bonus: 1500, tax: 2500 },
    { name: "Fabiano", salary: 1500, bonus: 500, tax: 200 },
    { name: "Alireza", salary: 4500, bonus: 1000, tax: 900 },
];

// calculate the net amount to be given to the employees
const calcAmt = (obj) => {
    newObj = {};
    newObj.name = obj.name;
    newObj.netEarning = obj.salary + obj.bonus - obj.tax;
    return newObj;
};

let newArr = employees.map(calcAmt);

console.log(newArr);
3

Hãy sử dụng tất cả các phương pháp được mô tả ở trên.  

Thí dụ.  

Javascript




arr.map(callback(currentValue), thisArg)
42

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
478

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
22
arr.map(callback(currentValue), thisArg)
23______224
arr.map(callback(currentValue), thisArg)
483

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
486

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
488
arr.map(callback(currentValue), thisArg)
436
arr.map(callback(currentValue), thisArg)
48
arr.map(callback(currentValue), thisArg)
49
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
488
arr.map(callback(currentValue), thisArg)
438
arr.map(callback(currentValue), thisArg)
48
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
25
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
488
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
29
arr.map(callback(currentValue), thisArg)
48
[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
31
arr.map(callback(currentValue), thisArg)
204

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
206____1452____548
arr.map(callback(currentValue), thisArg)
460
arr.map(callback(currentValue), thisArg)
204

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
206____1454____548
arr.map(callback(currentValue), thisArg)
458
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43

_______143____3386

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43____3389

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
22
arr.map(callback(currentValue), thisArg)
392

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43

_______143____3396

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
398

arr.map(callback(currentValue), thisArg)
43____4200

arr.map(callback(currentValue), thisArg)
43____4202

_______143____4204

_______143____4206

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
36____4209
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
212
arr.map(callback(currentValue), thisArg)
299
arr.map(callback(currentValue), thisArg)
22
arr.map(callback(currentValue), thisArg)
215

_______143____4217

arr.map(callback(currentValue), thisArg)
43

_______143____4220

arr.map(callback(currentValue), thisArg)
43

_______143____4223

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
22
arr.map(callback(currentValue), thisArg)
226

arr.map(callback(currentValue), thisArg)
43

_______143____3396

arr.map(callback(currentValue), thisArg)
43____4231

_______143____4233

_______143____336____4236

arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
212
arr.map(callback(currentValue), thisArg)
299
arr.map(callback(currentValue), thisArg)
22
arr.map(callback(currentValue), thisArg)
242

_______143____4217

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
247

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43____4250

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
22
arr.map(callback(currentValue), thisArg)
253

arr.map(callback(currentValue), thisArg)
43

_______143____4256

_______143____4258

arr.map(callback(currentValue), thisArg)
43____4260

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
36
arr.map(callback(currentValue), thisArg)
263
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
212
arr.map(callback(currentValue), thisArg)
299
arr.map(callback(currentValue), thisArg)
22
arr.map(callback(currentValue), thisArg)
269

_______143____4217

arr.map(callback(currentValue), thisArg)
473

đầu ra.  

Bản đồ JavaScript

9. Bản đồ. nguyên mẫu. forEach() – Nó thực thi chức năng gọi lại một lần cho mỗi cặp khóa/giá trị trong Bản đồ, theo thứ tự chèn.  

cú pháp.  

[
  { name: 'Adam', netEarning: 4500 },
  { name: 'Noah', netEarning: 7000 },
  { name: 'Fabiano', netEarning: 1800 },
  { name: 'Alireza', netEarning: 4600 }
]
3

Hàm gọi lại được cung cấp với ba tham số như sau.  

  • chìa khóa phần tử
  • giá trị phần tử
  • đối tượng Bản đồ được duyệt qua

Thí dụ

Javascript




arr.map(callback(currentValue), thisArg)
42

_______143____4275

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
478

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
22
arr.map(callback(currentValue), thisArg)
23______224
arr.map(callback(currentValue), thisArg)
483

arr.map(callback(currentValue), thisArg)
283

arr.map(callback(currentValue), thisArg)
43____4285

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
488
arr.map(callback(currentValue), thisArg)
436
arr.map(callback(currentValue), thisArg)
48
arr.map(callback(currentValue), thisArg)
49
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
488
arr.map(callback(currentValue), thisArg)
438
arr.map(callback(currentValue), thisArg)
48
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
25
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
488
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
29
arr.map(callback(currentValue), thisArg)
48
[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
31
arr.map(callback(currentValue), thisArg)
204

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
206____1452____548
arr.map(callback(currentValue), thisArg)
460
arr.map(callback(currentValue), thisArg)
204

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
206____1454____548
arr.map(callback(currentValue), thisArg)
458
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
283

arr.map(callback(currentValue), thisArg)
43____5418

arr.map(callback(currentValue), thisArg)
43____5420

arr.map(callback(currentValue), thisArg)
43____5422

________ 143 ________ 5424 ________ 5425

arr.map(callback(currentValue), thisArg)
43____5427

arr.map(callback(currentValue), thisArg)
428____5429

arr.map(callback(currentValue), thisArg)
43____5431

arr.map(callback(currentValue), thisArg)
283

arr.map(callback(currentValue), thisArg)
43____5434

arr.map(callback(currentValue), thisArg)
43____5436

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
36____5439
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43____5442

arr.map(callback(currentValue), thisArg)
283

arr.map(callback(currentValue), thisArg)
43____5418

arr.map(callback(currentValue), thisArg)
43____5447

arr.map(callback(currentValue), thisArg)
43____5449

arr.map(callback(currentValue), thisArg)
43____5424
arr.map(callback(currentValue), thisArg)
452

arr.map(callback(currentValue), thisArg)
43____5427

arr.map(callback(currentValue), thisArg)
428
arr.map(callback(currentValue), thisArg)
456____5457
arr.map(callback(currentValue), thisArg)
458

arr.map(callback(currentValue), thisArg)
43____5431

arr.map(callback(currentValue), thisArg)
283

arr.map(callback(currentValue), thisArg)
43____5463

arr.map(callback(currentValue), thisArg)
43____5465

_______143____336____5468

arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43____5471

arr.map(callback(currentValue), thisArg)
283

arr.map(callback(currentValue), thisArg)
43____5418

arr.map(callback(currentValue), thisArg)
43____5476

arr.map(callback(currentValue), thisArg)
43____5424
arr.map(callback(currentValue), thisArg)
479

arr.map(callback(currentValue), thisArg)
43____5427

_______5428____5483

arr.map(callback(currentValue), thisArg)
428____5485

arr.map(callback(currentValue), thisArg)
428
arr.map(callback(currentValue), thisArg)
456____5457
arr.map(callback(currentValue), thisArg)
458

_______5428____5491

arr.map(callback(currentValue), thisArg)
43____5431

arr.map(callback(currentValue), thisArg)
283

_______143____5496

arr.map(callback(currentValue), thisArg)
43____5498

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
36
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
201
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43____6204

arr.map(callback(currentValue), thisArg)
473

đầu ra

Bản đồ JavaScript
Bản đồ JavaScript

Ghi chú. Trong ví dụ trên, chúng tôi sử dụng hàm gọi lại đơn giản chỉ in một phần tử trong bảng điều khiển, nó có thể được thiết kế để thực hiện bất kỳ thao tác phức tạp nào theo yêu cầu

10. Bản đồ. nguyên mẫu[@@iterator]() – Nó trả về một Map iterator function là phương thức entry() của đối tượng Map theo mặc định.  

cú pháp.  

arr.map(callback(currentValue), thisArg)
40

Thí dụ.  

Javascript




arr.map(callback(currentValue), thisArg)
473

arr.map(callback(currentValue), thisArg)
43____6208

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
478

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
22
arr.map(callback(currentValue), thisArg)
23______224
arr.map(callback(currentValue), thisArg)
483

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
486

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
488
arr.map(callback(currentValue), thisArg)
436
arr.map(callback(currentValue), thisArg)
48
arr.map(callback(currentValue), thisArg)
49
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
488
arr.map(callback(currentValue), thisArg)
438
arr.map(callback(currentValue), thisArg)
48
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
25
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
488
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
29
arr.map(callback(currentValue), thisArg)
48
[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
31
arr.map(callback(currentValue), thisArg)
204

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
206____1452____548
arr.map(callback(currentValue), thisArg)
460
arr.map(callback(currentValue), thisArg)
204

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
206____1454____548
arr.map(callback(currentValue), thisArg)
458
arr.map(callback(currentValue), thisArg)
38

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
251

arr.map(callback(currentValue), thisArg)
43____6253

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
22
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
256

arr.map(callback(currentValue), thisArg)
43

_______143____3396

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
398

arr.map(callback(currentValue), thisArg)
43____4200

arr.map(callback(currentValue), thisArg)
43____4202

_______143____4204

_______143____4206

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
212
arr.map(callback(currentValue), thisArg)
299
arr.map(callback(currentValue), thisArg)
22
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
274

arr.map(callback(currentValue), thisArg)
26
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
276

arr.map(callback(currentValue), thisArg)
473

đầu ra.  

Bản đồ JavaScript

11. Bản đồ được sử dụng để lặp qua các mảng

cú pháp

arr.map(callback(currentValue), thisArg)
41

Thí dụ

Javascript




arr.map(callback(currentValue), thisArg)
42

arr.map(callback(currentValue), thisArg)
43____6280

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43____6283

arr.map(callback(currentValue), thisArg)
43

_______143____6286

arr.map(callback(currentValue), thisArg)
43____6288

arr.map(callback(currentValue), thisArg)
26
arr.map(callback(currentValue), thisArg)
36____6291
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
292
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
293
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
294

arr.map(callback(currentValue), thisArg)
26
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
296
const prices = [1800, 2000, 3000, 5000, 500, 8000];

let newPrices = prices.map(Math.sqrt);

// [ 42.42640687119285, 44.721359549995796, 54.772255750516614, // 70.71067811865476, 22.360679774997898, 89.44271909999159 ] console.log(newPrices); // custom arrow function const string = "JavaScript"; const stringArr = string.split(''); // array with individual string character

let asciiArr = stringArr.map(x => x.charCodeAt(0));

// map() does not change the original array console.log(stringArr); // ['J', 'a', 'v', 'a','S', 'c', 'r', 'i', 'p', 't'] console.log(asciiArr); // [ 74, 97, 118, 97, 83, 99, 114, 105, 112, 116 ]
297

_______143____6299

arr.map(callback(currentValue), thisArg)
43

arr.map(callback(currentValue), thisArg)
43
[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
302

arr.map(callback(currentValue), thisArg)
43
[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
304

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
36
[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
307
[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
308

arr.map(callback(currentValue), thisArg)
43
[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
310

arr.map(callback(currentValue), thisArg)
43
arr.map(callback(currentValue), thisArg)
36
[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
313
[
  42.42640687119285,
  44.721359549995796,
  54.772255750516614,
  70.71067811865476,
  22.360679774997898,
  89.44271909999159
]
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[
   74,  97, 118,  97,
   83,  99, 114, 105,
  112, 116
]
314

arr.map(callback(currentValue), thisArg)
473

đầu ra

Bản đồ JavaScript

 

Ghi chú. - Chúng tôi có thể tạo một người dùng xác định có thể lặp lại thay vì sử dụng mặc định

Thẩm quyền giải quyết.  

https. // nhà phát triển. mozilla. org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

JavaScript được biết đến nhiều nhất để phát triển trang web nhưng nó cũng được sử dụng trong nhiều môi trường không có trình duyệt. Bạn có thể học JavaScript từ đầu bằng cách làm theo Hướng dẫn JavaScript và Ví dụ về JavaScript này

map() trong JavaScript là gì?

map() tạo một mảng mới từ việc gọi một hàm cho mọi phần tử mảng . map() gọi một hàm một lần cho mỗi phần tử trong một mảng. map() không thực thi chức năng cho các phần tử trống.

Khi nào sử dụng bản đồ trong JavaScript?

Bản đồ có thể được sử dụng khi bạn muốn thực hiện một hành động trên từng phần tử trong bộ sưu tập và thu thập kết quả vào một mảng mới . Trong JavaScript, "vòng lặp for" hoặc "làm tổ" có thể hoàn thành nhiệm vụ tương tự, nhưng chúng ta có thể sử dụng map() để tạo các hàm dễ đọc hơn. Bản đồ có thể được tạo và khởi tạo với cú pháp Map() mới.

map() trả về cái gì trong JavaScript?

Giá trị trả về. Nó trả về một mảng mới và các phần tử của mảng là kết quả của hàm gọi lại . Các ví dụ dưới đây minh họa việc sử dụng phương thức array map() trong JavaScript. ví dụ 1. Ví dụ này sử dụng phương thức array map() và trả về bình phương của phần tử mảng.

Làm cách nào để viết hàm bản đồ trong JavaScript?

Cú pháp của phương thức map() như sau. mảng. map(function(element, index, array){ }, this); Hàm gọi lại() được gọi trên từng phần tử mảng và map( .