Bạn có thể sử dụng một số phương thức để xóa [các] mục khỏi một mảng:
//1
someArray.shift[]; // first element removed
//2
someArray = someArray.slice[1]; // first element removed
//3
someArray.splice[0, 1]; // first element removed
//4
someArray.pop[]; // last element removed
//5
someArray = someArray.slice[0, someArray.length - 1]; // last element removed
//6
someArray.length = someArray.length - 1; // last element removed
Nếu bạn muốn xóa phần tử tại vị trí
**Results**
4, hãy sử dụng:someArray.splice[x, 1];
Hoặc
someArray = someArray.slice[0, x].concat[someArray.slice[-x]];
Trả lời nhận xét của @Chill182: Bạn có thể xóa một hoặc nhiều phần tử khỏi một mảng bằng cách sử dụng
**Results**
5 hoặc
**Results**
6 kết hợp với
**Results**
7 [xem MDN].MDN].Xem dự án Stackblitz này hoặc đoạn trích bên dưới:
// non destructive filter > noJohn = John removed, but someArray will not change
let someArray = getArray[];
let noJohn = someArray.filter[ el => el.name !== "John" ];
log[`let noJohn = someArray.filter[ el => el.name !== "John"]`,
`non destructive filter [noJohn] =`, format[noJohn]];
log[`**someArray.length ${someArray.length}`];
// destructive filter/reassign John removed > someArray2 =
let someArray2 = getArray[];
someArray2 = someArray2.filter[ el => el.name !== "John" ];
log["",
`someArray2 = someArray2.filter[ el => el.name !== "John" ]`,
`destructive filter/reassign John removed [someArray2] =`,
format[someArray2]];
log[`**someArray2.length after filter ${someArray2.length}`];
// destructive splice /w findIndex Brian remains > someArray3 =
let someArray3 = getArray[];
someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1];
someArray3.splice[someArray3.findIndex[v => v.name === "John"], 1];
log["",
`someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1],`,
`destructive splice /w findIndex Brian remains [someArray3] =`,
format[someArray3]];
log[`**someArray3.length after splice ${someArray3.length}`];
// if you're not sure about the contents of your array,
// you should check the results of findIndex first
let someArray4 = getArray[];
const indx = someArray4.findIndex[v => v.name === "Michael"];
someArray4.splice[indx, indx >= 0 ? 1 : 0];
log["", `someArray4.splice[indx, indx >= 0 ? 1 : 0]`,
`check findIndex result first [someArray4] = [nothing is removed]`,
format[someArray4]];
log[`**someArray4.length [should still be 3] ${someArray4.length}`];
// -- helpers --
function format[obj] {
return JSON.stringify[obj, null, " "];
}
function log[...txt] {
document.querySelector["pre"].textContent += `${txt.join["\n"]}\n`
}
function getArray[] {
return [ {name: "Kristian", lines: "2,5,10"},
{name: "John", lines: "1,19,26,96"},
{name: "Brian", lines: "3,9,62,36"} ];
}
**Results**
Đã trả lời ngày 5 tháng 4 năm 2012 lúc 8:11Apr 5, 2012 at 8:11
KooiinckooiincKooiInc
Huy hiệu vàng 115K3131 gold badges141 silver badges176 bronze badges
8
Giải pháp sạch sẽ là sử dụng
**Results**
5:var filtered = someArray.filter[function[el] { return el.Name != "Kristian"; }];
Vấn đề với điều này là nó không hoạt động trên IE noJohn = John removed, but someArray will not change let someArray = getArray[]; let noJohn = someArray.filter[ el => el.name !== "John" ]; log[`let noJohn = someArray.filter[ el => el.name !== "John"]`, `non destructive filter [noJohn] =`, format[noJohn]]; log[`**someArray.length ${someArray.length}`]; // destructive filter/reassign John removed > someArray2 = let someArray2 = getArray[]; someArray2 = someArray2.filter[ el => el.name !== "John" ]; log["", `someArray2 = someArray2.filter[ el => el.name !== "John" ]`, `destructive filter/reassign John removed [someArray2] =`, format[someArray2]]; log[`**someArray2.length after filter ${someArray2.length}`]; // destructive splice /w findIndex Brian remains > someArray3 = let someArray3 = getArray[]; someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1]; someArray3.splice[someArray3.findIndex[v => v.name === "John"], 1]; log["", `someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1],`, `destructive splice /w findIndex Brian remains [someArray3] =`, format[someArray3]]; log[`**someArray3.length after splice ${someArray3.length}`]; // if you're not sure about the contents of your array, // you should check the results of findIndex first let someArray4 = getArray[]; const indx = someArray4.findIndex[v => v.name === "Michael"]; someArray4.splice[indx, indx >= 0 ? 1 : 0]; log["", `someArray4.splice[indx, indx >= 0 ? 1 : 0]`, `check findIndex result first [someArray4] = [nothing is removed]`, format[someArray4]]; log[`**someArray4.length [should still be 3] ${someArray4.length}`]; // -- helpers -- function format[obj] { return JSON.stringify[obj, null, " "]; } function log[...txt] { document.querySelector["pre"].textContent += `${txt.join["\n"]}\n` } function getArray[] { return [ {name: "Kristian", lines: "2,5,10"}, {name: "John", lines: "1,19,26,96"}, {name: "Brian", lines: "3,9,62,36"} ]; }0
Đã trả lời ngày 5 tháng 3 năm 2017 lúc 9:48Mar 5, 2017 at 9:48
solanki...solanki...solanki...
4.6022 Huy hiệu vàng24 Huy hiệu bạc29 Huy hiệu đồng2 gold badges24 silver badges29 bronze badges
2
Nếu bạn muốn xóa tất cả các lần xuất hiện của một đối tượng đã cho [dựa trên một số điều kiện] thì hãy sử dụng phương thức mối nối JavaScript bên trong A cho vòng lặp.
Vì việc loại bỏ một đối tượng sẽ ảnh hưởng đến độ dài mảng, hãy đảm bảo giảm một bước, để kiểm tra độ dài đó vẫn còn nguyên vẹn.
// non destructive filter > noJohn = John removed, but someArray will not change
let someArray = getArray[];
let noJohn = someArray.filter[ el => el.name !== "John" ];
log[`let noJohn = someArray.filter[ el => el.name !== "John"]`,
`non destructive filter [noJohn] =`, format[noJohn]];
log[`**someArray.length ${someArray.length}`];
// destructive filter/reassign John removed > someArray2 =
let someArray2 = getArray[];
someArray2 = someArray2.filter[ el => el.name !== "John" ];
log["",
`someArray2 = someArray2.filter[ el => el.name !== "John" ]`,
`destructive filter/reassign John removed [someArray2] =`,
format[someArray2]];
log[`**someArray2.length after filter ${someArray2.length}`];
// destructive splice /w findIndex Brian remains > someArray3 =
let someArray3 = getArray[];
someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1];
someArray3.splice[someArray3.findIndex[v => v.name === "John"], 1];
log["",
`someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1],`,
`destructive splice /w findIndex Brian remains [someArray3] =`,
format[someArray3]];
log[`**someArray3.length after splice ${someArray3.length}`];
// if you're not sure about the contents of your array,
// you should check the results of findIndex first
let someArray4 = getArray[];
const indx = someArray4.findIndex[v => v.name === "Michael"];
someArray4.splice[indx, indx >= 0 ? 1 : 0];
log["", `someArray4.splice[indx, indx >= 0 ? 1 : 0]`,
`check findIndex result first [someArray4] = [nothing is removed]`,
format[someArray4]];
log[`**someArray4.length [should still be 3] ${someArray4.length}`];
// -- helpers --
function format[obj] {
return JSON.stringify[obj, null, " "];
}
function log[...txt] {
document.querySelector["pre"].textContent += `${txt.join["\n"]}\n`
}
function getArray[] {
return [ {name: "Kristian", lines: "2,5,10"},
{name: "John", lines: "1,19,26,96"},
{name: "Brian", lines: "3,9,62,36"} ];
}
1Đoạn mã ở trên loại bỏ tất cả các đối tượng có tuổi lớn hơn 20.
Ksugiarto
9301 Huy hiệu vàng19 Huy hiệu bạc38 Huy hiệu đồng1 gold badge19 silver badges38 bronze badges
Đã trả lời ngày 15 tháng 6 năm 2018 lúc 20:10Jun 15, 2018 at 20:10
ObaidobaidObaid
2.49515 huy hiệu bạc14 Huy hiệu đồng15 silver badges14 bronze badges
Câu trả lời này
// non destructive filter > noJohn = John removed, but someArray will not change
let someArray = getArray[];
let noJohn = someArray.filter[ el => el.name !== "John" ];
log[`let noJohn = someArray.filter[ el => el.name !== "John"]`,
`non destructive filter [noJohn] =`, format[noJohn]];
log[`**someArray.length ${someArray.length}`];
// destructive filter/reassign John removed > someArray2 =
let someArray2 = getArray[];
someArray2 = someArray2.filter[ el => el.name !== "John" ];
log["",
`someArray2 = someArray2.filter[ el => el.name !== "John" ]`,
`destructive filter/reassign John removed [someArray2] =`,
format[someArray2]];
log[`**someArray2.length after filter ${someArray2.length}`];
// destructive splice /w findIndex Brian remains > someArray3 =
let someArray3 = getArray[];
someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1];
someArray3.splice[someArray3.findIndex[v => v.name === "John"], 1];
log["",
`someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1],`,
`destructive splice /w findIndex Brian remains [someArray3] =`,
format[someArray3]];
log[`**someArray3.length after splice ${someArray3.length}`];
// if you're not sure about the contents of your array,
// you should check the results of findIndex first
let someArray4 = getArray[];
const indx = someArray4.findIndex[v => v.name === "Michael"];
someArray4.splice[indx, indx >= 0 ? 1 : 0];
log["", `someArray4.splice[indx, indx >= 0 ? 1 : 0]`,
`check findIndex result first [someArray4] = [nothing is removed]`,
format[someArray4]];
log[`**someArray4.length [should still be 3] ${someArray4.length}`];
// -- helpers --
function format[obj] {
return JSON.stringify[obj, null, " "];
}
function log[...txt] {
document.querySelector["pre"].textContent += `${txt.join["\n"]}\n`
}
function getArray[] {
return [ {name: "Kristian", lines: "2,5,10"},
{name: "John", lines: "1,19,26,96"},
{name: "Brian", lines: "3,9,62,36"} ];
}
2không làm việc cho nhiều hồ sơ đáp ứng điều kiện. Nếu bạn có hai hồ sơ liên tiếp như vậy, chỉ có hồ sơ đầu tiên bị xóa và bản khác đã bỏ qua. Bạn phải sử dụng:
// non destructive filter > noJohn = John removed, but someArray will not change
let someArray = getArray[];
let noJohn = someArray.filter[ el => el.name !== "John" ];
log[`let noJohn = someArray.filter[ el => el.name !== "John"]`,
`non destructive filter [noJohn] =`, format[noJohn]];
log[`**someArray.length ${someArray.length}`];
// destructive filter/reassign John removed > someArray2 =
let someArray2 = getArray[];
someArray2 = someArray2.filter[ el => el.name !== "John" ];
log["",
`someArray2 = someArray2.filter[ el => el.name !== "John" ]`,
`destructive filter/reassign John removed [someArray2] =`,
format[someArray2]];
log[`**someArray2.length after filter ${someArray2.length}`];
// destructive splice /w findIndex Brian remains > someArray3 =
let someArray3 = getArray[];
someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1];
someArray3.splice[someArray3.findIndex[v => v.name === "John"], 1];
log["",
`someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1],`,
`destructive splice /w findIndex Brian remains [someArray3] =`,
format[someArray3]];
log[`**someArray3.length after splice ${someArray3.length}`];
// if you're not sure about the contents of your array,
// you should check the results of findIndex first
let someArray4 = getArray[];
const indx = someArray4.findIndex[v => v.name === "Michael"];
someArray4.splice[indx, indx >= 0 ? 1 : 0];
log["", `someArray4.splice[indx, indx >= 0 ? 1 : 0]`,
`check findIndex result first [someArray4] = [nothing is removed]`,
format[someArray4]];
log[`**someArray4.length [should still be 3] ${someArray4.length}`];
// -- helpers --
function format[obj] {
return JSON.stringify[obj, null, " "];
}
function log[...txt] {
document.querySelector["pre"].textContent += `${txt.join["\n"]}\n`
}
function getArray[] {
return [ {name: "Kristian", lines: "2,5,10"},
{name: "John", lines: "1,19,26,96"},
{name: "Brian", lines: "3,9,62,36"} ];
}
3thay vì .
Shiv Kumar
8961 Huy hiệu vàng9 Huy hiệu bạc 20 Huy hiệu Đồng1 gold badge9 silver badges20 bronze badges
Đã trả lời ngày 27 tháng 7 năm 2018 lúc 9:48Jul 27, 2018 at 9:48
JarmopjarmopJarmoP
1812 Huy hiệu bạc3 Huy hiệu đồng2 silver badges3 bronze badges
Dường như có một lỗi trong cú pháp mảng của bạn, vì vậy giả sử bạn có nghĩa là một mảng trái ngược với một đối tượng, mảng.splice là bạn của bạn ở đây:
// non destructive filter > noJohn = John removed, but someArray will not change
let someArray = getArray[];
let noJohn = someArray.filter[ el => el.name !== "John" ];
log[`let noJohn = someArray.filter[ el => el.name !== "John"]`,
`non destructive filter [noJohn] =`, format[noJohn]];
log[`**someArray.length ${someArray.length}`];
// destructive filter/reassign John removed > someArray2 =
let someArray2 = getArray[];
someArray2 = someArray2.filter[ el => el.name !== "John" ];
log["",
`someArray2 = someArray2.filter[ el => el.name !== "John" ]`,
`destructive filter/reassign John removed [someArray2] =`,
format[someArray2]];
log[`**someArray2.length after filter ${someArray2.length}`];
// destructive splice /w findIndex Brian remains > someArray3 =
let someArray3 = getArray[];
someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1];
someArray3.splice[someArray3.findIndex[v => v.name === "John"], 1];
log["",
`someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1],`,
`destructive splice /w findIndex Brian remains [someArray3] =`,
format[someArray3]];
log[`**someArray3.length after splice ${someArray3.length}`];
// if you're not sure about the contents of your array,
// you should check the results of findIndex first
let someArray4 = getArray[];
const indx = someArray4.findIndex[v => v.name === "Michael"];
someArray4.splice[indx, indx >= 0 ? 1 : 0];
log["", `someArray4.splice[indx, indx >= 0 ? 1 : 0]`,
`check findIndex result first [someArray4] = [nothing is removed]`,
format[someArray4]];
log[`**someArray4.length [should still be 3] ${someArray4.length}`];
// -- helpers --
function format[obj] {
return JSON.stringify[obj, null, " "];
}
function log[...txt] {
document.querySelector["pre"].textContent += `${txt.join["\n"]}\n`
}
function getArray[] {
return [ {name: "Kristian", lines: "2,5,10"},
{name: "John", lines: "1,19,26,96"},
{name: "Brian", lines: "3,9,62,36"} ];
}
4Đã trả lời ngày 5 tháng 4 năm 2012 lúc 8:13Apr 5, 2012 at 8:13
Khăn quàng cổ SimonSimon Scarfe
9.1504 Huy hiệu vàng27 Huy hiệu bạc32 Huy hiệu Đồng4 gold badges27 silver badges32 bronze badges
Bạn cũng có thể sử dụng
var filtered = someArray.filter[function[el] { return el.Name != "Kristian"; }];
4:// non destructive filter > noJohn = John removed, but someArray will not change
let someArray = getArray[];
let noJohn = someArray.filter[ el => el.name !== "John" ];
log[`let noJohn = someArray.filter[ el => el.name !== "John"]`,
`non destructive filter [noJohn] =`, format[noJohn]];
log[`**someArray.length ${someArray.length}`];
// destructive filter/reassign John removed > someArray2 =
let someArray2 = getArray[];
someArray2 = someArray2.filter[ el => el.name !== "John" ];
log["",
`someArray2 = someArray2.filter[ el => el.name !== "John" ]`,
`destructive filter/reassign John removed [someArray2] =`,
format[someArray2]];
log[`**someArray2.length after filter ${someArray2.length}`];
// destructive splice /w findIndex Brian remains > someArray3 =
let someArray3 = getArray[];
someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1];
someArray3.splice[someArray3.findIndex[v => v.name === "John"], 1];
log["",
`someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1],`,
`destructive splice /w findIndex Brian remains [someArray3] =`,
format[someArray3]];
log[`**someArray3.length after splice ${someArray3.length}`];
// if you're not sure about the contents of your array,
// you should check the results of findIndex first
let someArray4 = getArray[];
const indx = someArray4.findIndex[v => v.name === "Michael"];
someArray4.splice[indx, indx >= 0 ? 1 : 0];
log["", `someArray4.splice[indx, indx >= 0 ? 1 : 0]`,
`check findIndex result first [someArray4] = [nothing is removed]`,
format[someArray4]];
log[`**someArray4.length [should still be 3] ${someArray4.length}`];
// -- helpers --
function format[obj] {
return JSON.stringify[obj, null, " "];
}
function log[...txt] {
document.querySelector["pre"].textContent += `${txt.join["\n"]}\n`
}
function getArray[] {
return [ {name: "Kristian", lines: "2,5,10"},
{name: "John", lines: "1,19,26,96"},
{name: "Brian", lines: "3,9,62,36"} ];
}
5Đã trả lời ngày 6 tháng 7 năm 2017 lúc 18:59Jul 6, 2017 at 18:59
Artur Grigioartur GrigioArtur Grigio
4.8597 Huy hiệu vàng40 Huy hiệu bạc62 Huy hiệu Đồng7 gold badges40 silver badges62 bronze badges
Đây là những gì tôi sử dụng.
// non destructive filter > noJohn = John removed, but someArray will not change
let someArray = getArray[];
let noJohn = someArray.filter[ el => el.name !== "John" ];
log[`let noJohn = someArray.filter[ el => el.name !== "John"]`,
`non destructive filter [noJohn] =`, format[noJohn]];
log[`**someArray.length ${someArray.length}`];
// destructive filter/reassign John removed > someArray2 =
let someArray2 = getArray[];
someArray2 = someArray2.filter[ el => el.name !== "John" ];
log["",
`someArray2 = someArray2.filter[ el => el.name !== "John" ]`,
`destructive filter/reassign John removed [someArray2] =`,
format[someArray2]];
log[`**someArray2.length after filter ${someArray2.length}`];
// destructive splice /w findIndex Brian remains > someArray3 =
let someArray3 = getArray[];
someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1];
someArray3.splice[someArray3.findIndex[v => v.name === "John"], 1];
log["",
`someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1],`,
`destructive splice /w findIndex Brian remains [someArray3] =`,
format[someArray3]];
log[`**someArray3.length after splice ${someArray3.length}`];
// if you're not sure about the contents of your array,
// you should check the results of findIndex first
let someArray4 = getArray[];
const indx = someArray4.findIndex[v => v.name === "Michael"];
someArray4.splice[indx, indx >= 0 ? 1 : 0];
log["", `someArray4.splice[indx, indx >= 0 ? 1 : 0]`,
`check findIndex result first [someArray4] = [nothing is removed]`,
format[someArray4]];
log[`**someArray4.length [should still be 3] ${someArray4.length}`];
// -- helpers --
function format[obj] {
return JSON.stringify[obj, null, " "];
}
function log[...txt] {
document.querySelector["pre"].textContent += `${txt.join["\n"]}\n`
}
function getArray[] {
return [ {name: "Kristian", lines: "2,5,10"},
{name: "John", lines: "1,19,26,96"},
{name: "Brian", lines: "3,9,62,36"} ];
}
6Sau đó, nó đơn giản như nói
// non destructive filter > noJohn = John removed, but someArray will not change
let someArray = getArray[];
let noJohn = someArray.filter[ el => el.name !== "John" ];
log[`let noJohn = someArray.filter[ el => el.name !== "John"]`,
`non destructive filter [noJohn] =`, format[noJohn]];
log[`**someArray.length ${someArray.length}`];
// destructive filter/reassign John removed > someArray2 =
let someArray2 = getArray[];
someArray2 = someArray2.filter[ el => el.name !== "John" ];
log["",
`someArray2 = someArray2.filter[ el => el.name !== "John" ]`,
`destructive filter/reassign John removed [someArray2] =`,
format[someArray2]];
log[`**someArray2.length after filter ${someArray2.length}`];
// destructive splice /w findIndex Brian remains > someArray3 =
let someArray3 = getArray[];
someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1];
someArray3.splice[someArray3.findIndex[v => v.name === "John"], 1];
log["",
`someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1],`,
`destructive splice /w findIndex Brian remains [someArray3] =`,
format[someArray3]];
log[`**someArray3.length after splice ${someArray3.length}`];
// if you're not sure about the contents of your array,
// you should check the results of findIndex first
let someArray4 = getArray[];
const indx = someArray4.findIndex[v => v.name === "Michael"];
someArray4.splice[indx, indx >= 0 ? 1 : 0];
log["", `someArray4.splice[indx, indx >= 0 ? 1 : 0]`,
`check findIndex result first [someArray4] = [nothing is removed]`,
format[someArray4]];
log[`**someArray4.length [should still be 3] ${someArray4.length}`];
// -- helpers --
function format[obj] {
return JSON.stringify[obj, null, " "];
}
function log[...txt] {
document.querySelector["pre"].textContent += `${txt.join["\n"]}\n`
}
function getArray[] {
return [ {name: "Kristian", lines: "2,5,10"},
{name: "John", lines: "1,19,26,96"},
{name: "Brian", lines: "3,9,62,36"} ];
}
7Thay thế bất kỳ số nào thay cho ba. Sau khi đầu ra dự kiến là:
// non destructive filter > noJohn = John removed, but someArray will not change
let someArray = getArray[];
let noJohn = someArray.filter[ el => el.name !== "John" ];
log[`let noJohn = someArray.filter[ el => el.name !== "John"]`,
`non destructive filter [noJohn] =`, format[noJohn]];
log[`**someArray.length ${someArray.length}`];
// destructive filter/reassign John removed > someArray2 =
let someArray2 = getArray[];
someArray2 = someArray2.filter[ el => el.name !== "John" ];
log["",
`someArray2 = someArray2.filter[ el => el.name !== "John" ]`,
`destructive filter/reassign John removed [someArray2] =`,
format[someArray2]];
log[`**someArray2.length after filter ${someArray2.length}`];
// destructive splice /w findIndex Brian remains > someArray3 =
let someArray3 = getArray[];
someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1];
someArray3.splice[someArray3.findIndex[v => v.name === "John"], 1];
log["",
`someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1],`,
`destructive splice /w findIndex Brian remains [someArray3] =`,
format[someArray3]];
log[`**someArray3.length after splice ${someArray3.length}`];
// if you're not sure about the contents of your array,
// you should check the results of findIndex first
let someArray4 = getArray[];
const indx = someArray4.findIndex[v => v.name === "Michael"];
someArray4.splice[indx, indx >= 0 ? 1 : 0];
log["", `someArray4.splice[indx, indx >= 0 ? 1 : 0]`,
`check findIndex result first [someArray4] = [nothing is removed]`,
format[someArray4]];
log[`**someArray4.length [should still be 3] ${someArray4.length}`];
// -- helpers --
function format[obj] {
return JSON.stringify[obj, null, " "];
}
function log[...txt] {
document.querySelector["pre"].textContent += `${txt.join["\n"]}\n`
}
function getArray[] {
return [ {name: "Kristian", lines: "2,5,10"},
{name: "John", lines: "1,19,26,96"},
{name: "Brian", lines: "3,9,62,36"} ];
}
8Đã trả lời ngày 5 tháng 4 năm 2018 lúc 0:03Apr 5, 2018 at 0:03
Matthias Smatthias sMatthias S
1851 Huy hiệu bạc7 Huy hiệu đồng1 silver badge7 bronze badges
1
Tôi đoán các câu trả lời rất phân nhánh và thắt nút.
Bạn có thể sử dụng đường dẫn sau để xóa một đối tượng mảng phù hợp với đối tượng được đưa ra trong biệt ngữ JavaScript hiện đại.
// non destructive filter > noJohn = John removed, but someArray will not change
let someArray = getArray[];
let noJohn = someArray.filter[ el => el.name !== "John" ];
log[`let noJohn = someArray.filter[ el => el.name !== "John"]`,
`non destructive filter [noJohn] =`, format[noJohn]];
log[`**someArray.length ${someArray.length}`];
// destructive filter/reassign John removed > someArray2 =
let someArray2 = getArray[];
someArray2 = someArray2.filter[ el => el.name !== "John" ];
log["",
`someArray2 = someArray2.filter[ el => el.name !== "John" ]`,
`destructive filter/reassign John removed [someArray2] =`,
format[someArray2]];
log[`**someArray2.length after filter ${someArray2.length}`];
// destructive splice /w findIndex Brian remains > someArray3 =
let someArray3 = getArray[];
someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1];
someArray3.splice[someArray3.findIndex[v => v.name === "John"], 1];
log["",
`someArray3.splice[someArray3.findIndex[v => v.name === "Kristian"], 1],`,
`destructive splice /w findIndex Brian remains [someArray3] =`,
format[someArray3]];
log[`**someArray3.length after splice ${someArray3.length}`];
// if you're not sure about the contents of your array,
// you should check the results of findIndex first
let someArray4 = getArray[];
const indx = someArray4.findIndex[v => v.name === "Michael"];
someArray4.splice[indx, indx >= 0 ? 1 : 0];
log["", `someArray4.splice[indx, indx >= 0 ? 1 : 0]`,
`check findIndex result first [someArray4] = [nothing is removed]`,
format[someArray4]];
log[`**someArray4.length [should still be 3] ${someArray4.length}`];
// -- helpers --
function format[obj] {
return JSON.stringify[obj, null, " "];
}
function log[...txt] {
document.querySelector["pre"].textContent += `${txt.join["\n"]}\n`
}
function getArray[] {
return [ {name: "Kristian", lines: "2,5,10"},
{name: "John", lines: "1,19,26,96"},
{name: "Brian", lines: "3,9,62,36"} ];
}
9Đã trả lời ngày 10 tháng 6 năm 2020 lúc 6:40Jun 10, 2020 at 6:40
Splice [i, 1] trong đó tôi là chỉ số gia tăng của mảng sẽ loại bỏ đối tượng. Nhưng hãy nhớ rằng Splice cũng sẽ đặt lại độ dài mảng, vì vậy hãy coi chừng 'không xác định'. Sử dụng ví dụ của bạn, nếu bạn xóa 'Kristian', thì trong lần thực hiện tiếp theo trong vòng lặp, tôi sẽ là 2 nhưng một số điều sẽ là chiều dài 1, do đó nếu bạn cố gắng xóa "John" bạn sẽ nhận được lỗi "không xác định" . Một giải pháp cho điều này mặc dù không thanh lịch là có bộ đếm riêng để theo dõi chỉ số của phần tử sẽ được gỡ bỏ.
Đã trả lời ngày 9 tháng 10 năm 2014 lúc 23:45Oct 9, 2014 at 23:45
MaksoodmaksoodMaksood
1.13014 Huy hiệu bạc19 Huy hiệu đồng14 silver badges19 bronze badges
Trả về chỉ các đối tượng từ mảng có tài sản
var filtered = someArray.filter[function[el] { return el.Name != "Kristian"; }];
5 không phải là "Kristian"
**Results**
0Demo:
**Results**
1
**Results**
2Đã trả lời ngày 9 tháng 2 năm 2017 lúc 18:42Feb 9, 2017 at 18:42
LegendslegendsLegends
Huy hiệu vàng 19.8K1212 gold badges91 silver badges120 bronze badges
Khái niệm này sử dụng Kendo Grid
**Results**
3Đã trả lời ngày 4 tháng 8 năm 2017 lúc 11:48Aug 4, 2017 at 11:48