Nhận mảng phụ javascript

lập trình

jav

mảng

tách ra

webdev

mảng

1 nữa

[Chỉnh sửa]

VN

JavaScript - lấy mảng con từ mảng

2 người đóng góp

7 đóng góp

0 thảo luận

8 điểm

Được tạo bởi

Nhận mảng phụ javascript
Remy-Lebe

652

Trong bài viết này, chúng tôi xin hướng dẫn các bạn cách tách mảng trong JavaScript

1. Sum found between indexes 1 and 43 Ví dụ về phương pháp Sum found between indexes 1 and 44

Phương thức này lấy chỉ mục bắt đầu và chỉ mục giới hạn (chỉ mục bị loại trừ) làm đối số

// ONLINE-RUNNER:browser;

var array = [
    1, 2, 3, 'text 1', 'text 2', 'text 3', true, false

 /*
    0  1  2      3         4         5       6     7    <- positive indexes
   -8 -7 -6     -5        -4        -3      -2    -1    <- negative indexes
 */
];

var subArray1 = array.slice(0, 4);    // [ 1, 2, 3, 'text 1' ]
var subArray2 = array.slice(2, 4);    // [ 3, 'text 1' ]
var subArray3 = array.slice(4);       // [ 'text 2', 'text 3', true, false ]
var subArray4 = array.slice(-2);      // [ true, false ]
var subArray5 = array.slice(2, -4);   // [ 3, 'text 1' ]
var subArray6 = array.slice(-5, -2);  // [ 'text 1', 'text 2', 'text 3' ]

console.log(subArray1);
console.log(subArray2);
console.log(subArray3);
console.log(subArray4);
console.log(subArray5);
console.log(subArray6);

Đầu ra (với NodeJS)

[ 1, 2, 3, 'text 1' ]
[ 3, 'text 1' ]
[ 'text 2', 'text 3', true, false ]
[ true, false ]
[ 3, 'text 1' ]
[ 'text 1', 'text 2', 'text 3' ]

Người giới thiệu

  1. Chuỗi. nguyên mẫu. phương pháp phân tách - MDN Docs

tiêu đề thay thế

  1. JavaScript - cách lấy mảng con từ mảng?
  2. JavaScript - cách tách mảng?
  3. Làm cách nào để tách mảng trong JavaScript?
  4. JavaScript - cách lấy mảng con?
  5. JavaScript - nhận nhiều mục từ mảng

lập trình

bản đánh máy

mảng

tách ra

[Chỉnh sửa]

VN

TypeScript - lấy mảng con từ mảng

2 người đóng góp

7 đóng góp

0 thảo luận

0 điểm

Được tạo bởi

Nhận mảng phụ javascript
Romany-xứ Wales

502

Trong TypeScript có thể chia mảng theo cách sau

1. Sum found between indexes 1 and 43 Ví dụ về phương pháp Sum found between indexes 1 and 44

Phương thức này lấy chỉ mục bắt đầu và chỉ mục giới hạn (chỉ mục bị loại trừ) làm đối số

const array: any = [
    1, 2, 3, 'text 1', 'text 2', 'text 3', true, false

//  0  1  2      3         4         5       6     7    <- positive indexes
// -8 -7 -6     -5        -4        -3      -2    -1    <- negative indexes
];

const subArray1: any = array.slice(0, 4);   // [ 1, 2, 3, 'text 1' ]
const subArray2: any = array.slice(2, 4);   // [ 3, 'text 1' ]
const subArray3: any = array.slice(4);      // [ 'text 2', 'text 3', true, false ]
const subArray4: any = array.slice(-2);     // [ true, false ]
const subArray5: any = array.slice(2, -4);  // [ 3, 'text 1' ]
const subArray6: any = array.slice(-5, -2); // [ 'text 1', 'text 2', 'text 3' ]

console.log(subArray1);
console.log(subArray2);
console.log(subArray3);
console.log(subArray4);
console.log(subArray5);
console.log(subArray6);

Đầu ra (với NodeJS)

[ 1, 2, 3, 'text 1' ]
[ 3, 'text 1' ]
[ 'text 2', 'text 3', true, false ]
[ true, false ]
[ 3, 'text 1' ]
[ 'text 1', 'text 2', 'text 3' ]

Có thể có nhiều hơn một mảng con có tổng bằng tổng đã cho. Các giải pháp sau in đầu tiên subarray như vậy.  

Được đề xuất. Vui lòng giải quyết nó trên “PRACTICE” trước khi chuyển sang giải pháp.
 

