Binding to closest value in keyed array

Hi all,

Any help on this would be much appreciated. I think it’s fairly simple but I’m getting lost in the available UI options and not sure how to hand code it.

I have a repeat and inside the repeat is an object (probabilities) which is a keyed array where the key is a numeric amount (the cost) and the values are cost, count, cumulativeprobability (all numbers). Cost is the same (value and key).

image

On the front end I have a range input going from the minimum to the maximum possible amounts. As you slide the slider, I want to bind a text output to the cumulativeprobability associated with the closest cost value.

For example (ignoring count for simplicity), if we had data
{1: [cost: 1, cumulativeprobability: 20%], 1.5: [cost: 1.5, cumulativeprobability: 50%], 2.5: [cost: 2.5, cumulativeprobability: 70%], 4: [cost: 4, cumulativeprobability: 100%]}

If you slide the range input to 2.1 you should see 70%, at 3.5 it would be 100% etc.

First larger value is also acceptable if easier than finding the actual closest.

In English, the code would read:
“From probabilities object, the cumulativeprobability value from the item with key closest to the range input value”

Hope that’s clear and thanks all!

Paul

First larger - something like this should work:

probabilities.sort('cost').where('cost',yourslider.value,'>=')[0]

Thanks Ben. This is where I’m running into issues as I can’t find any information on what is /isn’t acceptable syntax. I tried yours and I get an error:

parser.js:829 Formatter sort in expression [probabilities.sort(‘cost’).where(‘cost’,widerange.value.toNumber(),’>=’)[0]] doesn’t exist for type object

‘widerange’ is the range input id.

The data is already sorted, so I’ve tried various combinations of things like:
probabilities.cost.where(‘cost’,widerange.value.toNumber(),’>=’)[0] or probabilities.probability.where(‘cost’,widerange.value.toNumber(),’>=’)[0]

Both of those are just blank but without an error. It’s the probability field I need to display but I was hoping the first one might get me part way to an answer.

You could try:

probabilities.values().where('cost',widerange.value.toNumber(),'>=')[0]

.values() converts the object to an array

1 Like

Getting closer (I think!). It now shows [object Object]. Not sure how to get the probability value specifically?

If I do:
{{probabilities.values().where(‘cost.toNumber()’, widerange.value.toNumber(), ‘>=’).min()}}
it just says ‘infinity’
If I do:
{{probabilities.probability.values().where(‘cost.toNumber()’, widerange.value.toNumber(), ‘>=’).min()}}
Then it’s blank

I’ll double-check there isn’t a mistake in my data but I don’t think so

probabilities.values().where('cost',widerange.value.toNumber(),'>=')[0].cumulativeprobability

Just reference the property you want to show

Amazing - thank you.

Turns out there was a typo between my data store schema (which I was using) and the javascript generating the data and inserting it into the data store. So the field name was slightly different and hence why I was tearing my hair out when it was blank all the time.

Anyway - this now works perfectly so thanks again for your help!

1 Like