Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
Array.filter is not a function

DouweDabbe
Guest
Quote
2022-02-15 00:58:02

in the scripting window I can use :
myArray.filter(myFunction);

but in my action is said:
myArray.filter(myFunction) is not a function

why ?


DouweDabbe
Guest
Quote
2022-02-15 03:13:28

This here, supposedly returns an array with only unique items, but it does not !!! why ???

Use helper array [ ]
function uniqueArray2(arr) {
var a = [];
for (var i=0, l=arr.length; i<l; i++)
if (a.indexOf(arr[i]) === -1 && arr[i] !== '')
a.push(arr[i]);
return a;
};

my input = integer array
myArray = [];
then myArray.push(10, 8, 3, 2, 8, 6,10);

// arrays can be defined as
// myArray = [1, 3, 5, 7]; and
// myArray = (2, 5,7 9,7);


just_in_case
Moderator
Quote
2022-02-15 03:52:57

I recently did something similar in my point light shader update to capture light data.

here is how you can do this, to remove duplicated items from your array. Thanks to @smnMhmdy for suggesting me to use Array.Filter.


function uniqueArray2(arr){
var newArray = [];
for(var i = 0; i < arr.length; i++){
var current = arr[i];
var found = ArrayElement(newArray, current);
if(found >= 0) continue;
newArray.push(current);
}
return newArray;
}

function ArrayElement(arr, element){
for(var i = 0; i < arr.length; i++){
var current = arr[i];
if(current == element)
return i;
}
return -1;
}


you can now use it like
print(uniqueArray2(YourArray)+"")


it will now print an array with unique items only. I am going to put this code in Code snippets on Neophyte.cf along with some more code snippets.
hope that helps


DouweDabbe
Guest
Quote
2022-02-15 21:25:29

Thanks

i got it from here and it maybe usefull for the snippet collection you are building:

https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates/1443895414438954

"Get all unique values in a JavaScript array (remove duplicates)"

I split all answers to 4 possible solutions:

Use object { } to prevent duplicates
Use helper array [ ]
Use filter + indexOf
Bonus! ES6 Sets method.



DouweDabbe
Guest
Quote
2022-02-20 14:50:33

is Object.keys() part of Coppercube ???

it not allowed in nthe scripting window while it seems old enough to be part of JS5.

quote]

Get array of object's keys - javascript - Stack Overflowhttps://stackoverflow.com › questions › get-array-of-ob...
6 Jan 2012
7 Jun 2011
8 Dec 2011
[/quote]

i do:

function uniqueArray1( ar ) {
var j = {};

ar.forEach( function(v) {
j[v+ '::' + typeof v] = v;
});

return Object.keys(j).map(function(v){
return j[v];
});
}

a = new array(1,2,3,4,5);
print("unik a="uniqArray(a) );

this version is supposed to be 1000 times faster as the helper array version.

thank you


just_in_case
Moderator
Quote
2022-02-21 04:50:47

Glad to know that you are satisfied with the results. :)


DouweDabbe
Guest
Quote
2022-02-21 21:34:03

Hi,

your function was more or less same as mine , but had more choises of monitoring in between steps.

My mistake was in using the function as a filter() action.
now it works,quite fast, but why not try object for even better performance ==> bug Object.keys()

still having problems though see this:


// one of these:
var arr = [0,1,2,3,4,5,6,7,8,9]; // is oke
const arr = [0,1,2,3,4,5,6,7,8,9]; // script:1 TypeError redeclaration of arr
print("arr="+arr); // oke just an array

var newArr = [];
while(arr.length)
newArr.push(arr.splice(0, 3) );
print("newArr="+newArr); // oke array copied

// but now click execute multiple times - surprise !!!


thanks


just_in_case
Moderator
Quote
2022-02-22 05:46:47

as you are executing the code in a while loop, it will be going to execute multiple times, as long as the condition for the loops is true, and you don't break the loop manually.

Also, you should use curly brackets parenthesis in your while loop. it's not the right way to do a while loop.


csp-games
Guest
Quote
2022-02-23 21:40:31

I would guess without the curly brackets it will loop the following command only, just like the "if(condition) command;" case.


DouweDabbe
Guest
Quote
2022-02-24 02:53:23

if exec one time then output =
arr= 0,1,2,3,4,5,6,7,8,9
newArr= 0,1,2,3,4,5,6,7,8,9

if exec 5 times then output is =
arr= 0,1,2,3,4,5,6,7,8,9
newArr= 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9 // huh?

