简体   繁体   中英

Is there a way to pass an array as an argument when defining a function in javascript?

const validateCred = arr => {
    let checkableArr = arr.pop();
    for (let i = arr.length - 1; i >= 0; i--) {
        arr[i]
        checkableArr.push(arr[i])
    }

}

When i run the code, I get an error saying that.push() is not a function, this is because checkableArr isn't an array,(I think) but checkableArr is a variation of arr (the argument that will be passed when the function is called) and the function has no way of knowing arr is going to be an array, is there any way to fix this?

You're getting that error, because you haven't made sure that the last item of the passed array ( arr ) is an array itself, but your function's logic requires it to be an array.

There are various ways to solve this, some of them have already been outlined by others (@hungerstar).

Check the last element of arr

One attempt is to ensure that the last element/item inside arr is an array and bail out if it isn't.

const validateCred = arr => {
  let lastItem = arr.pop();
  if (!Array.isArray(lastItem)) {
    throw new Error('validateCred :: Last item of passed array must be an array itself');
  }
  // ... rest of your code ...
}

Although that does not solve the root cause, it ensures you get a decent and descriptive message about what went wrong. It's possible to improve that by defining a fallback array in case the last item isn't an array itself. Something like this:

const validateCred = arr => {
  let lastItem = arr.pop();
  let checkableArr = Array.isArray(lastItem) ? lastItem : [];
  // ... rest of your code ...
}

One thing to note: If the last item may be an array with a value inside , you have to copy that value into the new array!

const validateCred = arr => {
  let lastItem = arr.pop();
  let checkableArr = Array.isArray(lastItem) ? lastItem : [lastItem]; // <--
  // ... rest of your code ...
}

HINT: The following answer is based on guessing. The name validateCred lets me assume you use it to validate credentials. However, that's just guessing because all the provided code does is taking the last item and then pushing the rest of the contents of arr reversely into it (= reversing and flattening)

Reversing and flattening

If all you want to do with validateCred is reversing and flattening (and you only target supporting environments), you can easily do that with a one-liner:

// impure version
const validateCred = arr => arr.reverse().flat();

// pure version
const validateCred = arr => arr.flat().reverse();

To support older environments as well, you can use .reduce and .concat instead of .flat :

// impure version
const validateCred = arr => arr.reverse().reduce((acc, x) => acc.concat(x), []);

// pure version
const validateCred = arr => arr.reduce((acc, x) => acc.concat(x), []).reverse();

Yes, we pass an array as an argument using call/apply method. In your code when you are using arr.pop() it gets converted to number/string depending upon what type of array you specified, I specified below integer value so checkableArr is now integer so because of this you are getting an error.

Corrected code is here. Just replace in your code like:

let checkableArr = arr; //arr.pop();

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