ajax JavaScript 如果 phone 号码已经存在,如何查找?

ajax JavaScript how to find if phone number already exist?

我正在收到用户的意见。如果任何用户输入他的 phone 号码,那么我想知道这个 phone 号码是否已经存在于我的 json 词典中。如果 phone 号码存在,那么我想自动呈现他的名字。这是我的代码:

<input type="text" name="patient_number" class="form-control" placeholder="+44****" id="patient_number">
<!- if phone number exist then I want to automatically render his name->
<input type="text" name="patient_name" class="form-control" id="patient_name" >  


<script> 
document.getElementById('patient_number').addEventListener('change', function() {
const phone_number =  this.value;
$.ajax({
           type: 'GET',
           url: "{%url 'hospital:appoinment-json'  %}",
           number: number,
           success: function(response){
                for (key in response.data){
                var patient_name = (response.data[key].patient_name)
                var patient_phone_number = (response.data[key].phone)   
                        
                }},
           error: function(error){
               console.log(error)
           }})});
 
</script>   

这是我的 json 对象:

{"data": [{"patient_name": "Jhone", "phone": "1234", "id": 2}, {"patient_name": "Mike Hartho", "phone": "454", "id": 1}]}

获得包含患者详细信息列表的 json 数据后,解析对象数组并检查是否有任何对象包含相同的 phone 编号。如果找到对象,获取对象的 ID 并使用该 ID 访问该对象以显示用户详细信息。

让我们假设变量 userDetails 有包含用户数据的 json 并且 phoneNumber 有phone 用户输入的号码。

userDetails.map((user) => {
  if (user.phone === phoneNumber) {
    console.log(`User: ${user.id}`);
    console.log(user);
  }
});