Html, Place items from an array in the top/bottom <div> based on a property

SA-

I am trying to place items 'next to each other' in either the top or bottom section depending on a property called 'section':

<span *ngFor="let item in [{name:"name1",section:"bottom"},{name:"name2",section:"top"}, 
{name:"name3",section:"top"},{name:"name4",section:"bottom"},{name:"name5",section:"bottom"}]">
    <span *ngIf="item.section=='top'">{{ item.name }}<span>
</span>

The result should look like this:
<div> name2 name3 </div>
<div> name1 name4 name5 </div>

Any help please? I can't seem to figure it on my own, thanks.

Hadi Golkarian

you can use two *ngFor each including an *ngIf for every section(top/bottom) like this: (considering you have your data in array property called foo)

<div>
    <ng-container *ngFor="let item of foo">
        <span *ngIf="item.section=='top'">
          {{ item.name }}
        </span>
    </ng-container>
</div>
<div>
    <ng-container *ngFor="let item of foo">
        <span *ngIf="item.section=='bottom'">
          {{ item.name }}
        </span>
    </ng-container>
</div>

if you have more than just two valid values for section property you can use this code to have more dynamic template according to your section values: (considering you have your section values in array property called bar)

<ng-container *ngFor="let section of bar">
    <div>
        <ng-container *ngFor="let item of foo">
            <span *ngIf="item.section==section">
              {{ item.name }}
            </span>
        </ng-container>
    </div>
</ng-container>

working example: stackblitz example

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove n items from an array with object based on property name

From Dev

Find items from List based on matching property

From Java

How to sum values in typescript array based on array items property?

From Dev

Remove items from based of substring in array jquery

From Dev

Filter items from an array based on input with wildcard

From Dev

Populating array with items from different HTML attributes

From Dev

Remove items from an HTML collection based on another HTML collection

From Dev

How to set the number of items in an array based on html form input?

From Java

Remove object from array based on array of some property of that object

From Dev

Keep items in div locked in place on mobile resize

From Dev

Remove multiple items from array based on matched value

From Dev

jQuery - cut html string to place items in arrays

From Dev

Count items in array by property value

From Dev

JavaScript Remove Object From Array Based on Child Property

From Java

Select a property from an array of objects based on a value : Javascript

From Dev

Use Underscore.js to remove object from array based on property

From Dev

Getting elements from array based on property values (AngularJS)

From Dev

Lodash remove to remove an object from the array based on an id property

From Dev

Use Underscore.js to remove object from array based on property

From Dev

MongoDB: Select element from array based on another property in the document

From Dev

Removing duplicates and filtering based on a boolean property from array of objects in JavaScript

From Dev

javascript remove all objects from array based on property value

From Dev

HTML - unable to place a div at the bottom of another div

From Dev

HTML - unable to place a div at the bottom of another div

From Dev

How to fetch a particular div from one html file and place it in other html file?

From Dev

Place items from right to left in gridLayout

From Dev

Delete JSON items based on array of allowed items

From Dev

Nesting Array Items Based on a Value

From Dev

Merge items of list based on common property

Related Related

  1. 1

    Remove n items from an array with object based on property name

  2. 2

    Find items from List based on matching property

  3. 3

    How to sum values in typescript array based on array items property?

  4. 4

    Remove items from based of substring in array jquery

  5. 5

    Filter items from an array based on input with wildcard

  6. 6

    Populating array with items from different HTML attributes

  7. 7

    Remove items from an HTML collection based on another HTML collection

  8. 8

    How to set the number of items in an array based on html form input?

  9. 9

    Remove object from array based on array of some property of that object

  10. 10

    Keep items in div locked in place on mobile resize

  11. 11

    Remove multiple items from array based on matched value

  12. 12

    jQuery - cut html string to place items in arrays

  13. 13

    Count items in array by property value

  14. 14

    JavaScript Remove Object From Array Based on Child Property

  15. 15

    Select a property from an array of objects based on a value : Javascript

  16. 16

    Use Underscore.js to remove object from array based on property

  17. 17

    Getting elements from array based on property values (AngularJS)

  18. 18

    Lodash remove to remove an object from the array based on an id property

  19. 19

    Use Underscore.js to remove object from array based on property

  20. 20

    MongoDB: Select element from array based on another property in the document

  21. 21

    Removing duplicates and filtering based on a boolean property from array of objects in JavaScript

  22. 22

    javascript remove all objects from array based on property value

  23. 23

    HTML - unable to place a div at the bottom of another div

  24. 24

    HTML - unable to place a div at the bottom of another div

  25. 25

    How to fetch a particular div from one html file and place it in other html file?

  26. 26

    Place items from right to left in gridLayout

  27. 27

    Delete JSON items based on array of allowed items

  28. 28

    Nesting Array Items Based on a Value

  29. 29

    Merge items of list based on common property

HotTag

Archive