我正在从API获取数组,像这样:
Array(4)
0:{email: "mail@mail.com", name: "Billie", lastName: "Jean",
id: "5b6f79"}
1:{email: "mail@mail.com", name: "John", lastName: "Doe",
id: "8b6z75"}
...
我想要的是提取id
以便在我的课程中使用它来构建类似${this.sellerData}/clients/ID
的URL,其中ID是数组中的ID。
我已经尝试过了:
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;
}
但是我只得到一个带有值的对象:
{email: "mail@mail.com", name: "Billy", lastName: "Jean", id: "5b6f79"}
...
谢谢您的帮助!
答案 0 :(得分:0)
在迭代数组clientid时将是{email: "mail@mail.com", name: "Billy", lastName: "Jean", id: "5b6f79"}
,因为您只希望id可以通过clientid.id来获取。
for (let clientid of this.sellerClients ) {
clientid = clientid.id;
}
答案 1 :(得分:0)
我不确定,但是我认为这是您要尝试的事情
const result = data.clients.map(el => {return {...el, id : this.clientIdNumber}});
此代码从数组中获取数据,并将该数组中的所有ID替换为clientIdNumber。
如果我不明白这个问题,那么我可以纠正代码。
答案 2 :(得分:0)
那看起来像一个对象数组。如果您将其分配给变量,请说“客户”:
clients =
[
{email: "mail@mail.com", name: "Billie", lastName: "Jean", id: "5b6f79"},
{email: "mail@mail.com", name: "John", lastName: "Doe", id: "8b6z75"}
];
要获取客户端ID,您必须首先遍历每个对象:
for(var i = 0, len = clients.length; i < len; i++)
{
console.log(clients[i].id); //Would give you the id of each client
}
上面的循环将打印:
"5b6f79"
"8b6z75"
答案 3 :(得分:-1)
您需要访问客户端对象的ID
for (let client of this.sellerClients ) {
console.log(client );
clientid = client.Id;
}