Một cách tiếp cận chức năng để có được các chữ số từ number
sẽ là nhận
var num = 123456;
var array = num.toString[]
.split['']
.map[function[item, index] {
return parseInt[item];
}]
.reduce[function[previousValue, currentValue, index, array] {
return previousValue + currentValue;
}, 0];
console.log[array]; // returns 21
0 từ number
của bạn, chia nó thành var num = 123456;
var array = num.toString[]
.split['']
.map[function[item, index] {
return parseInt[item];
}]
.reduce[function[previousValue, currentValue, index, array] {
return previousValue + currentValue;
}, 0];
console.log[array]; // returns 21
2 [của các ký tự] và ánh xạ từng phần tử trở lại một số.Ví dụ:
var number = 123456;
var array = number.toString[]
.split['']
.map[function[item, index] {
return parseInt[item];
}];
console.log[array]; // returns [1, 2, 3, 4, 5, 6]
Nếu bạn cũng cần tổng hợp tất cả các chữ số, bạn có thể nối phương thức
var num = 123456;
var array = num.toString[]
.split['']
.map[function[item, index] {
return parseInt[item];
}]
.reduce[function[previousValue, currentValue, index, array] {
return previousValue + currentValue;
}, 0];
console.log[array]; // returns 21
3 vào mã trước đó:var num = 123456;
var array = num.toString[]
.split['']
.map[function[item, index] {
return parseInt[item];
}]
.reduce[function[previousValue, currentValue, index, array] {
return previousValue + currentValue;
}, 0];
console.log[array]; // returns 21
Thay vào đó, với ECMAScript 2015 [Phiên bản thứ 6], bạn có thể sử dụng các chức năng mũi tên:
var number = 123456;
var array = number.toString[].split[''].map[[item, index] => parseInt[item]];
console.log[array]; // returns [1, 2, 3, 4, 5, 6]
Nếu bạn cần tổng hợp tất cả các chữ số, bạn có thể nối phương thức
var num = 123456;
var array = num.toString[]
.split['']
.map[function[item, index] {
return parseInt[item];
}]
.reduce[function[previousValue, currentValue, index, array] {
return previousValue + currentValue;
}, 0];
console.log[array]; // returns 21
3 vào mã trước đó:var num = 123456;
var result = num.toString[]
.split['']
.map[[item, index] => parseInt[item]]
.reduce[[previousValue, currentValue] => previousValue + currentValue, 0];
console.log[result]; // returns 21
Chia một số thành một mảng #
Để chia một số thành một mảng:
- Chuyển đổi số thành một chuỗi.
- Sử dụng phương thức
5 để chuyển đổi chuỗi thành một mảng các chữ số.var num = 123456; var array = num.toString[] .split[''] .map[function[item, index] { return parseInt[item]; }] .reduce[function[previousValue, currentValue, index, array] { return previousValue + currentValue; }, 0]; console.log[array]; // returns 21
- Sử dụng hàm
6 để chuyển đổi từng chuỗi trong mảng thành một số.var num = 123456; var array = num.toString[] .split[''] .map[function[item, index] { return parseInt[item]; }] .reduce[function[previousValue, currentValue, index, array] { return previousValue + currentValue; }, 0]; console.log[array]; // returns 21
Copied!
const number = 1234; const arrOfDigits = Array.from[String[number], Number]; console.log[arrOfDigits]; // 👉️ [1, 2, 3, 4]
Tham số đầu tiên chúng tôi chuyển đến phương thức mảng. Từ một đối tượng có thể lặp lại mà chúng tôi muốn chuyển đổi thành một mảng.
Chúng tôi đã phải chuyển đổi số thành một chuỗi vì các số không được điều chỉnh.
Copied!
// 👇️ ['1', '2', '3', '4'] console.log[Array.from[String[1234]]];
Tại thời điểm này, chúng tôi có một loạt các chuỗi. Chúng tôi đã sử dụng tham số thứ hai của phương thức
7 để chuyển đổi từng chuỗi thành một số.var num = 123456;
var array = num.toString[]
.split['']
.map[function[item, index] {
return parseInt[item];
}]
.reduce[function[previousValue, currentValue, index, array] {
return previousValue + currentValue;
}, 0];
console.log[array]; // returns 21
Tham số thứ hai mà phương thức lấy là một hàm mảng.map.
Hàm được gọi với mỗi phần tử trong mảng. Chúng tôi muốn chuyển đổi từng phần tử thành một số, vì vậy chúng tôi sử dụng đối tượng số.
Dưới đây là một ví dụ sử dụng rõ ràng phương thức
var num = 123456;
var array = num.toString[]
.split['']
.map[function[item, index] {
return parseInt[item];
}]
.reduce[function[previousValue, currentValue, index, array] {
return previousValue + currentValue;
}, 0];
console.log[array]; // returns 21
8.Copied!
const number = 1234; // 👇️ ['1', '2', '3', '4'] const arrOfStr = Array.from[String[number]]; const arrOfNum = arrOfStr.map[str => Number[str]]; console.log[arrOfNum]; // 👉️ [1, 2, 3, 4]
Phương thức
var num = 123456;
var array = num.toString[]
.split['']
.map[function[item, index] {
return parseInt[item];
}]
.reduce[function[previousValue, currentValue, index, array] {
return previousValue + currentValue;
}, 0];
console.log[array]; // returns 21
9 được gọi với mỗi chuỗi trong mảng.Trên mỗi lần lặp, chúng tôi chuyển đổi phần tử hiện tại thành một số và trả về kết quả.
Ngoài ra, bạn có thể sử dụng phương pháp
var number = 123456;
var array = number.toString[].split[''].map[[item, index] => parseInt[item]];
console.log[array]; // returns [1, 2, 3, 4, 5, 6]
0.Để chia một số thành một mảng:
- Chuyển đổi số thành một chuỗi.
- Sử dụng phương thức
5 để chuyển đổi chuỗi thành một mảng các chữ số.var num = 123456; var array = num.toString[] .split[''] .map[function[item, index] { return parseInt[item]; }] .reduce[function[previousValue, currentValue, index, array] { return previousValue + currentValue; }, 0]; console.log[array]; // returns 21
- Sử dụng hàm
6 để chuyển đổi từng chuỗi trong mảng thành một số.var num = 123456; var array = num.toString[] .split[''] .map[function[item, index] { return parseInt[item]; }] .reduce[function[previousValue, currentValue, index, array] { return previousValue + currentValue; }, 0]; console.log[array]; // returns 21
Copied!
// Supported in IE const number = 1234; const arr = String[number] .split[''] .map[str => Number[str]]; console.log[arr]; // 👉️ [1, 2, 3, 4]
Tham số đầu tiên chúng tôi chuyển đến phương thức mảng. Từ một đối tượng có thể lặp lại mà chúng tôi muốn chuyển đổi thành một mảng.
Chúng tôi đã phải chuyển đổi số thành một chuỗi vì các số không được điều chỉnh.
Copied!
const number = 1234; // 👇️ ['1', '2', '3' ,'4'] console.log[String[number].split['']];
Tại thời điểm này, chúng tôi có một loạt các chuỗi. Chúng tôi đã sử dụng tham số thứ hai của phương thức
7 để chuyển đổi từng chuỗi thành một số.var num = 123456;
var array = num.toString[]
.split['']
.map[function[item, index] {
return parseInt[item];
}]
.reduce[function[previousValue, currentValue, index, array] {
return previousValue + currentValue;
}, 0];
console.log[array]; // returns 21
Tham số thứ hai mà phương thức lấy là một hàm mảng.map.
Hàm được gọi với mỗi phần tử trong mảng. Chúng tôi muốn chuyển đổi từng phần tử thành một số, vì vậy chúng tôi sử dụng đối tượng số.
- Dưới đây là một ví dụ sử dụng rõ ràng phương thức
8.var num = 123456; var array = num.toString[] .split[''] .map[function[item, index] { return parseInt[item]; }] .reduce[function[previousValue, currentValue, index, array] { return previousValue + currentValue; }, 0]; console.log[array]; // returns 21
- Phương thức
9 được gọi với mỗi chuỗi trong mảng.var num = 123456; var array = num.toString[] .split[''] .map[function[item, index] { return parseInt[item]; }] .reduce[function[previousValue, currentValue, index, array] { return previousValue + currentValue; }, 0]; console.log[array]; // returns 21