简体   繁体   English

如何通过确实改变该数组的进程从数组中删除元素?

[英]How does one remove elements from an array by a process that does mutate this array?

I have an array of objects like this one:我有一组像这样的对象:

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"},
];

The only thing I need to do is to delete/remove all the objects with categoryId = 256, mutating the jokes array.我唯一需要做的就是删除/移除所有 categoryId = 256 的对象,改变笑话数组。 I've tried to chain a filter with a splice method (ES6 approach), but I could not do it.我尝试使用拼接方法(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);

if you want a simple ES6 approach you can override the value of "jokes" using a filter.如果你想要一个简单的 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 )

The simplest way to do that in-place (modifying existing array) would be to iterate over the array, find indexes of the elements you want to delete and call .splice() , like this:就地执行此操作(修改现有数组)的最简单方法是遍历数组,找到要删除的元素的索引并调用.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)

The only thing I need to do is to delete/remove all the objects with categoryId = 256, mutating the jokes array .我唯一需要做的就是删除/移除categoryId = 256 的所有对象,改变笑话数组 I've tried to chain a filter with a splice method (ES6 approach), but I could not do it.尝试使用splice方法(ES6 方法)链接filter ,但我做不到。

There are cases, like the one the OP is experiencing, where one can not reassign a filtered result to the original source reference.在某些情况下,例如 OP 正在经历的情况,无法将过滤结果重新分配给原始源参考。 Like with...就像...

const arr = [1, 2, 3]; arr = arr.filter(/* ... */);

... which will fail. ...这将失败。 One also might need to deal with properties which are not allowed to be written but of cause still can be mutated.还可能需要处理不允许写入但原因仍然可以变异的属性。

With exactly the same approach, which the OP already tried ("...I've tried to chain a filter with a splice method...") , one can generalize a solution for this kind of problem into a mutating remove implementation, based on a callback, exactly of the kind that filter asks for, where this callback function is the condition of whether to remove an array item or not...使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM