Ion-select Array using booleans, possible?


#1

Hello,

I am asking myself if its possible to store ion-selected items differently.

At the momment my Backend stores the ion-selected Items like this.

myItems:
     |_0: "First_Item"            
     |_1: "Second_Item"   
     |_2: "Third_Item"   
     |_3: "Fourth_Item"

(You get the idea…)

What I want to achieve is to store the Items like this:
If i selected the Items i want them to become true.

myItems:
     |_First_Item: "true"            
     |_Second_Item: "true"   
     |_Third_Item: "true"   
     |_Fourth_Item: "true"


For this example my code looks like this:

              <ion-select [(ngModel)]="myItems" multiple="true" cancelText="Cancel" okText="OK">
                <ion-option value="First_Item">First</ion-option>
                <ion-option value="Second_Item">Second</ion-option>
                <ion-option value="Third_Item">Third</ion-option>
                <ion-option value="Fourth_Item">Fourth</ion-option>
                <ion-option value="Sixth_Item">Sixth</ion-option>
              </ion-select>

Maybe someone of you has an answer for this question.
Thanks in advance! :blush:


#2

It would be fairly straightforward to write functions to transform back and forth between the two representations:

interface BooleanMatrix { [k: string]: boolean }

sparseToBooleanMatrix(sparse: string[]): BooleanMatrix {
  let rv: BooleanMatrix = {};
  for (let k of sparse) {
    rv[k] = true;
  }
  return rv;
}

booleanMatrixToSparse(bm: BooleanMatrix): string[] {
  let rv = [];
  for (let k in bm) {
    if (bm[k]) {
      rv.push(k);
    }
  }
  return rv;
}