Get value from array Angular 2

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Get value from array Angular 2



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.


id


$this.sellerData/clients/ID



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!





Possible duplicate of From an array of objects, extract value of a property as array
– Heretic Monkey
Aug 12 at 5:53




4 Answers
4



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


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



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


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



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..



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"



You need to access id of client Object


for (let client of this.sellerClients )
console.log(client );
clientid = client.Id;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

React Native Navigation and navigating to another Screen problem