1

I showed data using below angular functions

availableLockers = [
    {
      "lockerCode": "L01",
      "allocStatus": "alloc"
    },
    {
      "lockerCode": "L02",
      "allocStatus": "un-alloc"
    },
    {
      "lockerCode": "L03",
      "allocStatus": "un-alloc"
    },
    {
      "lockerCode": "L04",
      "allocStatus": "temp-alloc"
    }, {
      "lockerCode": "L05",
      "allocStatus": "alloc"
},]

I am using bellow html

<div *ngFor="let locker of availableLockers let i=index;">{{locker.lockerCode}} </div>

The above code is working well. currently I need do display count of each status. Ex: how many lockers with "alloc" status, how many lockers with "temp-alloc" status.

4
  • What do you mean by count ? Please clarify Commented Jun 4, 2021 at 4:47
  • how many lockers with "alloc" status, how many lockers with "temp-alloc" status Commented Jun 4, 2021 at 4:49
  • Bindings cannot contain assignments at column 31 in [ {{ availableLockers.filter(obj => allocStatus === "un-alloc").length Commented Jun 4, 2021 at 5:04
  • first thing that pops up in my mind is to loop the array and count statuses Commented Jun 4, 2021 at 5:13

3 Answers 3

2

You can try with filter(),

let availableLockers = [
  {
    lockerCode: 'L01',
    allocStatus: 'alloc'
  },
  {
    lockerCode: 'L02',
    allocStatus: 'un-alloc'
  },
  {
    lockerCode: 'L03',
    allocStatus: 'un-alloc'
  },
  {
    lockerCode: 'L04',
    allocStatus: 'temp-alloc'
  },
  {
    lockerCode: 'L05',
    allocStatus: 'alloc'
  }
];

function checkStatus(status) {
  let data = availableLockers.filter(locker => locker.allocStatus === status);
  return data.length;
}

console.log(checkStatus('alloc'));
console.log(checkStatus('un-alloc'));
console.log(checkStatus('temp-alloc'));
Sign up to request clarification or add additional context in comments.

Comments

2

It's better to create a dictionary for the count of each allocStatus using reduce

and then use it where ever you want with O(1) time complexity

availableLockers = [
  {
    lockerCode: "L01",
    allocStatus: "alloc",
  },
  {
    lockerCode: "L02",
    allocStatus: "un-alloc",
  },
  {
    lockerCode: "L03",
    allocStatus: "un-alloc",
  },
  {
    lockerCode: "L04",
    allocStatus: "temp-alloc",
  },
  {
    lockerCode: "L05",
    allocStatus: "alloc",
  },
];

const dict = availableLockers.reduce((acc, { allocStatus }) => {
  if (acc[allocStatus]) ++acc[allocStatus];
  else acc[allocStatus] = 1;
  return acc;
}, {});

console.log(dict["alloc"]);
console.log(dict["un-alloc"]);
console.log(dict["temp-alloc"]);

Comments

0

In Html File

<div *ngFor="let locker of availableLockers; let i=index;">
 {{locker.lockerCode}}
</div>
<div>{{alloc}}</div>
<div>{{unAlloc}}</div>
<div>{{tempAlloc}}</div>

In TS file

    alloc:number = 0;
    unAlloc:number = 0;
    tempAlloc:number = 0;
    
    ngOnInit(){
     availableLockers.forEach((value)=>{
     if (value['allocStatus']=='alloc'){
        alloc++}
     elseif (value['allocStatus']=='un-alloc'){
            unAlloc++}
     elseif (value['allocStatus']=='temp-alloc'){
            tempAlloc++}

     })
     }

1 Comment

My earlier code was completely wrong sorry for that, edited it and this should work now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.