javascript 求数组中对象的总数

vawmfj5a  于 2023-05-21  发布在  Java
关注(0)|答案(5)|浏览(122)

包括18-25岁的潜在选民有多少,26-35岁的有多少,36-55岁的有多少,以及每个年龄段有多少人实际投票。包含此数据的结果对象应具有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 
}

我正在尝试解决这个特定的问题,下面是我所尝试的,在那里我能够形成散列表。但我不知道如何解决上述问题。

function voterResults(arr) {
  let votesArray = ['youngVotes', 'youth', 'midVotes', 'mids', 
      'oldVotes', 'olds']
   return votesArray.reduce((acc, it) => {
     acc[it] = (acc[it] || 0) + 1  
     return acc;
   }, {})
}

//输出

{
   youngVotes: 1 ,
   youth: 1 ,
   midVotes: 1 ,
   mids: 1 ,
   oldVotes: 1 ,
   olds: 1
}

实际需要产量:

{
  youngVotes: 1,
  youth: 4,
  midVotes: 3,
  mids: 4,
  oldVotes: 3,
  olds: 4 
}
lnlaulya

lnlaulya1#

我首先创建一个helper函数,它返回与所传递的年龄相对应的属性字符串(例如20-> ['youth', 'youngVotes'])。然后使用.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);
9q78igpj

9q78igpj2#

你需要使用input arr,使用voted的-value,并使用age来分类和增加对象中的值。

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));
wrrgggsh

wrrgggsh3#

这里是使用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));
xsuvu9jc

xsuvu9jc4#

您可以简单地使用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())
gblwokeq

gblwokeq5#

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 age = (age) => {
     return age<26?  ['numYoungPeople', 'numYoungVotes']: 
     (age < 35 ?  ['numMidsPeople', 'numMidVotesPeople']:
     ['numOldsPeople', 'numOldVotesPeople'])
}

function voterResults(arr) {

const obs = arr.reduce((acc, num) =>{
    acc[age(num.age)[0]] =(acc[age(num.age)[0]] || 0) +1
    acc[age(num.age)[1]] = num.voted? (acc[age(num.age)[1]] || 0 )+1: (acc[age(num.age)[1]] || 0)
    return acc;
}, {})

return obs;
}
console.log(voterResults(voters)); // Returned value shown below:
/*
{ numYoungVotes: 1,
  numYoungPeople: 4,
  numMidVotesPeople: 3,
  numMidsPeople: 4,
  numOldVotesPeople: 3,
  numOldsPeople: 4 
}
*/

相关问题