使用 Javascript 搜索 JSON
[
{"lastName":"Noyce","gender":"Male","patientID":19389,"firstName":"Scott","age":"53Y,"},
{"lastName":"noyce724","gender":"Male","patientID":24607,"firstName":"rita","age":"0Y,"}
]
上面是我的 JSON 数据,
var searchBarInput = TextInput.value;
for (i in recentPatientsList.length) {
alert(recentPatientsList[i].lastName
}
我收到了此警报。现在我有一个 TextInput,在输入时应该搜索 Json 并给出结果。我正在搜索姓氏值。
我如何获取 JSON 中的值并进行搜索。
[
{"lastName":"Noyce","gender":"Male","patientID":19389,"firstName":"Scott","age":"53Y,"},
{"lastName":"noyce724","gender":"Male","patientID":24607,"firstName":"rita","age":"0Y,"}
]
Above is my JSON Data
var searchBarInput = TextInput.value;
for (i in recentPatientsList.length) {
alert(recentPatientsList[i].lastName
}
I am getting the alert for this. Now i have a TextInput which on typing should search the Json and yield me the result. I am searching for lastname value.
How would i take the value and search in my JSON.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是:
不正确的。迭代数组时应该做的是:
“for ... in”机制并不是真正用于迭代数组的索引属性。
现在,为了进行比较,您只需查找文本输入中的名称是否等于列表条目的“lastName”字段:
This:
is incorrect. What you should do to iterate over an array is:
The "for ... in" mechanism isn't really for iterating over the indexed properties of an array.
Now, to make a comparison, you'd just look for the name in the text input to be equal to the "lastName" field of a list entry:
您不应该使用
for..in
来迭代数组。相反,请使用普通的旧 for 循环。要获取与姓氏匹配的对象,请过滤数组。You should not use
for..in
to iterate over an array. Instead use a plain old for loop for that. To get objects matching the last name, filter the array.