Cách tiếp cận đơn giản. Một giải pháp đơn giản là xem xét từng mảng con một và kiểm tra tổng của từng mảng con. Chương trình sau thực hiện giải pháp đơn giản. Chạy hai vòng. vòng lặp bên ngoài chọn điểm bắt đầu I và vòng lặp bên trong thử tất cả các mảng con bắt đầu từ i.
Thuật toán.

  1. Duyệt mảng từ đầu đến cuối
  2. Từ mỗi chỉ mục bắt đầu một vòng lặp khác từ i đến cuối mảng để lấy tất cả các mảng con bắt đầu từ i, giữ một biến sum để tính tổng
  3. Đối với mọi chỉ mục trong cập nhật vòng lặp bên trong sum = sum + array[j]
  4. Nếu tổng bằng tổng đã cho thì in ra mảng con

Javascript




Sum found between indexes 1 and 4
7

Sum found between indexes 1 and 4
8

Sum found between indexes 1 and 4
9
Sum found between indexes 1 and 4
0

Sum found between indexes 1 and 4
1

Sum found between indexes 1 and 4
2

Sum found between indexes 1 and 4
9
Sum found between indexes 1 and 4
0

Sum found between indexes 1 and 4
9
Sum found between indexes 1 and 4
2

Sum found between indexes 1 and 4
3
Sum found between indexes 1 and 4
4

Sum found between indexes 1 and 4
5

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
7

Sum found between indexes 1 and 4
1

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
0

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
2
Sum found between indexes 1 and 4
3

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
5

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
7

Sum found between indexes 1 and 4
1

_______46____870

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
2
Sum found between indexes 1 and 4
73

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
5

Sum found between indexes 1 and 4
76
Sum found between indexes 1 and 4
77
Sum found between indexes 1 and 4
78

Sum found between indexes 1 and 4
76
Sum found between indexes 1 and 4
5

Sum found between indexes 1 and 4
81
Sum found between indexes 1 and 4
82
Sum found between indexes 1 and 4
83
Sum found between indexes 1 and 4
84

Sum found between indexes 1 and 4
85
Sum found between indexes 1 and 4
86
Sum found between indexes 1 and 4
87
Sum found between indexes 1 and 4
88

_______881____890____891

Sum found between indexes 1 and 4
76
Sum found between indexes 1 and 4
93

Sum found between indexes 1 and 4
76
Sum found between indexes 1 and 4
77
Sum found between indexes 1 and 4
96

Sum found between indexes 1 and 4
81
Sum found between indexes 1 and 4
98
Sum found between indexes 1 and 4
91

Sum found between indexes 1 and 4
76
Sum found between indexes 1 and 4
01

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
93

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
93

_______46____882____908

Sum found between indexes 1 and 4
09

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
90
Sum found between indexes 1 and 4
91

Sum found between indexes 1 and 4
93

Sum found between indexes 1 and 4
1

Sum found between indexes 1 and 4
15

Sum found between indexes 1 and 4
16

Sum found between indexes 1 and 4
17

Sum found between indexes 1 and 4
18

Sum found between indexes 1 and 4
19

Sum found between indexes 1 and 4
20

đầu ra

Sum found between indexes 1 and 4

Phân tích độ phức tạp.   

  • Độ phức tạp về thời gian. O(n^2) trong trường hợp xấu nhất.
    Vòng lặp lồng nhau được sử dụng để duyệt qua mảng nên độ phức tạp về thời gian là O(n^2)
  • Độ phức tạp của không gian. Ô(1).
    Vì cần thêm dung lượng liên tục.

Phương pháp tiếp cận hiệu quả. Có một ý tưởng nếu tất cả các phần tử của mảng là dương. Nếu một mảng con có tổng lớn hơn tổng đã cho thì không có khả năng thêm các phần tử vào mảng con hiện tại, tổng sẽ là x (tổng đã cho). Ý tưởng là sử dụng cách tiếp cận tương tự với cửa sổ trượt. Bắt đầu với một mảng con trống, thêm các phần tử vào mảng con cho đến khi tổng nhỏ hơn x. Nếu tổng lớn hơn x, hãy xóa các phần tử khỏi đầu mảng con hiện tại.
Thuật toán.

  1. Tạo ba biến, l=0, sum = 0
  2. Duyệt mảng từ đầu đến cuối
  3. Cập nhật biến sum bằng cách thêm phần tử hiện tại, sum = sum + array[i]
  4. Nếu tổng lớn hơn tổng đã cho, hãy cập nhật biến sum thành sum = sum – mảng[l] và cập nhật l thành, l++
  5. Nếu tổng bằng tổng đã cho, in mảng con và ngắt vòng lặp

