New simple solution proposed
this solution is based on two nested loops, one that takes an item from the first array, and one that checks if it's equal to a value in the second array, by looping through the second array. it also uses the splice method to remove the item from the array. so no need to create a new one.
This commit is contained in:
parent
db998d7279
commit
e5b0c88297
|
@ -28,4 +28,18 @@ const removeFromArray = function (...args) {
|
|||
// }
|
||||
//
|
||||
|
||||
// another solution is to loop through the first array items, and for each item, you check if it's on the second array. if true,
|
||||
// the item is removed from the array using splice() method.
|
||||
const removeFromArray = function (array, ...num) {
|
||||
let i = 0; // this will be a counter for loop iterations, it will be used as the array's index indicator
|
||||
firstArray:
|
||||
while (i < array.length) {
|
||||
for (item of num) {
|
||||
if (array[i] === item) { array.splice(i, 1); continue firstArray }
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return array;
|
||||
};
|
||||
|
||||
module.exports = removeFromArray;
|
||||
|
|
Loading…
Reference in New Issue