Skip to content Skip to sidebar Skip to footer

How To Count Json Data Item Ionic2

I find a new problem, I would like to count @NAME to set array FACET[] for show @KEY Myjson ' RESULT':{ 'FACET':[ { '@NAME' : 'creator',

Solution 1:

Ok. there's just a tip that I would like to point out to you. You shoud get a bit better with words. Not the English language in particular but more about explaining your current result and your desired result. I believe this will force you into learning english at a much faster pace than you're currently doing. (coming from a non-native speaker)

"I would like to count@NAME to set array FACET[] for show @KEY"

So, with the returned JSON we can do a couple of things, which do not get quite clear from immediately looking at your question (did a +1 since I don't believe it's of bad quality or isn't a good question).

So there are 3 things I should cover in my post

  • Counting the amount that NAME occurs
  • Setting an array for the FACET
  • Showing the KEY per FACET which seems to be the end goal.

Ok

Counting the amount that NAME occurs

So you basically already got this answer. You submitted this code

this.http.get("my_url") .subscribe(data =>{ this.date=data.json().RESULT.FACET; //get @NAME });

Which means you already have access to the FACET array. Counting the amount that NAME occurs can be done either by creating a for loop and checking if the name is not undefined (thus incrementing an int) or counting on the fact that NAME is always defined, thus just calling this.date.size() (if this.date is set correctly of course)

get an array for the FACET

this, you're already doing by assiging the this.date = data.json().RESULT.FACET. Your this.date now contains an array holding your FACET objects. Just see it like this: JSON (with comments which is not possible in JSON but just for demonstration purposes) =

{ FACET: // declare the main object, called FACET 
  [     // declaring the FACET as an Array ([] is for creating a list)
    {id: 2} //initializing a child of the FACET array
    ,{id: 3} //initializing the second FACET child.
  ]
} 

So, this pseudo (not realistic) JSON holds 2 objects, having id = 2 and id = 3

Ok, now you should understand a bit of JSON, let's take a look at how your code looks (taking a Businnes with multiple offices and multiple employees per office into account)

{
  OFFICES" : [
       {
         "id" : "1",
         "location" : "New York",
         "employees" : [
           {"id" : "1", "fullName" : "Jon Skeet"},
           {"id" : "2", "fullName" : "Random Stranger"}
         ]
     },
     {
        "id" : "2",
        "location" : "Amsterdam",
        "employees" : [
            {"id" : "1", "fullName" : "Ivaro18"},
            {"id" : "2", "fullName" : "Some-Random Stranger"}
         ]
      }
  ]
}

This code is basically the same as your code. And the question your asking now is, taking the code from my answer, how to get all the id's or names of all the employees. (get all names of keys of facet objects)

Now let's show you in typescript

someFunction() {
   this.http.get('someUrl').map(response => {
      console.log(JSON.stringify(response.json())); // check what you actually retrievelet decoded = response.json();
      for(let office of decoded.OFFICE) {
         console.log(office.location);

         for(let employee of office.employees) {
            console.log(employee.fullName);
         } 
      }
   });
}

Expected output of this program

> New York> Jon Skeet> Random Stranger> Amsterdam> Ivaro18> Some-Random Stranger

I think this pseudo code will get you to think a bit out-of-the-box and find the answer to your question. If you can't elaborate more on what you want as output I will be glad to help you at any time (even weekends) just comment and I'll respond when I have the time!

Solution 2:

@Ivaro18 thanks for your answers. I want to counting the amount that @NAME for setting an array for the FACET and showing the KEY per FACET[] which seems to be the end goal.

Yes, you totally know what I meant. But i want output is every @KEY of that FACET[]

Example :

<ion-select [(ngModel)]="refine" (ionChange)="optionsFn();"><ion-optionvalue="..." *ngFor="let item of date">{{item["@NAME"]}},({{item["@COUNT"]}})</ion-option></ion-select><ion-list><ion-item *ngFor="let item of foundRepos" (click)="itemClicked($event,item)"><h3> {{ item[@KEY"] }}</h3></ion-item></ion-list>

.

optionsFn(): void {
            this.http.get("my_url")
                        .subscribe(data =>{
                                 this.date=data.json().RESULT.FACET; //get @NAME
                        },error=>{
err =>console.error(err),
                           () =>console.log('getRepos completed')
            );
        console.log(this.date);

            console.log(this.date);
            this.goToapply()
      }

      goToapply(){

       this.http.get("my_url")
                        .subscribe(data =>{
                                 this.foundRepos=data.json().RESULT.FACET[0,1,2,3.4......].FACET_VALUES; ///get @KEY
                        },error=>{
                            console.log(error);
                        } );

      }

from above ion-select is show creator , lang and bnb ,I want to If When I select langion-option value="..." to keep number of "@NAME"

  • example counting amount of @NAME is 3 and
  • when I select creator is ion-option value="..." << is 0
  • when I select lang is ion-option value="..." << is 1
  • when I select bnb is ion-option value="..." << is 2

and If i get value ,I take it to goToapply() for set FACET[]

  • example I select bnb I get value =2 and console.log this.refine is show 2
  • take it(2) to let p = this.refine , so that p = 2
  • take p to this.foundRepos=data.json().RESULT.FACET[p].FACET_VALUES; for show @KEY in ion-list

When I select lang output is

> tha> eng

Post a Comment for "How To Count Json Data Item Ionic2"