11

I'm writing an application based on react-native and we know definitely we can use JavaScript codes in this project and all react projects.

But when I use below code the transpiler return Error to the simulator:

const mixer = (...arg) => arg.flat(9);

The error is: flat is not function

Why in react-native it is not define but in browser works well?

3
  • maybe it is not implemented in the user agent. Commented Oct 18, 2018 at 7:23
  • @NinaScholz, Thanks a lot but which user agent? Commented Oct 18, 2018 at 7:26
  • maybe where the transpiler is running ...? Commented Oct 18, 2018 at 7:34

4 Answers 4

5

React Native uses the JavaScriptCore engine which uses the ECMA-262 spec. The spec shows clearly that .flat is not supported and it is not included in the list of transformations for babel.

Write your own or install one of the many shims available on npm.

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

1 Comment

Oh, real thanks to your clarification. I wrote my own based on reduce and Array concat function. But I wanted to know why It doesn't support, thanks.
0

instead of arr.flat try using this function

function flatten(arr) {
    return Array.prototype.concat(...arr);
}

flatten(arr)

2 Comments

Actually my exact question is not a function. I said WHY. in other hand your answer is not true, because it just flat first floor of depth. I know how write it recursive and I used reduce function of JavaScript but I wanna WHY FLAT DOES NOT WORK THERE?
oh in that case, open the link below check the comments stackoverflow.com/questions/52283892/…
0

Is it supported on your platform? Array.prototype.flat

Link also contains implementations using reduce and concat as well as non-recursive.

3 Comments

Please check the OP's post: Why in browser it works well
please check my question, the last sentence, I said why it is not defined in react-native?
Well it is a normal array function that is not implemented everywhere. It isnt react native at all it is plain javascript. And I gues your react native is being executed on a platform where it is not supported?
0

const mixer = (...arg) => arg.flat(9);

The error is: flat is not function

Why in react-native it is not define but in browser works well?

My first guess is that the react-native transpiler is not interpreting your ...arg correctly, leading to an object that is not of Array type. You are trying to call .flat, which is a part of Array.prototype. Since arg could not be of type Array, it is unable to find .flat, thus leading to that error.

I am not certain though. Can you check the object type? You can use this code here below temporarily.

const mixer = (...arg) => { console.log(typeof arg); return []; }

What does it say? If it says Array, then you might operate on a platform that does not support that function yet. Using a polyfill can solve your problem here.

1 Comment

the react-native transpiler interpreted ...arg, it can not understand what is flat.

Your Answer

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