John w.
John w.

Reputation: 531

Check in object array if the length of groups is greater than zero in a OF element

I have an array of objects. Inside this object I have an array of groups that determine the quantity.

There is a way to check within the object array if the groups is greater than zero where the OF element is equal to 12.2.

DEMO

what I tried

Sort = [
    {
      id: 1,
      of: 12.2,
      tam: 's',
      name: 'Color Run',
      groups: [
        {
          type: 'type1',
          quant: 21
        }
      ]
    },
    {
      id: 2,
      of: 12.2,
      tam: 'M',
      name: 'Color Run',
      groups: []
    },
    {
      id: 3,
      of: 2.2,
      tam: 'L',
      name: 'Works',
      groups: []
    }
  ];

  ngOnInit() {
    console.log(this.Sort)
    var x = this.Sort.forEach(element => {
      if(element.of == 12.2){
        if(element.groups.length > 0){
          return true;
        }
        else{
          return false;
        }
      }
      
    });
    console.log(x)
  }

Upvotes: 0

Views: 1181

Answers (4)

Quyết
Quyết

Reputation: 602

Simply using the filter protoype will do the job:

let Sort = [
    {
      id: 1,
      of: 12.2,
      tam: 's',
      name: 'Color Run',
      groups: [
        {
          type: 'type1',
          quant: 21
        }
      ]
    },
    {
      id: 2,
      of: 12.2,
      tam: 'M',
      name: 'Color Run',
      groups: []
    },
    {
      id: 3,
      of: 2.2,
      tam: 'L',
      name: 'Works',
      groups: []
    }
  ];

let res = Sort.filter(e => e.of == 12.2 && e.groups.length > 0);

console.log(res)

Upvotes: 0

Bharat
Bharat

Reputation: 1205

I am just posting without angular part to run and see the output

Sort = [
    {
      id: 1,
      of: 12.2,
      tam: 's',
      name: 'Color Run',
      groups: [
        {
          type: 'type1',
          quant: 21
        }
      ]
    },
    {
      id: 2,
      of: 12.2,
      tam: 'M',
      name: 'Color Run',
      groups: []
    },
    {
      id: 3,
      of: 2.2,
      tam: 'L',
      name: 'Works',
      groups: []
    }
  ];


  console.log(this.Sort)
  var x = this.Sort.filter(element => element.of === 12.2 && element.groups.length >0);
  console.log("Filtered Output")
  console.log(x)
  

Upvotes: 0

Yash Rami
Yash Rami

Reputation: 2327

you can try like this

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  Sort = [
    {
      id: 1,
      of: 12.2,
      tam: 's',
      name: 'Color Run',
      groups: [
        {
          type: 'type1',
          quant: 21
        }
      ]
    },
    {
      id: 2,
      of: 12.2,
      tam: 'M',
      name: 'Color Run',
      groups: []
    },
    {
      id: 3,
      of: 2.2,
      tam: 'L',
      name: 'Works',
      groups: []
    }
  ];

  ngOnInit() {
    console.log(this.Sort);
    let groupLengthList = [];
    for (const data of this.Sort) {
      if (data.of === 12.2) {
        if (data.groups.length > 0) {
          groupLengthList.push(true);
        } else {
          groupLengthList.push(false);
        }
      }
    }
    console.log(groupLengthList);
  }
}

Upvotes: 0

TotallyNewb
TotallyNewb

Reputation: 4800

forEach ignores the return value and executes for all the elements. So it would be both easier and more performant to use some() (docs here) instead, e.g.

const exists = this.Sort.some(s => s.of === 12.2 && s.groups.length > 0);

If you want to use forEach anyway, you could place your boolean value outside of the loop with initial value of false, and then set it's value to true only if the element exists, e.g.

let exists = false;
this.Sort.forEach(e => {
  if (e.of === 12.2 && e.groups.length > 0) {
    exists = true;
  }
});

Upvotes: 1

Related Questions