Coding help, array operations

Array functions in PB are totally foreign to me and I can’t figure this out. Help appreciated. Everything I try gives me errors in syntax, etc that I can’t work out. I think the biggest problem I’m having has to do the fn notation in the Array Functions section in the PB help underneath where the code is entered. Examples are very sparse there.

I want to create an array identical to the FrequencyData array and then sort that created array to end up with a final array containing the four highest values along with the corresponding original index number for each of the four.

For example, I use the snippet below to create an array “indexArray” identical to array frequencyData but the contents of “indexArray” is always all zeros.

function fn(value, index, array){
value=value
index=index
array=array

}

export function beforeRender(delta) {

frequencyData.mapTo(indexedArray,fn)

}

What are your expecting mapTo to do?

It runs the function you give for every element of the array, and stores the return value in the destination array. Your ‘fn’ returns nothing, so nothing is stored.

var a = [1,2,3,4,5]
var b = array(5)
a.mapTo(b, (val, ix, arr) => val+2)

Will make b contain [3,4,5,6,7]

a.mapTo(b, (val, ix, arr) => ix)

Will make b contain [0,1,2,3,4]


The sorting will be tricky. You’d need to sortBy(indexArray, (a,b) => valueArray[a] - valueArray[b]) - that will only sort the index array, but based on the values in the valueArray. valueArray must be global.

A more straight-forward approach might be just to keep track of the top 4, and update them as you loop through the array.

(I’m not near a PB at the moment, so this might be slightly incorrect syntax)

Great! Thank you, ted! That’s all the help I needed. Your example, all by itself, if included in the PB inline help would be a great addition. I’ll give it a try later. So, I don’t need to write that separate function like I did in my example, just write it like you show it? If I want just to replicate the source array, just write:

a.mapTo(b, (val, ix, arr) => val) ?

You can write functions in any way that is valid JavaScript. It supports named functions like you had, anonymous functions without a name which can be stored in variables or passed as arguments, or lambdas which are shortcut syntax forms of anonymous functions.

function myNamedFunction(arg1, arg2) {
  return arg1 + arg2
}

var storedAnonymousFunction = function (arg1, arg2) {
  return arg1 + arg2
}

var storedLambdaWithBlock = (arg1, arg2) => { 
  return arg1 + arg2
}

var storedLambdaWithImplicitReturn = (arg1, arg2) => arg1 + arg2

All good, useful stuff. Thanks!

I got the array replication part to work fine with
a.mapTo(b, (val, ix, arr) => val)
like Ted wrote.

Looks like I left out the “return” word in my original function definition, but I didn’t go back yet to retest that with “return” added.

In the end, I wrote something to solve the entire problem without using array functions except for arraySort, but I’m surely going to come back to this for future reference.