4
votes

I want to iterate over key value pairs from an object and display two items per row. I looked at other examples like this one Example but I couldn't figure out how to add keyvalue to this.

Here is my code:

<div *ngFor="let item of book.bookData.privateData | keyvalue; index as i; let even = even">
    <div fxFlex="100" fxLayout="row" *ngIf="even">
        <div fxFlex="50" fxLayout="column">
            Key: <b>{{book.bookData.privateData[i].key}}</b> and Value: <b>{{book.bookData.privateData[i].value}}</b>
        </div>
        <div fxFlex="50" fxLayout="column">
            Key: <b>{{book.bookData.privateData[i+1].key}}</b> and Value: <b>{{book.bookData.privateData[i+1].value}}</b>
        </div>
    </div>
</div>

This does not work since there is no key and value attribute on the privateData object, the attributes are assigned to item.

Thanks in advance for any help!

EDIT:

The following is a working example of what I'm trying to achieve, but it's clearly not an efficient way:

<div *ngFor="let item of bookState.bookData.privateData | keyvalue; index as i; let even = even">
    <div fxFlex="100" fxLayout="row" *ngIf="even">
        <div fxFlex="50">
            <div *ngFor="let deeperItem1 of bookState.bookData.privateData | keyvalue; index as i2">
                <div *ngIf="i2 === i">
                    <b>INDEX {{i2}} and {{i}} </b>Key: <b>{{deeperItem1.key}}</b> and Value: <b>{{deeperItem1.value}}</b>
                </div>
            </div>
        </div>
        <div fxFlex="50">
            <div *ngFor="let deeperItem2 of bookState.bookData.privateData | keyvalue; index as i3">
                <div *ngIf="(i3-1) === i">
                    <b>INDEX {{i3}} and {{i}} </b>Key: <b>{{deeperItem2.key}}</b> and Value: <b>{{deeperItem2.value}}</b>
                </div>
            </div>
        </div>
    </div>
</div>

EDIT2:

To specify the question: The problem is not that the keyvalue pipe is not working, the problem is how to efficiently add indexes to these to display x-amount (in my case 2) of items per row. Sorry for any misunderstanding!

2
could you add a schema of what are you looping on? what is in item ? - SeleM
What is the keyvalue pipe exactly doing or trying to do? Can you share the interface for pirvateData? - Caius
privateData contains an arbitrary amount of key value pairs as a JSON response. key and value are both strings. - lennertr

2 Answers

4
votes

It depends on the structure you are iterating. Here you can find the following example:

@Component({
  selector: 'keyvalue-pipe',
  template: `<span>
    <p>Object</p>
    <div *ngFor="let item of object | keyvalue">
      {{item.key}}:{{item.value}}
    </div>
    <p>Map</p>
    <div *ngFor="let item of map | keyvalue">
      {{item.key}}:{{item.value}}
    </div>
  </span>`
})
export class KeyValuePipeComponent {
  object: {[key: number]: string} = {2: 'foo', 1: 'bar'};
  map = new Map([[2, 'foo'], [1, 'bar']]);
}

In your case, if you do not have a key value structure, use the map case like this:

<div *ngFor="let item of book.bookData.privateData | keyvalue; let i=index">
    <div fxFlex="100" fxLayout="row" *ngIf="i%2 === 0">
        <div fxFlex="50" fxLayout="column">
            Key: <b>{{item.key}}</b> and Value: <b>{{item.value}}</b>
        </div>
        <div fxFlex="50" fxLayout="column" *ngIf="(book.bookData.privateData|keyvalue)[i+1]">
            Key: <b>{{(book.bookData.privateData|keyvalue)[i+1].key}}</b> and Value: <b>{{(book.bookData.privateData|keyvalue)[i+1].value}}</b>
        </div>
    </div>
</div>
-1
votes

@lennertr please update your angular version to 6.1.0 to use keyvalue pipe. because keyvalue is available from version 6. and please refer below demo.

https://stackblitz.com/edit/angular-keyvalue