[英]How to get keys and count of each key from an array of objects
我试图在下面获得预期的结果,但是我很挣扎。 最好的方法是什么?
const data = [{ name: 'Dave', country: 'England', color: 'Brown' }, { name: 'Dave', country: 'England', color: 'white' }, { name: 'Cae', country: 'USA', color: 'white' }, { name: 'Dave', country: 'England', color: 'Red' }, { name: 'Cae', country: 'USA', color: 'Yellow' }, { name: 'Dave', country: 'England', color: 'white' }, { name: 'Manuel', country: 'USA', color: 'Red' }, { name: 'Dave', country: 'England', color: 'white' } ]; // Tentative: (function getDataForName() { count = 0; nameL = []; nameCount = []; for (let i = 0; i < data.length; i++) { if (nameL.indexOf(data[i].name) === -1) { nameL.push(data[i].name); count++; } nameCount.push(count); } console.log(nameL); console.log(nameCount); })()
预期成绩:
nameL = ['Dave', 'Cae', 'Manuel'];
nameCount = [4, 2, 1];
如何为您工作? Array.prototype.reduce()
只是在传入先前值的同时继续在数组上调用此回调。
const data = [{name: 'Dave', country: 'England', color: 'Brown'},{name: 'Dave', country: 'England', color: 'white'},{name: 'Cae', country: 'USA', color: 'white'},{name: 'Dave', country: 'England', color: 'Red'},{name: 'Cae', country: 'USA', color: 'Yellow'},{name: 'Dave', country: 'England', color: 'white'},{name: 'Manuel', country: 'USA', color: 'Red'},{name: 'Dave', country: 'England', color: 'white'}]; x = data.reduce(function(sums,entry){ sums[entry.name] = (sums[entry.name] || 0) + 1; return sums; },{}); console.log(x)
使用Lodash,可以使用countBy
方法获取一个对象,然后可以在该对象上使用keys
和values
方法。
const data = [{name: 'Dave', country: 'England', color: 'Brown'},{name: 'Dave', country: 'England', color: 'white'},{name: 'Cae', country: 'USA', color: 'white'},{name: 'Dave', country: 'England', color: 'Red'},{name: 'Cae', country: 'USA', color: 'Yellow'},{name: 'Dave', country: 'England', color: 'white'},{name: 'Manuel', country: 'USA', color: 'Red'},{name: 'Dave', country: 'England', color: 'white'}] const result = _.countBy(data, 'name') const names = _.keys(result); const count = _.values(result); console.log(names); console.log(count)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
其他方式:
const [nameL, nameCount] = _.chain(data)
.groupBy('name')
.mapValues(_.size)
.toPairs()
.thru(_.spread(_.zip))
.value();
将forEach
函数与Object.keys
和Object.values
函数一起使用。
const data = [ {name: 'Dave', country: 'England', color: 'Brown'}, {name: 'Dave', country: 'England', color: 'white'}, {name: 'Cae', country: 'USA', color: 'white'}, {name: 'Dave', country: 'England', color: 'Red'}, {name: 'Cae', country: 'USA', color: 'Yellow'}, {name: 'Dave', country: 'England', color: 'white'}, {name: 'Manuel', country: 'USA', color: 'Red'}, {name: 'Dave', country: 'England', color: 'white'} ], a = {}; data.forEach((c) => a[c.name] = (a[c.name] || 0) + 1); console.log(JSON.stringify(Object.keys(a))); console.log(JSON.stringify(Object.values(a)));
.as-console-wrapper { max-height: 100% !important; top: 0; }
这样做无需任何外部库:
const data=[{name:'Dave',country:'England',color:'Brown'},{name:'Dave',country:'England',color:'white'},{name:'Cae',country:'USA',color:'white'},{name:'Dave',country:'England',color:'Red'},{name:'Cae',country:'USA',color:'Yellow'},{name:'Dave',country:'England',color:'white'},{name:'Manuel',country:'USA',color:'Red'},{name:'Dave',country:'England',color:'white'}]; const result = data.reduce(function(count, entry){ count[entry.name] = count[entry.name] ? count[entry.name] + 1 : 1; return count; },{}); console.log(Object.keys(result)); console.log(Object.values(result));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.