0

How to get the length of an object array object in angular.

Here's the code:

list.component.ts

const obj: any = {};
this.days.forEach((x: any) => {
  const itemFilter = this.rowData.filter((item: any) => item);
  itemFilter.forEach((item: any) => {
    const date = format(item.lastpmdate, 'YYYY-MM-DD');
    if (format(x.date, 'YYYY-MM-DD') === date) {
      if (obj[date]) {
        obj[date].push(item);
      } else {
        obj[date] = [item];
      }
    }
  });
});

enter image description here

What I need is to get the length of the object array.

Thanks in advance.

2
  • 2
    Object.keys(obj).length ? Commented Nov 29, 2019 at 8:52
  • @JacopoSciampi I put the Object.keys(obj).length below the this.days.forEach((x: any) => { .... }});.. it returns 3 Commented Nov 29, 2019 at 8:56

1 Answer 1

1

You can list the keys of the target object with Object.keys and then check its length as a normal array:

Object.keys(this.days).length

If you have an object of arrays:

Object.values(this.days).map(v => v.length)

Object.values() allows to iterate over the object values instead of the keys.
Having these, you can then get the length for each of the inner arrays.

Sign up to request clarification or add additional context in comments.

3 Comments

it return 7, it should return 1, 1, 7
1 for date: 2019-11-24, 1 for date: 2019-11-25 then 7 for date: 2019-11-30
I updated the answer, and the new code returns what you need.

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.