【发布时间】: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