如何通过子数组对象属性计算父数组的项数

时间:2017-03-27 07:18:26

标签: javascript arrays

据说我有一系列人员,有很多办公室。

var personnel = [
  {
    id: 1,
    name: 'John Doe',
    offices: [
      {
        id: 1,
        name: 'Office 1',
        isAssigned: false
      },
      {
        id: 2,
        name: 'Office 2',
        isAssigned: true
      },
      {
        id: 3,
        name: 'Office 3',
        isAssigned: false
      }
    ]
  },{
    id: 2,
    name: 'Jerry Smith',
    offices: [
      {
        id: 2,
        name: 'Office 2',
        isAssigned: true,
      },
      {
        id: 3,
        name: 'Office 3',
        isAssigned: false,
      }
    ]
  }
]

我如何获得在特定办公室分配的人员的数量?或者office 2中分配了多少?结果:2

5 个答案:

答案 0 :(得分:2)

这是一个解构参数的版本:

personnel.filter( ({offices}) => 
    offices.some( ({name, isAssigned}) => isAssigned && name === office) 
).length

function countInOffice(personnel, office) {
    return personnel.filter( ({offices}) => 
        offices.some( ({name, isAssigned}) => isAssigned && name === office) 
    ).length
}

var personnel = [
  {
    id: 1,
    name: 'John Doe',
    offices: [
      {
        id: 1,
        name: 'Office 1',
        isAssigned: false
      },
      {
        id: 2,
        name: 'Office 2',
        isAssigned: true
      },
      {
        id: 3,
        name: 'Office 3',
        isAssigned: false
      }
    ]
  },{
    id: 2,
    name: 'Jerry Smith',
    offices: [
      {
        id: 2,
        name: 'Office 2',
        isAssigned: true,
      },
      {
        id: 3,
        name: 'Office 3',
        isAssigned: false,
      }
    ]
  }
];

var count = countInOffice(personnel, 'Office 2');

console.log(count);

答案 1 :(得分:1)

迭代指定的办公室并将计数添加到对象

这将得到每个办公室的计数

var personnel = [{
  id: 1,
  name: 'John Doe',
  offices: [{
      id: 1,
      name: 'Office 1',
      isAssigned: false
    },
    {
      id: 2,
      name: 'Office 2',
      isAssigned: true
    },
    {
      id: 3,
      name: 'Office 3',
      isAssigned: false
    }
  ]
}, {
  id: 2,
  name: 'Jerry Smith',
  offices: [{
      id: 2,
      name: 'Office 2',
      isAssigned: true,
    },
    {
      id: 3,
      name: 'Office 3',
      isAssigned: false,
    }
  ]
}];

let offices = {};
personnel.forEach(p=>{
  p.offices.filter(o=>o.isAssigned).forEach(o=>{
    offices[o.name] = offices[o.name] || 0;
    offices[o.name]++;
  });
});

console.log(offices);
let officeToCheck = "Office 2";
console.log(offices[officeToCheck]);

如果您需要直接获取特定办公室的计数

var personnel = [{
  id: 1,
  name: 'John Doe',
  offices: [{
      id: 1,
      name: 'Office 1',
      isAssigned: false
    },
    {
      id: 2,
      name: 'Office 2',
      isAssigned: true
    },
    {
      id: 3,
      name: 'Office 3',
      isAssigned: false
    }
  ]
}, {
  id: 2,
  name: 'Jerry Smith',
  offices: [{
      id: 2,
      name: 'Office 2',
      isAssigned: true,
    },
    {
      id: 3,
      name: 'Office 3',
      isAssigned: false,
    }
  ]
}];

let count = personnel.filter(p=>p.offices.some(o=>o.name==='Office 2'&&o.isAssigned)).length;
console.log(count);

答案 2 :(得分:1)

要获得总计数(所有办事处的统计数据“为每个办事处分配了多少员工”),请使用以下方法:

var personnel = [{id: 1,name: 'John Doe',offices: [{id: 1,name: 'Office 1',isAssigned: false},{id: 2,name: 'Office 2',isAssigned: true},{id: 3,name: 'Office 3',isAssigned: false}]},{id: 2,name: 'Jerry Smith',offices: [{id: 2,name: 'Office 2',isAssigned: true,},{id: 3,name: 'Office 3',isAssigned: false,}]}
],

    counts = personnel.reduce(function (r, o) {
        o.offices.forEach(function (i) {
            (r[i.name])? ++r[i.name] : r[i.name] = 1;
        });
        return r;
    }, {});

console.log(counts);

答案 3 :(得分:1)

您可以使用两个嵌套的Array#reduce并检查给定的名称。



function getCount(name) {
    return personnel.reduce(function (r, a) {
        return a.offices.reduce(function (s, b) {
            return s + (b.name === name);
        }, r);
    }, 0);
}

var personnel = [{ id: 1, name: 'John Doe', offices: [{ id: 1, name: 'Office 1', isAssigned: false }, { id: 2, name: 'Office 2', isAssigned: true }, { id: 3, name: 'Office 3', isAssigned: false }] }, { id: 2, name: 'Jerry Smith', offices: [{ id: 2, name: 'Office 2', isAssigned: true, }, { id: 3, name: 'Office 3', isAssigned: false, }] }],
    result = getCount('Office 2');

console.log(result);




如果您在一个办公室中只有一个名称,则可以使用Array#some并在找到一个名称时退出。



function getCount(name) {
    return personnel.reduce(function (r, a) {
        return r + a.offices.some(function (b) {
            return b.name === name;
        });
    }, 0);
}

var personnel = [{ id: 1, name: 'John Doe', offices: [{ id: 1, name: 'Office 1', isAssigned: false }, { id: 2, name: 'Office 2', isAssigned: true }, { id: 3, name: 'Office 3', isAssigned: false }] }, { id: 2, name: 'Jerry Smith', offices: [{ id: 2, name: 'Office 2', isAssigned: true, }, { id: 3, name: 'Office 3', isAssigned: false, }] }],
    result = getCount('Office 2');

console.log(result);




ES6



function getCount(name) {
    return personnel.reduce((r, a) =>  r + a.offices.some(b => b.name === name), 0);
}

var personnel = [{ id: 1, name: 'John Doe', offices: [{ id: 1, name: 'Office 1', isAssigned: false }, { id: 2, name: 'Office 2', isAssigned: true }, { id: 3, name: 'Office 3', isAssigned: false }] }, { id: 2, name: 'Jerry Smith', offices: [{ id: 2, name: 'Office 2', isAssigned: true, }, { id: 3, name: 'Office 3', isAssigned: false, }] }],
    result = getCount('Office 2');

console.log(result);




答案 4 :(得分:1)

使用Array.prototype.filter()Array.prototype.some()

const office2Personnel = personnel.filter(p =>
        p.offices.some(o => o.name === 'Office 2' && o.isAssigned));

console.log(office2Personnel.length);



const personnel = [
  {
    id: 1,
    name: 'John Doe',
    offices: [
      {
        id: 1,
        name: 'Office 1',
        isAssigned: false
      },
      {
        id: 2,
        name: 'Office 2',
        isAssigned: true
      },
      {
        id: 3,
        name: 'Office 3',
        isAssigned: false
      }
    ]
  },{
    id: 2,
    name: 'Jerry Smith',
    offices: [
      {
        id: 2,
        name: 'Office 2',
        isAssigned: true,
      },
      {
        id: 3,
        name: 'Office 3',
        isAssigned: false,
      }
    ]
  }
];

const office2Personnel = personnel.filter(p => p.offices.some(o => o.name === 'Office 2' && o.isAssigned));

console.log(office2Personnel.length);