-1

I'm getting an array from an API, like this:

Array(4)
 0:{email: "mail@mail.com", name: "Billie", lastName: "Jean", 
id: "5b6f79"}
 1:{email: "mail@mail.com", name: "John", lastName: "Doe", 
id: "8b6z75"}
...

What I want is to extract the id to use it in my class to construct an URL like ${this.sellerData}/clients/ID where the ID is the one from the array.

I have tried this:

this.sellerClients = data.clients;
  console.log (this.sellerClients); 
  //prints the array in console
  for (let clientid of this.sellerClients ) {
     console.log(clientid);
         clientid = this.clientIdNumber;    
  }  

But I only get an object with the values:

{email: "mail@mail.com", name: "Billy", lastName: "Jean", id: "5b6f79"}
... 

Thank you for your help!

roymckrank
  • 689
  • 3
  • 12
  • 27
  • Possible duplicate of [From an array of objects, extract value of a property as array](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) – Heretic Monkey Aug 12 '18 at 05:53

4 Answers4

0

while iterating the array clientid will be {email: "mail@mail.com", name: "Billy", lastName: "Jean", id: "5b6f79"} as you want only id you can get it by clientid.id

for (let clientid of this.sellerClients ) { clientid = clientid.id; }

ravi
  • 1,127
  • 9
  • 10
0

I'm not sure but i think that this is what that you are tring to do

const result = data.clients.map(el => {return {...el, id : this.clientIdNumber}});

this code gets the data from the array and replace all of the id in that array to the clientIdNumber.

if i didn't understand the question currect me so i coul'd correct the code..

Lagistos
  • 3,539
  • 1
  • 10
  • 18
0

That looks like an object array. If you assign it to a variable say 'clients':

 clients =
 [
   {email: "mail@mail.com", name: "Billie", lastName: "Jean", id: "5b6f79"},
   {email: "mail@mail.com", name: "John",   lastName: "Doe",  id: "8b6z75"}
 ];

To get to the client id, you'd have to first iterate through each object:

for(var i = 0, len = clients.length; i < len; i++) 
{
    console.log(clients[i].id); //Would give you the id of each client
}

The above loop would print:

"5b6f79"
"8b6z75"
Ashish Koshy
  • 131
  • 1
  • 7
-1

You need to access id of client Object

for (let client of this.sellerClients ) {
     console.log(client );
     clientid = client.Id;    
} 
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396