0

I am trying to solve a problem I'm facing in Javascript, I am totally new to JS. This is my code:

    function filterOutStringfromArr(array) {
        var arr = []
        for (var i = 0; i < array.length; i++) {
            if (typeof array[i] === 'number') {
                arr[i] = array[i]
            }
        }
        console.log(arr)
    }
    filterOutStringfromArr([1,2,'A','B',123])

when I run this, I get this result: [ 1, 2, <2 empty items>, 123 ].

I know this is happening because the length of my array is 5. But I want to append just the filtered value to the empty array arr[]. How can I do this?

5
  • 2
    Use arr.push() instead of arr[i] = . Commented May 7, 2020 at 18:01
  • 1
    you can use filter function to achieve the same Commented May 7, 2020 at 18:02
  • @sirko when i try to arr.push() it just add the last element to empty array Commented May 7, 2020 at 18:04
  • @bair isnt that what you asked for? I want to append just the filtered value to the empty array To me that reads add the found item to the last element in the array. Are you looking for something different? Commented May 7, 2020 at 18:06
  • @gh9 i got my answer dear . thanks Commented May 7, 2020 at 18:08

5 Answers 5

2
function filterOutStringfromArr(array) {
    var arr = []
    for (var i = 0; i < array.length; i++) {
        if (typeof array[i] === 'number') {
          arr.push(array[i])
        }
    }
    console.log(arr)
}
filterOutStringfromArr([1,2,'A','B',123])

MDN link that is pretty good

You should use push that will append to the end of an array. By inserting the found element into spot i you are going from index 3 to 5 thus the empty elements.

Sign up to request clarification or add additional context in comments.

1 Comment

@bari no worries glad I could help.
1

I suggest to use the Array.prototype.push() method.

function filterOutStringsfromArray(array) {
    var arr = [];
    for (let i = 0; i < array.length; i++) {
        if (typeof array[i] === 'string') {
          arr.push(array[i]);
        }
    }
    return arr;
}

filterOutStringfromArr([5, 2, 'h', 2, 's', 2]) // Expected Output: ['h', 's']

Comments

1

Filter method will solve your issue.

function filterOutStringfromArr(array) {
    const newArray = array.filter(item => {
        if (typeof item === 'number') {
            return item;
        }
    })    
    console.log(newArray);
}

filterOutStringfromArr([1,2,'A','B',123])

Comments

1

Personally i would just use a normal array filter function and test the condition. let native array.filter function do the work for you. The below will filter the array and return a new array (not modify the existing array).

function filterToArrayOfNumbers(arrayElement) {
    return typeof arrayElement === 'number';
}
console.log([1,2,'A','B',123].filter(filterToArrayOfNumbers));

Comments

0
arr[i] = array[i]  // <------------  your issue

i is still being incremented, so when you are looking at your last number 123, (index 4), it will set the new array at index 4 (leaving 2 and 3 empty)

rather than setting it explicitly, you can do arr.push(array[i])

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.