我必须返回来自欧洲的JavaScript开发人员的数量。我有一个包含4个对象的数组。 (名称,国家和语言等)
//以供参考并澄清问题
var list1 = [
{ firstName: 'Noah', lastName: 'M.', country: 'Switzerland',
continent: 'Europe', age: 19, language: 'JavaScript' },
{ firstName: 'Maia', lastName: 'S.', country: 'Tahiti', continent:
'Oceania', age: 28, language: 'JavaScript' },
{ firstName: 'Shufen', lastName: 'L.', country: 'Taiwan',
continent: 'Asia', age: 35, language: 'HTML' },
{ firstName: 'Sumayah', lastName: 'M.', country: 'Tajikistan',
continent: 'Asia', age: 30, language: 'CSS' }
];
//我的下面的代码
function countDevelopers(list) {
let euro = 0;
for( let coder = 0; coder < list.length; coder++ ){
list[coder]["continent"];
if( list[coder]["continent"] == 'Europe' ){
for (let lang = 0; lang < list[coder].length; lang++ ){
list[lang]["language"];
if( list[coder][lang]["language"] == 'Javascript'){
euro++;
}
}
}
}
return euro;
}
答案 0 :(得分:2)
您可以通过reduce
调用来实现此功能:
let count = list1.reduce(
(sum, person) => {
let european_using_javascript = person.continent === 'Europe' && person.language === 'JavaScript';
// `Number(boolean)` returns `1` if true, and `0` if false
// Can also use `+european_using_javascript` unary operator, or
// `european_using_javascript ? 1 : 0`
return sum + Number(european_using_javascript);
},
// Initialize the accumulator to `0`
0
);
console.log(count);
答案 1 :(得分:2)
让我们看一下您的代码当前正在做什么:
function countDevelopers(list) {
let euro = 0;
for( let coder = 0; coder < list.length; coder++ ){
它遍历了所有编码器的整个列表。
list[coder]["continent"];
此行绝对不执行任何操作;您可以将其删除。
if( list[coder]["continent"] == 'Europe' ){
for (let lang = 0; lang < list[coder].length; lang++ ){
如果当前的编码器来自欧洲,那么您将再次遍历所有编码器-等待。您觉得这对吗?您只需要知道此编码器是否使用JavaScript,就不必费心看所有其他编码器。它们与该编码器无关。
list[lang]["language"];
另一行什么也不做,可以删除。
if( list[coder][lang]["language"] == 'Javascript'){
什么是list[coder][lang]
?编码器和lang都是从头到尾遍历整个数组索引的变量。假设您使用的是第二个编码器(索引1)和第一个lang编码器(索引0),那么这是在请求list[1][0].language
。但是列表中的每个元素都是一个对象,而不是一个数组,因此没有索引0。这行没有意义。
编码的最后一部分是递增euro
,就可以了。
因此,您真正需要的只是一个循环:
for (let coder = 0; coder < list.length; coder++) {
if (list[coder].continent === 'Europe' && list[coder].language === 'JavaScript') {
euro++;
}
}
(如果您想花哨的话,可以通过Array.reduce
操作使其更加简洁:
let euro = list.reduce((total, coder) => total + (coder.continent === 'Europe' && coder.language === 'JavaScript') ? 1 : 0, 0);
)
答案 2 :(得分:1)
迭代项目,如果用户所在的洲是欧洲,并且语言是Javascript,则将计数增加1:
function countDevelopers(list) {
let count = 0;
for(var i = 0; i < list1.length; i++){
const user = list1[i];
if(user.continent === 'Europe' && user.language === 'JavaScript') {
count += 1;
}
}
return count;
}
const list1 = [{"firstName":"Noah","lastName":"M.","country":"Switzerland","continent":"Europe","age":19,"language":"JavaScript"},{"firstName":"Maia","lastName":"S.","country":"Tahiti","continent":"Oceania","age":28,"language":"JavaScript"},{"firstName":"Shufen","lastName":"L.","country":"Taiwan","continent":"Asia","age":35,"language":"HTML"},{"firstName":"Sumayah","lastName":"M.","country":"Tajikistan","continent":"Asia","age":30,"language":"CSS"}];
const result = countDevelopers(list1);
console.log(result);
答案 3 :(得分:1)
有两种方法,一种用foreach
,另一种用reduce
。
var list1 = [{
firstName: 'Noah',
lastName: 'M.',
country: 'Switzerland',
continent: 'Europe',
age: 19,
language: 'JavaScript'
},
{
firstName: 'Maia',
lastName: 'S.',
country: 'Tahiti',
continent: 'Oceania',
age: 28,
language: 'JavaScript'
},
{
firstName: 'Shufen',
lastName: 'L.',
country: 'Taiwan',
continent: 'Asia',
age: 35,
language: 'HTML'
},
{
firstName: 'Sumayah',
lastName: 'M.',
country: 'Tajikistan',
continent: 'Asia',
age: 30,
language: 'CSS'
}
];
const countDev = (x) => {
let count = 0;
x.forEach(x => {
if (x.continent === "Europe" && x.language === "JavaScript") count++
})
return count;
}
console.log(countDev(list1))
// OR
const countDev1 = (x) => {
return x.reduce((accum, x) => {
if (x.continent === "Europe" && x.language === "JavaScript") return ++accum;
return accum;
}, 0)
}
console.log(countDev1(list1))
答案 4 :(得分:0)
您可以使用过滤器,并具有提供国家和语言的通用功能
var list1 = [
{ firstName: 'Noah', lastName: 'M.', country: 'Switzerland',
continent: 'Europe', age: 19, language: 'JavaScript' },
{ firstName: 'Maia', lastName: 'S.', country: 'Tahiti', continent:
'Oceania', age: 28, language: 'JavaScript' },
{ firstName: 'Shufen', lastName: 'L.', country: 'Taiwan',
continent: 'Asia', age: 35, language: 'HTML' },
{ firstName: 'Sumayah', lastName: 'M.', country: 'Tajikistan',
continent: 'Asia', age: 30, language: 'CSS' }
];
function countDevelopers(list, continent, language) {
return list.filter(developer => {return developer.continent == continent && developer.language == language}).length
}
console.log(countDevelopers(list1, 'Europe', 'JavaScript'));