Reputation: 5147
I have an array of animals arr = ['cat','dog','elephant','lion','tiger','mouse']
I want to write a function remove(['dog','lion'])
which can remove the elements from arr, can we write this function using es6 spread?
example:
arr = ['cat','dog','elephant','lion','tiger','mouse']
remove(['cat', 'lion'])
arr should get changed to
arr = ['dog','elephant','tiger','mouse']
Note: I don't want mutations, so please don't suggest solutions that mutates array.
Upvotes: 5
Views: 33656
Reputation: 1
If you're using Typescript, you can also use the Omit
utility type to remove specific keys from you object/ array.
interface Todo {
title: string;
description: string; // <--- assuming you want to omit `description` key
completed: boolean;
createdAt: number;
}
type TodoPreview = Omit<Todo, "description">;
the TodoPreview
type will now expect an object with the other members included but with decription
omitted
const todo: TodoPreview = {
title: "Clean room",
completed: false,
createdAt: 1615544252770,
};
const todo: TodoPreview = {
title: "Clean room",
completed: false,
description: 'this should not be here' // <--- TS compiler will complain
createdAt: 1615544252770,
};
Upvotes: 0
Reputation: 1
Similar to the previous filter solution, but using a regular expression. Beneficial if the remove array is large...
let arr = ['cat', 'dog', 'elephant', 'lion', 'tiger', 'mouse'];
const removeArr = ['cat', 'lion'];
const removeRegExp = new RegExp(removeArr.join('|'));
arr = arr.filter(e => !removeRegExp.test(e));
console.log(arr);
Upvotes: 0
Reputation: 55
I don't understand the selected answer was selected since it does not contain solution to the end goal of removing elements from an array
You can't use spread alone to remove, but you can slice and spread:
function removeOne(sourceList, value) {
const index = sourceList.indexOf(value);
if (index >= 0 && index < sourceList.length) {
return [
...sourceList.slice(0, index),
...sourceList.slice(index+1),
];
}
return sourceList;
}
function remove(sourceList, removeList) {
let res = [...sourceList]
removeList.forEach((item) => {
res = removeOne(res, item);
})
return res;
}
Upvotes: 3
Reputation: 422
you can use the array.filter method, it will return array with filtered values,
var filterMe = ["cat", "dog", "elephant", "lion", "tiger", "mouse"];
var answer = filterMe.filter((item) => item !== "dog")
.filter((item) => item !== "lion");
console.log(answer);
Upvotes: 4
Reputation: 22474
From what I understand you're looking for a function that has its argument defined using the spread syntax. Here is an example:
var arr = ['cat','dog','elephant','lion','tiger','mouse'];
function remove(...toRemove){
toRemove.forEach(item => {
var index = arr.indexOf(item);
if(index != -1){
arr.splice(index, 1);
}
})
}
remove('dog', 'lion'); // OR remove(...['dog', 'lion']);
console.log(arr);
This is actually changing the original array (it mutates it) you've mentioned that you're not looking for mutation but you've also mentioned this arr should get changed to...
Upvotes: 3
Reputation: 386680
You could take an iterator and exclude the values from iteration with spread operator.
Btw, it is not really advisable.
function remove(array, items) {
array[Symbol.iterator] = function* () {
for (let value of Object.values(this)) {
if (!items.includes(value)) {
yield value;
}
}
};
}
var array = ['cat', 'dog', 'elephant', 'lion', 'tiger', 'mouse'];
remove(array, ['dog', 'lion']);
console.log([...array]);
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 1
Reputation: 1075049
No, you can't, because they're not next to each other. There's been some discussion of taking spread and rest syntax further, but even in those discussions, I don't think a series of discontiguous selections would be possible.
I think the closest you can get is to call out the first few specifically and then use rest syntax for everything after 'lion'
:
const arr = ['cat','dog','elephant','lion','tiger','mouse'];
const arr2 = [arr[1], arr[2], ...arr.slice(4)];
console.log(arr2);
...which I'm sure isn't what you wanted to do. :-)
Upvotes: 4
Reputation: 1066
You can use spread if you want to pass lots of strings like remove('lion', 'dog')
. Otherwise I don't think spread can help you.
Upvotes: 1