0

I have a code like this :

var data = [
   {
     first_name: "Apurv",
     date: "2018-01-22",
     is_present: "1", 
   },
   {
     first_name: "Lucky",
     date: "2018-01-22",
     is_present: "0", 
   },
   {
     first_name: "Apurv",
     date: "2018-01-20",
     is_present: "0", 
   },
   {
     first_name: "Lucky",
     date: "2018-01-20",
     is_present: "1", 
   }
];

var groupByName = {};

data.forEach(function (a) {
    groupByName [a.first_name] = groupByName [a.first_name] || [];
    groupByName [a.first_name].push({ date: a.date, is_present: a.is_present });
});

console.log(groupByName);

I try to make local variable of groupByName variable become global in order to works with *ngIf on Angular 5, could everyone clearing this or maybe there is another way to solve this issue? I would be thank you before!

2
  • 1
    Not sure I got what you are trying to do. But I would make it a property of your component class. Commented Oct 25, 2018 at 5:28
  • I just want to make looping from groupByName variable but the variable is in local, I try to make it global Commented Oct 25, 2018 at 10:03

1 Answer 1

2

You should do it as a property of your component :

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

@Component({
  selector: 'app-mycomponent',
  template: '<some html here>',
})
export class MyComponent implements OnInit{

  public data:any[];
  public groupByName:any;


  ngOnInit() {
    this.data = [
       {
         first_name: "Apurv",
         date: "2018-01-22",
         is_present: "1", 
       },
       {
         first_name: "Lucky",
         date: "2018-01-22",
         is_present: "0", 
       },
       {
         first_name: "Apurv",
         date: "2018-01-20",
         is_present: "0", 
       },
       {
         first_name: "Lucky",
         date: "2018-01-20",
         is_present: "1", 
       }
    ];

    this.groupByName = {};

    this.data.forEach(function (a) {
        this.groupByName[a.first_name] = this.groupByName [a.first_name] || [];
        this.groupByName[a.first_name].push({ date: a.date, is_present: a.is_present });
    });
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

sorry, copy/paste error. Forgot this at "data.foreach...". - just corrected my solution
yeah, I've trying as you explained above but this.groupByName is undefined, I don't know why as before I've trying using return this.groupByName but the result is still undefined. Maybe you know the way how to groupping by JSON data by each categories, I would be thank you for your response.
I checked your routine and the result is OK: { Apurv: [ { date: '2018-01-22', is_present: '1' }, { date: '2018-01-20', is_present: '0' } ], Lucky: [ { date: '2018-01-22', is_present: '0' }, { date: '2018-01-20', is_present: '1' } ] } Must be another problem! Could you please post your exact error message ?
Thanks, unexpectedly it works after I re-run the ionic serve command, maybe livereload won't reload the browser properly. Well, I say thank you for solving this.

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.