I searched among many similar questions, but was unable to find a answer to this tricky one. I need a function that removes all the items that are not numbers from the array. But function should modify existing array, not create a new one. I tried to do something like this:
let array = [1, 'a', 'b', 2];
array = array.filter(item => typeof item !== "string");
console.log(array);
//--> [1, 2]
While I am getting the right result I fell the test on additional points, because according to MDN the result creates a new array with the right result, which I was neglected and not knew pletely.
So does anyone have the more experience than me in solving these, from the point of my thoughts I am trying to do something for hours with array.splice and for loops, but nothing near successful.Thank you in advance, and sorry for the long opening post.
I searched among many similar questions, but was unable to find a answer to this tricky one. I need a function that removes all the items that are not numbers from the array. But function should modify existing array, not create a new one. I tried to do something like this:
let array = [1, 'a', 'b', 2];
array = array.filter(item => typeof item !== "string");
console.log(array);
//--> [1, 2]
While I am getting the right result I fell the test on additional points, because according to MDN the result creates a new array with the right result, which I was neglected and not knew pletely.
So does anyone have the more experience than me in solving these, from the point of my thoughts I am trying to do something for hours with array.splice and for loops, but nothing near successful.Thank you in advance, and sorry for the long opening post.
Share Improve this question asked Oct 28, 2018 at 23:46 SrkiSrki 512 silver badges9 bronze badges 7- What difference does it make if you modify the existing array? It's very likely to be much faster to create a new array. – Pointy Commented Oct 28, 2018 at 23:47
- @Pointy While I agree with you pletely, the task was strict and specific and targeting to be a tricky one, harder than it seems. – Srki Commented Oct 28, 2018 at 23:51
-
1
Would need to iterate and use
Array#splice()
if requirements are that specific then. Realistically what you have done makes sense as it is if you don't need any of the other data in the future – charlietfl Commented Oct 28, 2018 at 23:51 - 2 Note that splicing in a loop is also tricky since it changes the array length. Best to work backwards (from end to beginning) to avoid issues – charlietfl Commented Oct 28, 2018 at 23:56
- I reckon those are two(array.splice(), for loop) only ways to go, but I tried to iterate first, and not knowing how to check for "string" and removing them from the array at the same time. It could be tricky because each time I do a splice, index numbers of next string is changing. I was trapped there somewhere.. – Srki Commented Oct 28, 2018 at 23:59
4 Answers
Reset to default 3You can use Array#splice()
in a reverse for
loop
Because you are working backwards anything you remove will not affect any of the indexes in the direction you are moving since the original elements still exist from 0
to current position
let array = [1, 'a', 'b', 2];
for (let i = array.length - 1; i >= 0; i--) {
if (typeof array[i] === "string") {// modify conditional as needed
array.splice(i, 1);
}
}
console.log(array);
//--> [1, 2]
You could always try sorting first, instead of looping. For example:
const array = [1, 'a', 'b', '2', 3, 2]
// sort into strings after everything else
array.sort(a => typeof a === 'string')
// splice the string parts away
array.splice(array.findIndex(i => typeof i === 'string'))
console.log(array)
//--> [1, 3, 2]
This works because the sort function is done in place, and basically says if the first pared item is a string, push it back in the sort. This keeps to your constraints, is much less prone to loop bugs, and has the added benefit of only calling splice
once.
But really, why are you constrained to mutate the original array? Seems unnecessary, and using filter
would make your code so much more readable.
You can do this with a pointer and a while
loop that checks whether the pointer has reached the end of the array:
let array = [1, 'a', 'b', 2, 3, 'c', 'd', 3];
let i = 0;
while (i < array.length) {
if (typeof array[i] === "string") array.splice(i, 1);
else i++;
}
console.log(array);
Note that if we decide to splice
something, we don't increment the pointer.
I suggest you to use while loop, which will start from the last element and splice the element with the help of Array.splice()
method based on the element type, you want to remove.
let array = [1, 'a', 'b', 2];
let i = array.length;
while (i--) {
if (typeof array[i] === 'string') {
array.splice(i, 1);
}
}
console.log(array);