2021-04-30 10:12:52 +00:00
|
|
|
// we have 2 solutions here, an easier one and a more advanced one.
|
|
|
|
// The easiest way to get an array of all of the arguments that are passed to a function
|
2021-10-27 19:46:13 +00:00
|
|
|
// is using the rest operator. If this is unfamiliar to you look it up!
|
2022-05-28 20:19:49 +00:00
|
|
|
const removeFromArray = function (array, ...args) {
|
2021-04-30 10:12:52 +00:00
|
|
|
// create a new empty array
|
|
|
|
const newArray = [];
|
|
|
|
// use forEach to go through the array
|
|
|
|
array.forEach((item) => {
|
|
|
|
// push every element into the new array
|
|
|
|
// UNLESS it is included in the function arguments
|
|
|
|
// so we create a new array with every item, except those that should be removed
|
|
|
|
if (!args.includes(item)) {
|
|
|
|
newArray.push(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// and return that array
|
|
|
|
return newArray;
|
|
|
|
};
|
2017-11-24 20:23:16 +00:00
|
|
|
|
2021-04-30 10:12:52 +00:00
|
|
|
// A simpler, but more advanced way to do it is to use the 'filter' function,
|
|
|
|
// which basically does what we did with the forEach above.
|
|
|
|
|
2022-05-28 20:19:49 +00:00
|
|
|
// var removeFromArray = function(array, ...args) {
|
2021-04-30 10:12:52 +00:00
|
|
|
// return array.filter(val => !args.includes(val))
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
|
|
|
|
module.exports = removeFromArray;
|