0

When I try to run this code without the Number(), it doesn't work.

ar filter = (arr, fn) => {
 let filteredArr = [];

   for(const i in arr){

      if(fn(arr[i], Number(i)))  filteredArr.push(arr[i])
      
   }

return filteredArr;
};

I don't understand why this code doesn't work without the Number(), if the index is some number, at least 0, it's a integer, then it should work... Could you help me? I would be really grateful.

5
  • how are you calling filter? what is the value of fn? hint: object property names are always type String Commented Aug 2, 2023 at 0:50
  • Are you sure it does not work? It looks good to me. What kind of error do you have Commented Aug 2, 2023 at 0:50
  • @Daniel Cruz When I tried, it returns: var i should be a integer. Apparently, when we use for(const i in arr), it treats it like an object, so this array becomes something like { "0": "Value", "2": "ValueTwo" }, then i == String. Commented Aug 6, 2023 at 17:38
  • @Jaromanda X It's a leetCode problem, Input: arr = [0,10,20,30], fn = function greaterThan10(n) { return n > 10; } Output: [20,30] Explanation: const newArray = filter(arr, fn); // [20, 30] The function filters out values that are not greater than 10. leetcode.com/problems/filter-elements-from-array Commented Aug 6, 2023 at 17:41
  • I am very grateful for the help of all of you, Thank you very much. Commented Aug 6, 2023 at 17:44

3 Answers 3

1

The for...in loop iterates over enumerable string properties. All the indexes used to access elements of an array are such properties, so you need to convert each index to a number when iterating with for (const i in arr).

Note that it is generally a very bad idea to use this form of loop when you want to access indexes. If you cannot use Array#filter, you could use Array#forEach which passes the index to its callback, or a standard index-based for loop.

arr.forEach((x, i) => {
    if (fn(x, i)) filteredArr.push(x);
});
Sign up to request clarification or add additional context in comments.

Comments

0

Because you use for...in:

var filter = (arr, fn) => {
    let filteredArr = [];
    for(const i in arr){
        console.log(typeof i)
        if(fn(arr[i], Number(i)))  filteredArr.push(arr[i])
    }
    return filteredArr;
};

This loop is used for objects and is not recommended for arrays.

You can try to use the loop for that consists of three optional expressions:

var filter = (arr, fn) => {
    let filteredArr = [];
    for(let i=0; i<arr.length; i++){
        if(fn(arr[i], i))  filteredArr.push(arr[i])
    }
    return filteredArr;
};

Comments

0

By map method of js

var filter = function(arr, fn) {
    let filterArr=[];
    arr.map((n,i)=>{
        if(fn(n,i)){
            filterArr.push(n);
        }
    });
    return filterArr;
};

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.