[英]Find the total number of counts in array of objects
Include how many of the potential voters were in the ages 18-25, how many from 26-35, how many from 36-55, and how many of each of those age ranges actually voted.包括有多少潜在选民在 18-25 岁之间,有多少人在 26-35 岁之间,有多少人在 36-55 岁之间,以及这些年龄范围中有多少人实际投票。 The resulting object containing this data should have 6 properties.
包含此数据的结果对象应具有 6 个属性。
var voters = [
{name:'Bob' , age: 30, voted: true},
{name:'Jake' , age: 32, voted: true},
{name:'Kate' , age: 25, voted: false},
{name:'Sam' , age: 20, voted: false},
{name:'Phil' , age: 21, voted: true},
{name:'Ed' , age:55, voted:true},
{name:'Tami' , age: 54, voted:true},
{name: 'Mary', age: 31, voted: false},
{name: 'Becky', age: 43, voted: false},
{name: 'Joey', age: 41, voted: true},
{name: 'Jeff', age: 30, voted: true},
{name: 'Zack', age: 19, voted: false}
];
function voterResults(arr) {
// your code here
}
console.log(voterResults(voters)); // Returned value shown below:
/*
{ youngVotes: 1,
youth: 4,
midVotes: 3,
mids: 4,
oldVotes: 3,
olds: 4
}
I am being trying out this specific problem, below is what I have tried, where i am able to form the hashmap.我正在尝试这个特定的问题,下面是我尝试过的,我可以在这里形成哈希图。 But not sure how I can solve the above problem.
但不确定如何解决上述问题。
function voterResults(arr) {
let votesArray = ['youngVotes', 'youth', 'midVotes', 'mids',
'oldVotes', 'olds']
return votesArray.reduce((acc, it) => {
acc[it] = (acc[it] || 0) + 1
return acc;
}, {})
}
//output //输出
{
youngVotes: 1 ,
youth: 1 ,
midVotes: 1 ,
mids: 1 ,
oldVotes: 1 ,
olds: 1
}
Actual output needed:实际需要的输出:
{
youngVotes: 1,
youth: 4,
midVotes: 3,
mids: 4,
oldVotes: 3,
olds: 4
}
I'd first make a helper function that returns the property strings corresponding to the age passed (eg 20
-> ['youth', 'youngVotes']
).我首先创建一个辅助函数,该函数返回与传递的年龄相对应的属性字符串(例如
20
-> ['youth', 'youngVotes']
)。 Then use .reduce
to iterate over the voters
array - call that function to find out which property to increment, and increment it:然后使用
.reduce
迭代voters
数组 - 调用该函数以找出要递增的属性,并递增它:
const getCat = (age) => { if (age < 25) return ['youth', 'youngVotes']; if (age < 35) return ['mids', 'midVotes']; return ['olds', 'oldVotes']; }; var voters = [ {name:'Bob' , age: 30, voted: true}, {name:'Jake' , age: 32, voted: true}, {name:'Kate' , age: 25, voted: false}, {name:'Sam' , age: 20, voted: false}, {name:'Phil' , age: 21, voted: true}, {name:'Ed' , age:55, voted:true}, {name:'Tami' , age: 54, voted:true}, {name: 'Mary', age: 31, voted: false}, {name: 'Becky', age: 43, voted: false}, {name: 'Joey', age: 41, voted: true}, {name: 'Jeff', age: 30, voted: true}, {name: 'Zack', age: 19, voted: false} ]; const counts = voters.reduce((a, { age, voted }) => { const [prop, voteProp] = getCat(age); a[prop] = (a[prop] || 0) + 1; if (voted) { a[voteProp] = (a[voteProp] || 0) + 1; } return a; }, {}); console.log(counts);
You need to use input arr, and use-value of voted and use age to classify and increment value in the object.您需要使用输入的arr、投票的使用值和使用年龄对对象中的值进行分类和递增。
const voters = [
{name:'Bob' , age: 30, voted: true},
{name:'Jake' , age: 32, voted: true},
{name:'Kate' , age: 25, voted: false},
{name:'Sam' , age: 20, voted: false},
{name:'Phil' , age: 21, voted: true},
{name:'Ed' , age:55, voted:true},
{name:'Tami' , age: 54, voted:true},
{name: 'Mary', age: 31, voted: false},
{name: 'Becky', age: 43, voted: false},
{name: 'Joey', age: 41, voted: true},
{name: 'Jeff', age: 30, voted: true},
{name: 'Zack', age: 19, voted: false}
];
// define your limit here, will check for <= of defined age
let categories = { youngVotes: 21, youth: 30, midVotes: 40, mids: 50, oldVotes: 60, olds: 130}
function voterResults(arr) {
const conditions = Object.entries(categories);
return arr.reduce((val, vote)=>{
if(vote.voted) {
for(let i=0;i<conditions.length;i++) {
if(vote.age <= conditions[i][1]) {
val[conditions[i][0]] = val[conditions[i][0]] ? val[conditions[i][0]] + 1 : 1;
return val;
}
}
}
return val;
}, {})
}
console.log(voterResults(voters));
Here is using reduce
method and age groups can be extended easily.这里使用的是
reduce
方法,年龄组可以很容易地扩展。
function voterResults(arr) { const ranges = { youngVotes: [18, 25], youth: [26, 35] }; return arr .filter(x => x.voted) .reduce((acc, curr) => { Object.keys(ranges).forEach(key => { if (curr.age >= ranges[key][0] && curr.age <= ranges[key][1]) { acc[key] = key in acc ? acc[key] + 1 : 1; } }); return acc; }, {}); } var voters = [ { name: "Bob", age: 30, voted: true }, { name: "Jake", age: 32, voted: true }, { name: "Kate", age: 25, voted: false }, { name: "Sam", age: 20, voted: false }, { name: "Phil", age: 21, voted: true }, { name: "Ed", age: 55, voted: true }, { name: "Tami", age: 54, voted: true }, { name: "Mary", age: 31, voted: false }, { name: "Becky", age: 43, voted: false }, { name: "Joey", age: 41, voted: true }, { name: "Jeff", age: 30, voted: true }, { name: "Zack", age: 19, voted: false } ]; console.log(voterResults(voters));
You can do simply using if conditions您可以简单地使用 if 条件
var voters = [
{name:'Bob' , age: 30, voted: true},
{name:'Jake' , age: 32, voted: true},
{name:'Kate' , age: 25, voted: false},
{name:'Sam' , age: 20, voted: false},
{name:'Phil' , age: 21, voted: true},
{name:'Ed' , age:55, voted:true},
{name:'Tami' , age: 54, voted:true},
{name: 'Mary', age: 31, voted: false},
{name: 'Becky', age: 43, voted: false},
{name: 'Joey', age: 41, voted: true},
{name: 'Jeff', age: 30, voted: true},
{name: 'Zack', age: 19, voted: false}
];
function voterResults(){
let youngVotes = 0;
let youth = 0;
let midVotes = 0;
let mids =0;
let oldVotes =0;
let olds =0;
for (var i = voters.length - 1; i >= 0; i--) {
if(voters[i].age >= 18 && voters[i].age <= 25 && voters[i].voted === true){
youngVotes++;
}
if(voters[i].age >= 18 && voters[i].age <= 25){
youth++;
}
if(voters[i].age >= 26 && voters[i].age <= 35 && voters[i].voted === true){
midVotes++;
}
if(voters[i].age >= 26 && voters[i].age <= 35 ){
mids++;
}
if(voters[i].age >= 36 && voters[i].age <= 55 && voters[i].voted === true){
oldVotes++;
}
if(voters[i].age >= 36 && voters[i].age <= 55){
olds++;
}
}
return{
youngVotes,
youth,
midVotes,
mids,
oldVotes,
olds
}
}
console.log(voterResults())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.