Kim_Trager1 wrote:
Hi @Jeremy_Oglesby I tried all sorts of different ways to get the index from my record.
However .index does not seem to be valid.
Then I tried with various things, but kind of stuck getting the index of the any of the records.
So i’m not sure if the index is that easily available.
const luPlanRecords = await prodView.selectRecordsAsync();
console.log(luPlanRecords.records)
for (let record in luPlanRecords.records) {
//console.log(luPlanRecords.records.indexOf())
console.log(luPlanRecords.records[luPlanRecords.records.indexOf(record.id)])
//let recordAbove = luPlanRecords.records[record.index() -1]
}
Sorry about that, @Kim_Trager1 – I gave you a bad example. I’m no JavaScript wizard, and was trying patch together some help for you while on my phone.
I looked up the way I’ve done this in the past, and I’ve been able to use the Array.map() function to get the index of the record under iteration:
const luPlanRecords = await prodView.selectRecordsAsync();
let someNewArrayOfLuPlanRecords = luPlanRecords.records.map(record, index => {
let recordAbove = luPlanRecords.records[index - 1]
...
});
Array.map
allows you to express a variable to hold the current index under iteration as well (separated by a comma after the array object under iteration). So inside the map arrow function, you can directly ask for the index
of the record.
However, 2 things to note about this:
- as I said before, you need to be careful of the way this behaves with the first record - I’m not sure exactly what it will do, but you’ll need some sort of guard clause to treat the first record differently
- since this is a
.map
function, it’s not behaving quite the same way as a for loop
– you need to return something from the arrow function, and the ‘something’ that gets returned will be saved into the new array – not sure if this will work for what you are wanting to do; you can still process logic in the arrow function that’s unrelated to whatever gets returned, but it’s best to return some value that represents the effect of what you did in the arrow function
Hope that makes sense.
Another approach, if .map
isn’t quite working the way you want, is to just use an iteration counter with the for loop
, and let that iteration counter represent the value of the index under iteration:
const luPlanRecords = await prodView.selectRecordsAsync();
let index = 0
for (let record in luPlanRecords.records) {
console.log(index) //expect '0' on first loop, '1' on second, etc.
let recordAbove = luPlanRecords.records[index - 1];
... //processing logic
index++ //iterate index for next loop
}
This is more similar to what you are trying already. Again though, need to be careful with that first record.