我想在下面得到预期的结果,但我正在努力。最好的方法是什么?
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];
答案 0 :(得分:2)
这对你有什么用? 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)
答案 1 :(得分:2)
使用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)
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
&#13;
答案 2 :(得分:1)
const [nameL, nameCount] = _.chain(data)
.groupBy('name')
.mapValues(_.size)
.toPairs()
.thru(_.spread(_.zip))
.value();
答案 3 :(得分:0)
使用功能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; }
答案 4 :(得分: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));