0

I have one third Party API which returns data as below - It has been called from Angular Service using HttpClient.

const someObject = {

    employees:[   

        {name:"XYZ",age:30},
        {name:"ABC",age:28},
    ]
}

I want to transform this data into below structure where name, age are as it is and description will be calculated based on name and age

{ 
  name : 
  age  : 
  description: //calculate based on name and age 
},
{ 
  name : 
  age  : 
  description:  //calculate based on name and age 
}

Above structure will be typed to some Interface and this is ultimately returned from my Angular service

How can I do this using RxJS ?

Can anybody please help me as I am struggling with it

Ultimately , my Angular service will return data as above structure ?

EDIT :

Based on responses got , I tried below :

return this.http.get<any>(url).pipe(
            map(data => data.employees),
            map(employees =>{
              values.map(employee =>{
                console.log('individual value =',employee);
                return{
                  employee,
                  description:employee.name + "" + employee.age 
                }
              })          
            })
        )  

But when this Observable is subscribed , I am getting value as undefined.

Is it because the second map operator map(employees is not returning anything ? How can I make it return object constructed from inner map ?

1
  • Have you tried using pipe and then map operator to create new object containing description along with other fields? Commented Nov 20, 2019 at 10:49

3 Answers 3

1

so basically you want to add and process additional property:

assume this is the method in your service:

getData(){...}

Then you can simply do that:

const employees$ = this.service.getData().pipe(
  map(data => data.employees),
  map(employees => employees.map(employee => {
       const description = `My name is ${employee.name}`;
       return {...employee, description};
    }
  )
);

then in employees$ you have the observable with employees with the structure you want

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

4 Comments

seems it will {...employee, description}; not work
In addition there is one more problem . I have edited the question . Can you take a look ?
yea - there should be return added:
return values.map(employee =>{
1

    const someObject = {
    
        employees:[   
            {name:"XYZ",age:30},
            {name:"ABC",age:28},
        ]
    }
    
    
    let description = 'description goes here';
    let result = someObject.employees.map( o => { return  {...o, ...{description} } });
    console.log(result);

Comments

0

You can try something like this

of([
        {name:"XYZ",age:30},
        {name:"ABC",age:28},
    ]).pipe(
    map(employees => 
      employees.map(
        elem => {return {
          "name": elem.name,
          "age": elem.age,
          "description": 'blablabla'
        }}
      )
    )
  ).subscribe(employees => console.log(employees))

Now just change the string in the description. Hope I helped

Comments

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.