array.find() is it working?
Hi, I am trying to do this array find tutorial to snapchat but it gives me error when I apply it to my project. I copy the exact tutorial but it gives error, here is my code
// -----JS CODE-----
var x = [
{
obj: "john",
pos: new vec3(0,0,0)
},
{
obj: "doe",
pos: new vec3(0,1,0)
}
];
function findPos(person){
return person.obj === "doe"
}
var a = x.find(findPos).pos
print(a)
Thanks for your help
array.find is an ES6 method, you will have to use an ES5 friendly way since LS is mostly ES5.
You can fix it with a polyfill, just add this code in your script. Make sure it is placed near the top, before anything calls it.
https://github.com/jsPolyfill/Array.prototype.find/blob/master/find.js
Note: you don't really need to add the 'use strict' for LS, though it does work in LS if you want to run the script in strict mode