我有以下数组:
{ id: 19531285,
domain: 'fjuhsd.org',
alexa_rank: 458835,
country: 236,
employees: '0',
revenue: '0',
industry_id: '0' },
{ id: 2657031,
domain: 'deporlovers.
alexa_rank: 470687,
country: 209,
employees: '0',
revenue: '0',
industry_id: '0' },
{ id: 1846092,
domain: 'lehighsports
alexa_rank: 477376,
country: 236,
employees: '0',
revenue: '0',
industry_id: '0' },
{ id: 41443846,
domain: 'blacklightsl
alexa_rank: 477964,
country: 0,
employees: '0',
revenue: '0',
industry_id: '0' },
{ id: 3881608,
domain: 'audubonportl
alexa_rank: 478643,
country: 236,
employees: '2',
revenue: '2',
industry_id: '39' },
{ id: 32704527,
domain: 'lowryparkzoo
alexa_rank: 488859,
country: 236,
employees: '0',
revenue: '0',
industry_id: '0' },
{ id: 1907473,
domain: 'citymb.info'
alexa_rank: 490285,
country: 236,
employees: '4',
revenue: '4',
industry_id: '53' },
{ id: 8716166,
domain: 'terrainracin
alexa_rank: 490404,
country: 0,
employees: '0',
revenue: '0',
industry_id: '0' },
{ id: 935439,
domain: 'triatlonchan
alexa_rank: 491953,
country: 83,
employees: '0',
revenue: '0',
industry_id: '0' }
我正在使用Lodash _.filter()函数来尝试对以下数组进行排序,以仅返回值等于收入最多的元素,industry_id和employees。
在这种情况下,这些数字最多(基于查看数组)
收入是:0,industry_id:0和员工:0
如何使用_.filter过滤数组来执行此操作?我还没有设法找到一种方法来做到这一点。
感谢。
答案 0 :(得分:1)
您需要先找到必要字段的最常用值。您可以查看this以了解如何执行此操作。
一旦你拥有最频繁的字段,你就可以做到这一点 -
_.filter(records, record => {
return record.revenue === mostFrequentRevenue &&
record.industry_id === mostFrequentIndustryId &&
record.employees === mostFrequentEmployees;
})
答案 1 :(得分:0)
您可以使用地图来记录发生次数。
function sortExtract(data) {
var sorted = data.sort((a, b) => {
return a.revenue > b.revenue || a.industry_id > b.industry_id || a.employees > b.employees;
})
var count = new Map();
for (let l of sorted) {
var m = l.revenue + l.employees;
if (count.has(m)) {
var val = count.get(m);
val.push(l)
} else count.set(m, [l]);
}
var len = 0;
var obj = 0
var len = Array.from(count, (x, y) => {
if (x[1].length > len) {
len = x[1].length;
obj = x[1]
}
})
return obj[0];
}
var data = [{
id: 19531285,
domain: 'fjuhsd.org',
alexa_rank: 458835,
country: 236,
employees: '0',
revenue: '0',
industry_id: '0'
},
{
id: 2657031,
domain: 'deporlovers',
alexa_rank: 470687,
country: 209,
employees: '0',
revenue: '0',
industry_id: '0'
},
{
id: 1846092,
domain: 'lehighsport',
alexa_rank: 477376,
country: 236,
employees: '0',
revenue: '0',
industry_id: '0'
},
{
id: 41443846,
domain: 'blacklights',
alexa_rank: 477964,
country: 0,
employees: '0',
revenue: '0',
industry_id: '0'
},
{
id: 3881608,
domain: 'audubonport',
alexa_rank: 478643,
country: 236,
employees: '2',
revenue: '2',
industry_id: '39'
},
{
id: 32704527,
domain: 'lowryparkzoo',
alexa_rank: 488859,
country: 236,
employees: '0',
revenue: '0',
industry_id: '0'
},
{
id: 1907473,
domain: 'citymb.info',
alexa_rank: 490285,
country: 236,
employees: '4',
revenue: '4',
industry_id: '53'
},
{
id: 8716166,
domain: 'terrainraci',
alexa_rank: 490404,
country: 0,
employees: '0',
revenue: '0',
industry_id: '0'
},
{
id: 935439,
domain: 'triatloncha',
alexa_rank: 491953,
country: 83,
employees: '0',
revenue: '0',
industry_id: '0'
}
];
console.log(sortExtract(data));
答案 2 :(得分:0)
通过合并收入,industry_id和员工值,为数据中的事件计数,并获得最高值的“id”,为每个项目创建一个“id”。
使用此ID过滤数据。
const data = [{"id":19531285,"domain":"fjuhsd.org","alexa_rank":458835,"country":236,"employees":"0","revenue":"0","industry_id":"0"},{"id":2657031,"domain":"deporlovers","alexa_rank":470687,"country":209,"employees":"0","revenue":"0","industry_id":"0"},{"id":1846092,"domain":"lehighsport","alexa_rank":477376,"country":236,"employees":"0","revenue":"0","industry_id":"0"},{"id":41443846,"domain":"blacklights","alexa_rank":477964,"country":0,"employees":"0","revenue":"0","industry_id":"0"},{"id":3881608,"domain":"audubonport","alexa_rank":478643,"country":236,"employees":"2","revenue":"2","industry_id":"39"},{"id":32704527,"domain":"lowryparkzoo","alexa_rank":488859,"country":236,"employees":"0","revenue":"0","industry_id":"0"},{"id":1907473,"domain":"citymb.info","alexa_rank":490285,"country":236,"employees":"4","revenue":"4","industry_id":"53"},{"id":8716166,"domain":"terrainraci","alexa_rank":490404,"country":0,"employees":"0","revenue":"0","industry_id":"0"},{"id":935439,"domain":"triatloncha","alexa_rank":491953,"country":83,"employees":"0","revenue":"0","industry_id":"0"}];
const getId = ({ revenue, industry_id, employees }) => `${revenue}-${industry_id}-${employees}`; // create the id of the element by combining the required properties
const mostOccurring = _.get(_(data)
.countBy(getId) // count the number of items that have the same "id"
.entries() // get the entries
.maxBy(([_, v]) => v), 0); // find the maximum, and use get to take the id
const result = data.filter((o) => getId(o) === mostOccurring); // filter all items by the most occuring id
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>