简体   繁体   English

如何在 angular 中获取 API 的响应数据中迭代对象数组?

[英]How to iterate over an array of objects in response data of a get API in angular?

Suppose I have a response body of an GET API in angular and its an array of objects it goes like this:假设我在 angular 中有一个 GET API 的响应主体,它的对象数组如下所示:

79: {customerId: '61c1b85fa7b16079fe7b5b64', title: 'Mr', phoneNumber: Array(0), jobProfile: 'GOLD', designation: 'Uuuuuuuuuuuu', …}
80: {customerId: '61c1b904a7b16079fe7b5b65', title: 'Mr', phoneNumber: Array(0), jobProfile: 'DIAMOND', designation: 'Player', …}
81: {customerId: '61c1b94aa7b16079fe7b5b66', title: 'Mr', phoneNumber: Array(0), jobProfile: 'DIAMOND', designation: 'Boss', …}
82: {customerId: '61c1b987a7b16079fe7b5b67', title: 'Mr', phoneNumber: Array(0), jobProfile: 'DIAMOND', designation: 'Vvvjkb ', …}
83: {customerId: '61c1bb4ca7b16079fe7b5b68', title: 'Mr', phoneNumber: Array(0), jobProfile: 'DIAMOND', designation: 'Player', …}
84: {customerId: '61c1bbd3a7b16079fe7b5b69', title: 'Mrs', phoneNumber: Array(0), jobProfile: 'GOLD', designation: 'Player', …}
85: {customerId: '61c1bd0ca7b16079fe7b5b6a', title: 'Mr', phoneNumber: Array(0), jobProfile: 'GOLD', designation: 'Player', …}
86: {customerId: '61c1bd4ba7b16079fe7b5b6b', title: 'Mr', phoneNumber: Array(0), jobProfile: 'DIAMOND', designation: 'CEO', …}
87: {customerId: '61c1bf34a7b16079fe7b5b6c', title: 'Mr', phoneNumber: Array(0), jobProfile: 'GOLD', designation: 'HELLO', …}
88: {customerId: '61c1bf79a7b16079fe7b5b6d', title: 'Mrs', phoneNumber: Array(0), jobProfile: 'DIAMOND', designation: 'Vvvvvvvvvv', …}
89: {customerId: '61c1c2b2a7b16079fe7b5b6e', title: 'Mr', phoneNumber: Array(0), jobProfile: 'DIAMOND', designation: 'Hiiii', …}
90: {customerId: '61c1c8c7a7b16079fe7b5b6f', title: 'Mr', phoneNumber: Array(0), jobProfile: 'GOLD', designation: 'CEO', …}

This is the array and it has 1000 something entries, So if I want to iterate over this response body and I want to match a particular customerId so that I can save the customerId in local storage and then get the details of a particular row.这是一个数组,它有 1000 个条目,所以如果我想遍历这个响应主体并且我想匹配一个特定的 customerId,以便我可以将 customerId 保存在本地存储中,然后获取特定行的详细信息。 How can I do that in angular??我怎么能在 angular 中做到这一点?

This is pretty basic thing in angular.这是 angular 中非常基本的事情。

To find a specific ID in the API response, simply save the response in a variable if used later, or create a local variable in the API response and then you can extract the object containing the ID.要在 API 响应中查找特定 ID,只需将响应保存在变量中(如果以后使用),或在 API 响应中创建局部变量,然后您可以提取包含 ID 的 object。

Here's a sample piece of code for this:这是一个示例代码:

this.httpService.get('API_CAll_ROUTE_').subscribe(
   response => {
      const specificCustomer= response.find(el => el.customerId=== '61c1b85fa7b16079fe7b5b64');
      if (specificCustomer) {
         localStorage.set('customer', JSON.stringify(specificCustomer.customerId));
      }
   }
);

When you get it from localStorage, you can simply call JSON.parse to get the object.当你从 localStorage 获取它时,你可以简单地调用 JSON.parse 来获取 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM