javascript - Uncaught (in promise) TypeError: .filter is not a function 。试图从 json 文件中获取数据
问题描述
我想从以下 json 文件中筛选出诺贝尔奖获得者的姓名:
{
"prizes":[
{"year":"2018","category":"physics","overallMotivation":"\u201cfor groundbreaking inventions in the field of laser physics\u201d","laureates":[{"id":"960","firstname":"Arthur","surname":"Ashkin","motivation":"\"for the optical tweezers and their application to biological systems\"","share":"2"},{"id":"961","firstname":"G\u00e9rard","surname":"Mourou","motivation":"\"for their method of generating high-intensity, ultra-short optical pulses\"","share":"4"},{"id":"962","firstname":"Donna","surname":"Strickland","motivation":"\"for their method of generating high-intensity, ultra-short optical pulses\"","share":"4"}]},{"year":"2018","category":"chemistry","laureates":[{"id":"963","firstname":"Frances H.","surname":"Arnold","motivation":"\"for the directed evolution of enzymes\"","share":"2"},{"id":"964","firstname":"George P.","surname":"Smith","motivation":"\"for the phage display of peptides and antibodies\"","share":"4"},{"id":"965","firstname":"Sir Gregory P.","surname":"Winter","motivation":"\"for the phage display of peptides and antibodies\"","share":"4"}]},{"year":"2018","category":"medicine","laureates":[{"id":"958","firstname":"James P.","surname":"Allison","motivation":"\"for their discovery of cancer therapy by inhibition of negative immune regulation\"","share":"2"},{"id":"959","firstname":"Tasuku","surname":"Honjo","motivation":"\"for their discovery of cancer therapy by inhibition of negative immune regulation\"","share":"2"}]},{"year":"2018","category":"peace","laureates":[{"id":"966","firstname":"Denis","surname":"Mukwege","motivation":"\"for their efforts to end the use of sexual violence as a weapon of war and armed conflict\"","share":"2"},{"id":"967","firstname":"Nadia","surname":"Murad","motivation":"\"for their efforts to end the use of sexual violence as a weapon of war and armed conflict\"","share":"2"}]},{"year":"2018","category":"economics","laureates":[{"id":"968","firstname":"William D.","surname":"Nordhaus","motivation":"\"for integrating climate change into long-run macroeconomic analysis\"","share":"2"},{"id":"969","firstname":"Paul M.","surname":"Romer","motivation":"\"for integrating technological innovations into long-run macroeconomic analysis\"","share":"2"}]},{"year":"2017","category":"physics","laureates":[{"id":"941","firstname":"Rainer","surname":"Weiss","motivation":"\"for decisive contributions to the LIGO detector and the observation of gravitational waves\"","share":"2"},{"id":"942","firstname":"Barry C.","surname":"Barish","motivation":"\"for decisive contributions to the LIGO detector and the observation of gravitational waves\"","share":"4"},{"id":"943","firstname":"Kip S.","surname":"Thorne","motivation":"\"for decisive contributions to the LIGO detector and the observation of gravitational waves\"","share":"4"}]},{"year":"2017","category":"chemistry","laureates":[{"id":"944","firstname":"Jacques","surname":"Dubochet","motivation":"\"for developing cryo-electron microscopy for the high-resolution structure determination of biomolecules in solution\"","share":"3"},{"id":"945","firstname":"Joachim","surname":"Frank","motivation":"\"for developing cryo-electron microscopy for the high-resolution structure determination of biomolecules in solution\"","share":"3"}]}
这是我的 Javascript 文件:
const search = document.getElementById('search');
const matchlist = document.getElementById('match-List');
//search names.json and filter
const searchName = async searchText => {
const res = await fetch('../data/prize.json');
const names = await res.json();
//get matches to current text input
const matching = names.filter(data => {
const regex = new RegExp(`^${searchText}`,'gi');
return data.firstname.match(regex);
});
console.log(matching);
};
search.addEventLi`enter code here`stener('keyup', () =>
searchName(search.value));
运行代码后,它说 names.filter 不是函数。我哪里做错了?
解决方案
您filter
只能在数组上使用。您的数据不是数组。
推荐阅读
- python - Python:我如何确定投掷时使球运动最远的角度?
- express - Loopback 4 使用带有环回的现有快速服务器
- php - 如何在php中加密和解密字符串?
- python - Python 如果我调用我的函数,我应该如何解决这个问题,它返回函数对象
- flutter - Flutter google_pay 实现
- python - matplotlib.pyplot show() 不清除图形
- javascript - Vue 与 Muuri - 如何使用?
- asyncfileupload - Blazor 文件上传 IFormFile
- sql - SQL Server 过去 12 个月的 begin_date 和 end_date
- reactjs - React BrowserRouter 不适用于嵌套路径