[英]Keyword search of object returning array
Beginning coder here taking a js course.在这里开始 coder 学习 js 课程。 I'm almost done with a higher order function lesson, but am stuck.我几乎完成了高阶函数课程,但被卡住了。 I have an object containing 250 countries with different info.我有一个对象,其中包含 250 个不同信息的国家/地区。 Example:例子:
const countries = [
{
name: 'Afghanistan',
capital: 'Kabul',
languages: ['Pashto', 'Uzbek', 'Turkmen'],
population: 27657145,
flag:
'https://restcountries.eu/data/afg.svg',
currency: 'Afghan afghani'
},
{
name: 'Åland Islands',
capital: 'Mariehamn',
languages: ['Swedish'],
population: 28875,
flag:
'https://restcountries.eu/data/ala.svg',
currency: 'Euro'
}, etc.
I'm asked to write a function that searches each country's name and returns an array of only the countries meeting the keyword criteria.我被要求编写一个函数来搜索每个国家/地区的名称并返回一个仅包含符合关键字条件的国家/地区的数组。 I'm stumped and feel very lost.我很难过,感到非常失落。 Here's what I have:这是我所拥有的:
const keys = Object.keys(countries)
function categorizeCountries(keyword) {
for (let i = 0; i < keys.length; i++) {
let country = countries.name
if (country.includes(keyword)) {
console.log(countries.filter((country)
=> country.includes(keyword)))
} else {
console.log('Country not found')
}
}
return country
}
categorizeCountries('land')
scategorizeCountries('stan')
I'm sure the issue is in my conditional statement, but I don't know how else to go about this.我确定问题出在我的条件语句中,但我不知道该怎么做。 Any help is greatly appreciated.任何帮助是极大的赞赏。
This is a short snippet demonstrating how to do it with .filter()
and .includes()
(@IvanaMurray made a good point regarding the application of .toLowerCase()
for the strings to be compared, which I have left out here for simplicity):这是一个简短的片段,演示了如何使用.filter()
和.includes()
(@IvanaMurray 提出了一个很好的观点,即.toLowerCase()
用于要比较的字符串,为了简单起见,我在这里省略了):
const countries = [ { name: 'Afghanistan', capital: 'Kabul', languages: ['Pashto', 'Uzbek', 'Turkmen'], population: 27657145, flag: 'https://restcountries.eu/data/afg.svg', currency: 'Afghan afghani' }, { name: 'Åland Islands', capital: 'Mariehamn', languages: ['Swedish'], population: 28875, flag: 'https://restcountries.eu/data/ala.svg', currency: 'Euro' }]; ["land","stan","an"].forEach(s=> console.log(countries.filter(c=> c.name.includes(s))) )
When using Object.keys
you only get the keys of the array.使用Object.keys
时,您只能获取数组的键。 In your case just "0"
and "1"
.在您的情况下,只是"0"
和"1"
。 What you want is the values.你想要的是价值观。 And then it's quite easy to sort using the filter prototype.然后使用过滤器原型很容易进行排序。
const countries = [ { name: 'Afghanistan', capital: 'Kabul', languages: ['Pashto', 'Uzbek', 'Turkmen'], population: 27657145, flag: 'https://restcountries.eu/data/afg.svg', currency: 'Afghan afghani' }, { name: 'Åland Islands', capital: 'Mariehamn', languages: ['Swedish'], population: 28875, flag: 'https://restcountries.eu/data/ala.svg', currency: 'Euro' }]; const countriesArray = Object.values(countries); function categorizeCountries(keyword) { console.log(countriesArray.filter(country => country.name.toLowerCase().includes(keyword.toLowerCase()))) } categorizeCountries('af')
you are looping incorrectly through countries (you don't need Object.keys
and you omitted usage of i
also you have to accamulate your result in array)您在国家/地区的循环不正确(您不需要Object.keys
并且您省略了i
的使用,您还必须将结果累积到数组中)
function categorizeCountries(keyword) {
const result = [];
for (let i = 0; i < countries.length; i++) {
let { name } = countries[i];
if (name.includes(keyword)) {
console.log(name);
result.push(name);
} else {
console.log('Country not found')
}
}
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.