【问题标题】:How to iterate over an array of objects in response data of a POST API in angular?如何以角度迭代POST API的响应数据中的对象数组?
【发布时间】:2022-01-20 23:08:34
【问题描述】:

假设我有一个角度形式的 POST 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', …}

这是一个数组,它有 1000 个条目,所以如果我想遍历这个响应主体并且我想匹配一个特定的 customerId,以便我可以将 customerId 保存在本地存储中,然后获取特定的详细信息排 。我怎样才能在角度做到这一点??

编辑!! **这是我的代码你能帮帮我吗APP是我的API中需要哪种类型数据的接口**


public getSeacrhCustomer(
 aadharId: string,
 emails:  string ,
 pan: string,
 phoneNumber: string
 ){
const postData : App = {
aadharId: aadharId,
emails: emails,
pan: pan,
phoneNumber: phoneNumber,
address: '',
city: '',
country: '',
customerId: '',
dateOfBirth: '',
designation: '',
firstName: '',
jobProfile: '',
lastName: '',
middleName: '',
monthlySalary: '',
passbooks: '',
pinCode: '',
state: '',
title: ''
}
{
return this.http.post('http://localhost:9900/api/v1/customer/search',postData
)
.subscribe(responseData=>
{
const specificCustomer= responseData.find((el: { phoneNumber: string | null; }) => el.phoneNumber=== this.phoneId);

  if (specificCustomer) {
    var specificCustomerId =   localStorage.set('customer', JSON.stringify(specificCustomer.customerId));
  }
console.log("Search Customer called from Existing Component!!")
console.log(responseData);
console.log(localStorage.getItem("values"))
console.log(this.phoneId)

});  
}

}

这里 phoneNumber 来自 API 响应,我想检查它是否等于 phoneId 如果它相等,那么我会将与该电话号码连接的相应 customerId 保存在本地存储中

这里 .find() 方法出错“对象”类型上不存在“查找”属性。

【问题讨论】:

标签: javascript angular angularjs angular-httpclient angular-http


【解决方案1】:

这是 Angular 中非常基本的东西。

要在 API 响应中查找特定 ID,只需将响应保存在变量中(如果以后使用),或者在 API 响应中创建一个局部变量,然后您可以提取包含该 ID 的对象。

这是一个示例代码:

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));
      }
   }
);

当你从 localStorage 中获取时,你可以简单地调用 JSON.parse 来获取对象。

请记住,响应对象可能不是数组,因此您必须将上述代码添加到包含属性的数组中。

【讨论】:

  • 试过这段代码,但它说(属性'find'在类型'Object'上不存在。).subscribe(responseData=> { const specificCustomer= responseData.find(el => el. phoneNumber=== this.phoneId); if (specificCustomer) { var specificCustomerId = localStorage.set('customer', JSON.stringify(specificCustomer.customerId)); } console.log(responseData); console.log("电话 ID !!" + this.phoneId); });这是我想通过phoneId检查客户的代码,然后我将保存与我的phoneId匹配的customerId
  • response 是一个对象,因此您必须检查对象中的哪个元素是您要迭代的数组。 find() 应该可以解决这个问题
猜你喜欢
  • 1970-01-01
  • 2020-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-25
  • 1970-01-01
  • 2016-01-25
  • 1970-01-01
相关资源
最近更新 更多