简体   繁体   中英

Passing a function parameter within a function

I have a iterateThroughElements function that accepts an array or an object as the first argument. There is an optional function as the second argument.

If the user did not pass in the function second parameter, it will just iterate through the element and console log all of the elements.

If the user pass in the function it will do the exactly what the function does.

Here's my codes:

function filterArgReturnEvenElements(el) {
  return (typeof el === 'number') && !(el % 2);
}

function returnKeyWithArrayValue(arg, key){
  return arg[key] instanceof Array;
}


function iterateThroughElements(arg, myFunc){
  // if arg is an Array
  if (arg instanceof Array) {
   // if function exist then call specific function or callback
    if(myFunc && typeof(myFunc) == "function"){
      return arg.filter(myFunc);
    // else if function doesnt exist, iterate through array and console.log all the items
    }else{
       arg.forEach(item => console.log(item));
    }
  } 
    // if arg is an Object
    else if(arg instanceof Object) {
   // if function exist then call specific function or callback
    if(myFunc&& typeof(myFunc) == "function"){
    return Object.keys(arg).filter(key => myFunc(arg, key));
      // else if function doesnt exist, iterate through object and console.log all the items
    }else{
      for(key in arg){
      if(arg.hasOwnProperty(key)){
        console.log(key + " : " + arg[key]);
      }
    }
    }
}

let arr = [1, 2, 3, 4, 5, 7];
let obj = {
  firstname: 'mik',
  lastname: 'lasner',
  age: 21,
  favorite: ['hiking', 'surfing', 'beach']
};

let output = iterateThroughElements(arr, filterArgReturnEvenElements);
console.log(output);

let output2 = iterateThroughElements(obj, returnKeyWithArrayValue);
console.log(output2);

let output3 = iterateThroughElements(arr);
console.log(output);

let output4 = iterateThroughElements(obj);
console.log(output2);

As you can see above the filterArgReturnEvenElements function will return the items on the array that are even numbers.

While the returnKeyWithArrayValue function will return the key in an object that has an array value.

Right now my codes doesn't work. Where do you think did I go wrong? Please help!

IS THERE A BETTER WAY TO DO THIS?

Hi one bracket was missed and also callBack variable is not defined.

function filterArgReturnEvenElements(el) {
  return (typeof el === 'number') && !(el % 2);
}

function returnKeyWithArrayValue(arg, key){
  return arg[key] instanceof Array;
}


function iterateThroughElements(arg, myFunc){
  // if arg is an Array
  if (arg instanceof Array) {
    console.log("its an array");
   // if function exist then call specific callback
    if(myFunc && typeof(myFunc) == "function"){
      return arg.filter(myFunc);
    // else if function doesnt exist, iterate through array and console.log all the items
    }else{
       arg.forEach(item => console.log(item));
    }
  } 
    // if arg is an Object
    else if(arg instanceof Object) {
    // if function exist then call specific callback
    if(myFunc && typeof(myFunc) == "function"){
    return Object.keys(arg).filter(key => myFunc(arg, key));
      // else if function doesnt exist, iterate through object and console.log all the items
    }else{
      for(key in arg){
      if(arg.hasOwnProperty(key)){
        console.log(key + " : " + arg[key]);
      }
    }
    }
}
}
let arr = [1, 2, 3, 4, 5, 7];
let obj = {
  firstname: 'mik',
  lastname: 'lasner',
  age: 21,
  favorite: ['hiking', 'surfing', 'beach']
};
console.log("it works");
let output = iterateThroughElements(arr, filterArgReturnEvenElements);
console.log(output);

let output2 = iterateThroughElements(obj, returnKeyWithArrayValue);
console.log(output2);

let output3 = iterateThroughElements(arr);
console.log(output);

let output4 = iterateThroughElements(obj);
console.log(output2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM