Home > Software design >  I want to get specific data from array
I want to get specific data from array

Time:04-26

I want to get specific data for chart from existing array

I have array like this:

ageGroup: 1
countryId: 5992
countryName: "France"
gender: 1
id: "FR"
monthNo: 7
nights: 415055
tourType: 1
value: 53698
yearNo: 2021

but for chart I want data from above array, which only contains id, countryName, value and year. like this:

id: "FR",
countryName: "France",
value: 26124560,
year: 2014

I was searching a lot, but I could not find any solution. can u help me?

I have array, which is created from three API call

forkJoin({
  countriesList: this.getQueryCountriesList(),
  nights: this.getQueryNights(),
  codes: this.getCodes()
}) 
.subscribe(({countriesList, nights, codes}) => {
  this.resulti = countriesList.map((obj:any) => ({
       ...obj,
       ...nights.find((o:any) => o.countryId === obj.countryId),
       ...codes.find((o:any) => o.countryId === obj.countryId)
  }));
  return this.resulti
});

And it returns 200 objects in array

CodePudding user response:

You can use the map operator from RxJS. For example, in subscribe, I want to create a new object from 'subsrcibed'...

return this.dataService.getCustomersDetailsObject(customerId)
    .pipe(
      map(customer => {
        //object that you want
        let customerResolved: ICustomerResolved = {
          customer: customer
        };
        return customerResolved;
      }),
      catchError(error => {
        const message: any = `Retrieval error: ${error}`;
        console.error(message);
        return of<ICustomerResolved>({ error: message });
      })
    );

CodePudding user response:

Just create another short array from existing array. Use the following function

function createShortArray(arr)
{
    let shortArray=[];

    for(let item of arr)
    {
        shortArray.push({id:item.id,countryName:item.countryName,value:item.value,year:item.yearNo})
    }

    return shortArray;
}
  • Related