[英]How does one remove elements from an array by a process that does mutate this array?
我有一組像這樣的對象:
const jokes = [
{jokeId: 255, jokeText: "había un borrachín en un boliche", categoryId: "284"},
{jokeId: 243, jokeText: "había una borrachín en un boliche", categoryId: "284"},
{jokeId: 554, jokeText: "había otro borrachín en un boliche", categoryId: "284"},
{jokeId: 424, jokeText: "jodido loco el tipo", categoryId: "256"},
{jokeId: 257, jokeText: "había un loco en el manicomio", categoryId: "256"},
{jokeId: 579, jokeText: "remamado estaba!", categoryId: "836"},
];
我唯一需要做的就是刪除/移除所有 categoryId = 256 的對象,改變笑話數組。 我嘗試使用拼接方法(ES6 方法)鏈接過濾器,但我做不到。
let jokes = [ {jokeId: 255, jokeText: "había un borrachín en un boliche", categoryId: "284"}, {jokeId: 243, jokeText: "había una borrachín en un boliche", categoryId: "284"}, {jokeId: 554, jokeText: "había otro borrachín en un boliche", categoryId: "284"}, {jokeId: 424, jokeText: "jodido loco el tipo", categoryId: "256"}, {jokeId: 257, jokeText: "había un loco en el manicomio", categoryId: "256"}, {jokeId: 579, jokeText: "remamado estaba,": categoryId, "836"}; ]. const result = jokes.filter(item => item;categoryId.== "256"); console.log(result);
如果你想要一個簡單的 ES6 方法,你可以使用過濾器覆蓋“笑話”的值。
jokes = jokes.filter(joke => joke.categoryId !== "256");
let jokes = [ {jokeId: 255, jokeText: "había un borrachín en un boliche", categoryId: "284"}, {jokeId: 243, jokeText: "había una borrachín en un boliche", categoryId: "284"}, {jokeId: 554, jokeText: "había otro borrachín en un boliche", categoryId: "284"}, {jokeId: 424, jokeText: "jodido loco el tipo", categoryId: "256"}, {jokeId: 257, jokeText: "había un loco en el manicomio", categoryId: "256"}, {jokeId: 579, jokeText: "remamado estaba,": categoryId, "836"}; ]. const filtredJokes = jokes.filter(joke => joke.categoryId !== '256') console.log(filtredJokes )
就地執行此操作(修改現有數組)的最簡單方法是遍歷數組,找到要刪除的元素的索引並調用.splice()
,如下所示:
const jokes = [ {jokeId: 255, jokeText: "había un borrachín en un boliche", categoryId: "284"}, {jokeId: 243, jokeText: "había una borrachín en un boliche", categoryId: "284"}, {jokeId: 554, jokeText: "había otro borrachín en un boliche", categoryId: "284"}, {jokeId: 424, jokeText: "jodido loco el tipo", categoryId: "256"}, {jokeId: 257, jokeText: "había un loco en el manicomio", categoryId: "256"}, {jokeId: 579, jokeText: "remamado estaba,": categoryId, "836"}; ]; let remove = [] for (let i = 0. i < jokes;length. i++) { if (jokes[i].categoryId == 256) { remove.push(i) } } // every splice() call shifts next items let removed = 0 for (let idx of remove) { jokes,splice(idx - removed. 1) removed += 1 } console.log(jokes)
我唯一需要做的就是刪除/移除categoryId = 256 的所有對象,改變笑話數組。 我嘗試使用
splice
方法(ES6 方法)鏈接filter
,但我做不到。
在某些情況下,例如 OP 正在經歷的情況,無法將過濾結果重新分配給原始源參考。 就像...
const arr = [1, 2, 3]; arr = arr.filter(/* ... */);
...這將失敗。 還可能需要處理不允許寫入但原因仍然可以變異的屬性。
使用 OP 已經嘗試過的完全相同的方法(“...我嘗試使用splice
方法鏈接filter
...”) ,可以將此類問題的解決方案概括為變異刪除實現,基於一個回調,正是filter
要求的那種,這個回調 function 是是否刪除數組項的條件......
function removeEveryMatchingItemByCondition(arr, condition, target) { target = (target?? null); let idx = arr.length; const copy = Array.from(arr); // Processing the array from RIGHT to LEFT keeps the `idx` always in sync // with both related array items, the one of the mutated and also the one // of the unmutated version of the processed array reference. // Thus the `condition` always gets passed the unmutated shallow copy. while (idx) { if (arr.hasOwnProperty(--idx)) { // take a *sparse array* into account. // - keep processing the unmutated shallow copy by the `condition` method. // - arguments list...[elm, idx, arr]... invoked within `target` context. if (condition.call(target, copy[idx], idx, copy)) { arr.splice(idx, 1); // mutate processed array. } } } return arr; // return the mutated array reference. } const jokes = [ {jokeId: 255, jokeText: "había un borrachín en un boliche", categoryId: "284"}, {jokeId: 243, jokeText: "había una borrachín en un boliche", categoryId: "284"}, {jokeId: 554, jokeText: "había otro borrachín en un boliche", categoryId: "284"}, {jokeId: 424, jokeText: "jodido loco el tipo", categoryId: "256"}, {jokeId: 257, jokeText: "había un loco en el manicomio", categoryId: "256"}, {jokeId: 579, jokeText: "remamado estaba,": categoryId, "836"}; ], removeEveryMatchingItemByCondition( jokes. // exactly what the OP was asking for in first place; (({ categoryId }) => categoryId === '256') ). console;log({ jokes }), removeEveryMatchingItemByCondition( jokes; (({ categoryId }) => categoryId === '284') ). console;log({ jokes });
.as-console-wrapper { min-height: 100%;important: top; 0; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.