which i did not expect:
I asssumed arr and newArr to be newly defined
every time I click exec in the CC scripting window.
I assume the newArr points to the same memory adress every time.

[quot]Also, you should use curly brackets parenthesis[/quot]
it is not obligatory but the loop can contain ONLY ONE SINGLE statement.
the code statement is finished by the : dotcomma

It is old style coding when people used small screens and even single line editors, I learned programming in late 1980s early 1990s, pascal and later some selftought C and Fortran77/90.
I used VDE (video display editor) a very innovative editor that could load files {High} into video buffer so i had more ram for the compiler. It new wordstar commands an it still exists, a 125 kb wonder claimed to be written in assembler.
And my computer a "Vobis HighSceen 386 with 387 copro of 40Mhz costed some 3500 dutch guilders almost equal to 6500 euros today. Boot time around 90 seconds easy.
Today I code on an 10 year old notebook, a bit crappy, second hand no more than 100 euro and even that is dubious with these specs. But hey its very economical. Energy prices are up 40 percent this winter due to politics.


just_in_case
Moderator
Quote
2022-02-24 05:45:02

The Reason you are having

newArr = 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9;

is because you are adding it to the previously created array, every time you execute the command.

print("newArr="+newArr);


you need to remove the '+' sign there. then it will always print newArr = 0,1,2,3,4,5,6,7,8,9;


regarding the While Lopp, I am already aware that the braces are not obligatory when you want to loop a single line only, but it is always a good practice to use them whenever you are using while, for, if-else statements and loops. As I am still learning coding and programs, I found that it makes the code more readable, but again it is one's own preferences how to code.

I wasn't even born when you started learning, huge respect for you. I prefer to be an old-school guy, I have this strange Nostalgia about everything from the 90s to 2005, which was like the golden era of my life. Music, Movies, Games, Toys, Cars, Books, like everything of that era, seems far better than today's era to me. even though there was less technology and hardware limitation. In today's world, nothing gives me that happiness and joy like those early days of my life.
even the food lost its taste in today's world. The same food during the old days tasted 100 times better than today's food.

I don't know if you agree with me or not, but man 90s was the best era to live in, and we are lucky enough that we witnessed and enjoyed that era.


csp-games
Guest
Quote
2022-02-24 11:58:06

Not sure about the "+" sign, isn't he just printing a string expression?

Anyway, this:
while(arr.length)
newArr.push(arr.splice(0, 3) );
should result in:
3,4,5,6,7,8,9,6,7,8,9

as splice (as far as I see) first removes the first 3 entries of arr, then pushes the rest to newArr, and repeats so until arr length is 0. I'm surprised it didn't throw an error, as it tries to splice off 3 entries when only 1 is left over. 10/3 =3.333

What do you actually want to achieve anyway? Arrays can be stubborn. Esp. when the code is experimental. Ever tried to access an array index like 3.4 ? You would expect it to deliver value of index 3 - but no, all hell breaks loose.


csp-games
Guest
Quote
2022-02-24 12:01:22

oops sorry I meant
3,4,5,6,7,8,9,6,7,8,9,9

also, "not a function" errors I get a lot when I try to use a function with the wrong type of argument or parameter, for instance numeric instead of string.


just_in_case
Moderator
Quote
2022-02-24 13:13:57

Aah @dieter , you are right, sometimes I am just a dodo brain, I completely ignored that he was printing the string and not adding the actual array.
Extremely Sorry, for the mis-information there.

Also , I just tried the code and it wasn't giving me the result described by the author, it was printing the content of the array, only once, also I did removed the const that was redeclaring the arr variable.

here is a screenshot of the result in the debugging panel.
🔎︎



DouweDabbe
Guest
Quote
2022-02-24 15:24:50

i can declare an array as a var but
not as a const

const someArr = [];
solely this statement will throw an error:
redeclaration of var someArr

also for ( let i=0; i<someArr; i++ )
will throw errors until i replace let with var

I am working on some vertex/edges based pathfinding routines
difficulty here is the human players movement, vertex layout can be really weird and therefore dont know beforehand where player can go.

also need to rewrite a js library for tile based path finding.

After that coppercube can handle any terrain for path finding, be it a park like environment or a crowded city.

I also want to know if you know if Coppercube has the data types of : List, Set, Stack, Queue and Dictionary. and how to adress them. This would reduce these librarys quite a bit in size.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Inte?national" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact