Home > Back-end >  Retrieve conditional amount of objects from array
Retrieve conditional amount of objects from array

Time:10-02

Trying to work out how to retrieve a maximum amount of records from an array of objects

Lets say I have an array of objects like so (i've reduced the amount for readability, but there can be hundreds)

[
 { name: 'Roger East',
   kick_off: 2021-10-01T14:00:00.000Z
 },
 { name: 'Anthony Taylor',
   kick_off: 2021-10-01T14:00:00.000Z
 },
 { name: 'Mike Dean',
   kick_off: 2021-09-20T14:00:00.000Z
 },
 { name: 'Roger East',
   kick_off: 2021-09-19T14:00:00.000Z
 },
 { name: 'Mike Dean',
   kick_off: 2021-08-10T14:00:00.000Z
 },
 { name: 'Anthony Taylor',
   kick_off: 2021-08-09T14:00:00.000Z
 }
] 

For my example I may only want to retrieve 1, 2, 3 or 4 records of each unique name (ordered by latest kick off date, though they are in kick_off order in the array so there may be no need to have that logic)

So lets say in my original object I have 100 records for Roger East, 90 Records for Mike Dean and 50 Records for Anthony Taylor. I would like to return the first 3 records for each name in the array (hope that makes sense)

So i would end up with

[
 { name: 'Roger East',
   kick_off: 2021-10-01T14:00:00.000Z
 },
 { name: 'Roger East',
   kick_off: 2021-10-01T14:00:00.000Z
 },
 { name: 'Roger East',
   kick_off: 2021-09-20T14:00:00.000Z
 },
 { name: 'Mike Dean',
   kick_off: 2021-09-19T14:00:00.000Z
 },
 { name: 'Mike Dean',
   kick_off: 2021-08-10T14:00:00.000Z
 },
 { name: 'Mike Dean',
   kick_off: 2021-08-10T14:00:00.000Z
 },
 { name: 'Anthony Taylor',
   kick_off: 2021-08-09T14:00:00.000Z
 },
 { name: 'Anthony Taylor',
   kick_off: 2021-08-09T14:00:00.000Z
 },
 { name: 'Anthony Taylor',
   kick_off: 2021-08-09T14:00:00.000Z
 }
] 

Thanks

CodePudding user response:

Here you are:

let data1 = [
 {
   name: 'Roger East',
   kick_off: '2021-10-01T14:00:00.000Z'
 },
 {
   name: 'Anthony Taylor',
   kick_off: '2021-10-01T14:00:00.000Z'
 },
 {
   name: 'Mike Dean',
   kick_off: '2021-09-20T14:00:00.000Z'
 },
 {
   name: 'Roger East',
   kick_off: '2021-09-19T14:00:00.000Z'
 },
 {
   name: 'Mike Dean',
   kick_off: '2021-08-10T14:00:00.000Z'
 },
 { 
   name: 'Anthony Taylor',
   kick_off: '2021-08-09T14:00:00.000Z'
 }
];
let data2 = [
 {
   name: 'Roger East',
   kick_off: '2021-10-01T14:00:00.000Z'
 },
 {
   name: 'Anthony Taylor',
   kick_off: '2021-10-01T14:00:00.000Z'
 },
 {
   name: 'Anthony Taylor',
   kick_off: '2021-10-02T14:00:00.000Z'
 },
 {
   name: 'Mike Dean',
   kick_off: '2021-09-20T14:00:00.000Z'
 },
 {
   name: 'Roger East',
   kick_off: '2021-09-19T14:00:00.000Z'
 },
 {
   name: 'Anthony Taylor',
   kick_off: '2021-10-06T14:00:00.000Z'
 },
 {
   name: 'Anthony Taylor',
   kick_off: '2021-11-07T14:00:00.000Z'
 },
 {
   name: 'Mike Dean',
   kick_off: '2021-08-10T14:00:00.000Z'
 },
 { 
   name: 'Anthony Taylor',
   kick_off: '2021-08-09T14:00:00.000Z'
 },
 {
   name: 'Anthony Taylor',
   kick_off: '2021-10-22T14:00:00.000Z'
 },
 {
   name: 'Anthony Taylor',
   kick_off: '2021-10-24T14:00:00.000Z'
 },
];
const limitEachNameByNumber1 = 1;
const limitEachNameByNumber2 = 3;

function receiveFilteredData(data, limit) {
  let filteredData = [];
  let counter = {};
  
  for (let record of data) {
    if (!counter[record.name]) {
      counter[record.name] = 0;
    }
    
    if (counter[record.name] >= limit) {
      continue;
    }
    
    filteredData.push(record);
    counter[record.name]   ;
  }

  return filteredData;
}

console.log(receiveFilteredData(data1, limitEachNameByNumber1));
console.log(receiveFilteredData(data2, limitEachNameByNumber2));

CodePudding user response:

here's what you can do with map and filter

let dataset = [{
    name: 'Roger East',
    kick_off: '2021-10-01T14:00:00.000Z'
  },
  {
    name: 'Anthony Taylor',
    kick_off: '2021-10-01T14:00:00.000Z'
  },
  {
    name: 'Mike Dean',
    kick_off: '2021-09-20T14:00:00.000Z'
  },
  {
    name: 'Roger East',
    kick_off: '2021-09-19T14:00:00.000Z'
  },
  {
    name: 'Mike Dean',
    kick_off: '2021-08-10T14:00:00.000Z'
  },
  {
    name: 'Anthony Taylor',
    kick_off: '2021-08-09T14:00:00.000Z'
  }
]

let names = [...new Set(dataset.map(d => d.name))]

let firstuser = dataset.filter(x => x.name === names[0]) // use any index for unique entries with name
let seconduser = dataset.filter(x => x.name === names[1])
let thirduser = dataset.filter(x => x.name === names[2])

console.log({
  firstuser,
  seconduser,
  thirduser
})

CodePudding user response:

const src = [{ name: "Roger East", kick_off: "2021-10-01T14:00:00.000Z" },{ name: "Anthony Taylor", kick_off: "2021-10-01T14:00:00.000Z" },{ name: "Mike Dean", kick_off: "2021-09-20T14:00:00.000Z" },{ name: "Roger East", kick_off: "2021-09-19T14:00:00.000Z" },{ name: "Mike Dean", kick_off: "2021-08-10T14:00:00.000Z" },{ name: "Anthony Taylor", kick_off: "2021-08-09T14:00:00.000Z" },{ name: "Roger East", kick_off: "2021-10-01T14:00:00.000Z" },{ name: "Anthony Taylor", kick_off: "2021-10-01T14:00:00.000Z" },{ name: "Mike Dean", kick_off: "2021-09-20T14:00:00.000Z" },{ name: "Roger East", kick_off: "2021-09-19T14:00:00.000Z" },{ name: "Mike Dean", kick_off: "2021-08-10T14:00:00.000Z" },{ name: "Anthony Taylor", kick_off: "2021-08-09T14:00:00.000Z" },{ name: "Roger East", kick_off: "2021-10-01T14:00:00.000Z" },{ name: "Anthony Taylor", kick_off: "2021-10-01T14:00:00.000Z" },{ name: "Mike Dean", kick_off: "2021-09-20T14:00:00.000Z" },{ name: "Roger East", kick_off: "2021-09-19T14:00:00.000Z" },{ name: "Mike Dean", kick_off: "2021-08-10T14:00:00.000Z" },{ name: "Anthony Taylor", kick_off: "2021-08-09T14:00:00.000Z" }];



const byName = src.reduce((acc, {
  name,
  kick_off
}) => {
  if (!acc[name]) {
    acc[name] = [];
  } else {
    acc[name].push(kick_off);
  }
  return acc;
}, {});


const result = Object.keys(byName).map((name) => byName[name].slice(0, 3).map(kick_off => ({
  name,
  kick_off
}))).flat()



console.log(result)

  • Related