Javascript




Sum found between indexes 1 and 4
7

Sum found between indexes 1 and 4
22

_______89____924

Sum found between indexes 1 and 4
9
Sum found between indexes 1 and 4
26

_______89____928

Sum found between indexes 1 and 4
3
Sum found between indexes 1 and 4
4

Sum found between indexes 1 and 4
5

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
93

Sum found between indexes 1 and 4
94

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
0

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
2
Sum found between indexes 1 and 4
99

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
5

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
03

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
05

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
07
Sum found between indexes 1 and 4
08

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
5

Sum found between indexes 1 and 4
76
Sum found between indexes 1 and 4
12

_______876____414

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
93

Sum found between indexes 1 and 4
94

_______46____419

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
21

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
77
Sum found between indexes 1 and 4
78

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
5

Sum found between indexes 1 and 4
76
Sum found between indexes 1 and 4
28

Sum found between indexes 1 and 4
76
Sum found between indexes 1 and 4
82

Sum found between indexes 1 and 4
76
Sum found between indexes 1 and 4
83
Sum found between indexes 1 and 4
33

Sum found between indexes 1 and 4
34
Sum found between indexes 1 and 4
35
Sum found between indexes 1 and 4
87
Sum found between indexes 1 and 4
37
Sum found between indexes 1 and 4
38
Sum found between indexes 1 and 4
09

Sum found between indexes 1 and 4
76
Sum found between indexes 1 and 4
90
Sum found between indexes 1 and 4
42

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
44

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
46

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
77
Sum found between indexes 1 and 4
49

Sum found between indexes 1 and 4
76
Sum found between indexes 1 and 4
51

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
44

_______46____882____908

Sum found between indexes 1 and 4
09

Sum found between indexes 1 and 4
6
Sum found between indexes 1 and 4
90
Sum found between indexes 1 and 4
60

Sum found between indexes 1 and 4
93

Sum found between indexes 1 and 4
62

Sum found between indexes 1 and 4
63

Sum found between indexes 1 and 4
17

Sum found between indexes 1 and 4
18

Sum found between indexes 1 and 4
19

Sum found between indexes 1 and 4
67

Sum found between indexes 1 and 4
20

đầu ra

Sum found between indexes 1 and 4

Phân tích độ phức tạp.   

  • Độ phức tạp về thời gian. Trên).
    Chỉ cần một lần duyệt mảng. Vậy độ phức tạp thời gian là O(n).
  • Độ phức tạp của không gian. Ô(1).
    Vì cần thêm dung lượng liên tục.

Vui lòng tham khảo bài viết đầy đủ về Tìm mảng con với tổng đã cho. Đặt 1 (Số không âm) để biết thêm chi tiết

Làm cách nào để lấy mảng phụ trong JavaScript?

Thuật toán. .
Duyệt mảng từ đầu đến cuối
Từ mỗi chỉ mục bắt đầu một vòng lặp khác từ i đến cuối mảng để lấy tất cả các mảng con bắt đầu từ i, giữ một biến sum để tính tổng
Đối với mọi chỉ mục trong cập nhật vòng lặp bên trong sum = sum + array[j]
Nếu tổng bằng tổng đã cho thì in ra mảng con

Làm cách nào để lấy một phần của mảng trong Java?

Tạo và làm trống mảng nguyên thủy có kích thước endIndex-startIndex. Sao chép các phần tử từ start Index đến end Index từ mảng ban đầu sang mảng lát. .
Lấy Mảng và start Index và end Index
Lấy lát cắt bằng Arrays. phương thức copyOfRange()
Trả về hoặc in lát của mảng

Làm cách nào để lấy phạm vi từ mảng JavaScript?

Mảng JavaScript - lấy phạm vi mục .
var newArr = mảng. lát (bắt đầu, kết thúc);
// nếu bạn muốn 3 đến 7 thì người dùng
var newArr = mảng. lát (2, 7);
// nếu bạn muốn từ một vị trí (ví dụ thứ 5) đến cuối mảng
var newArr = mảng. lát(4);

Làm cách nào để cắt mảng đối tượng trong JavaScript?

Mảng. nguyên mẫu. slice() Phương thức slice() trả về một bản sao nông của một phần của mảng vào một đối tượng mảng mới được chọn từ đầu đến cuối (không bao gồm phần cuối) trong đó phần đầu và phần cuối biểu thị chỉ số của . Mảng ban đầu sẽ không bị sửa đổi.