I am working on a project where in my component.ts file I am calling a post API which is giving me a response in the format
email: string,
employeeAddress: string,
employeeId: string,
employeeLevel: string
I am not sure whether my approach is correct or not but here is my API calling code
return this.http.post<{[employeeId : string] : Create}>('http://localhost:9900/api/v1/employee',postData).
pipe(map(response => {
var res = JSON.parse(JSON.stringify(response))
const getCreateempIdArray : any = [] ;
console.log(res)
for(const employeeId in res){
if(res.hasOwnProperty(employeeId)){
getCreateempIdArray.push({ ...res[employeeId]});
}
if(res.hasOwnProperty(email)){
getCreateempIdArray .push({...res[email]});
}
}
console.log("Employee Id Array from Create employee Service " + getCreateempIdArray)
return getCreateempIdArray;
})).
subscribe((responseData)=>
{
console.log(response)
})
Now the thing I want is that when I get my response then I want to push my employeeId and email corresponding to that employeeId in an array so that I can save the array in local storage and I can get the particular employee email through employeeId by the help of this array.
But when I do console.log(getCreateempIdArray) it gives me [object Object]
I just want to push the employeeId and its corresponding emailId which I am getting from response body into an array
It is shown in the form [object Object] because you are concatenating a string with an object (using ' ' +) and automatically the object is converted to a string.
try this way:
Instead of using a (+), use a comma (,) to separate the string and the object.