0

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

3
  • 1
    I am not sure that I understand what logic you need to implement. Could you provide me the example of result of execution on this array? Maybe, one more example, too. Also, do you mean one, two, three records, or first, second, third records? Commented Oct 2, 2021 at 8:26
  • I have updated the question, I hope it makes a little more sense now, apologies if unclear Commented Oct 2, 2021 at 8:42
  • Yes, that is better with an example. Hope, I've understood what you need (the code is in answer below) Commented Oct 2, 2021 at 9:07

3 Answers 3

1

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));

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You, that works and is exactly what i am wanting to achieve
0

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
})

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.