Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
How to change parameter of objects in array depending on their similarity?

I have this array of objects:

const data = [
  { position: 1, name: "a", score: 9000 },
  { position: 2, name: "b", score: 8000 },
  { position: 3, name: "c", score: 6000 },
  { position: 3, name: "c", score: 6000 },
  { position: 4, name: "d", score: 6000 },
  { position: 5, name: "e", score: 6000 },
  { position: 6, name: "f", score: 6000 },
  { position: 7, name: "g", score: 4000 },
  { position: 8, name: "h", score: 3000 },
  { position: 9, name: "i", score: 2500 },
  { position: 10, name: "j", score: 2500 },
  { position: 11, name: "k", score: 1000 },
  { position: 12, name: "l", score: 1000 },
];

I'm trying to iterate through it using only simple JavaScript to get the following result:

const data = [
  { position: "1", name: "a", score: 9000 },
  { position: "2", name: "b", score: 8000 },
  { position: "3-6", name: "c", score: 6000 },
  { position: "3-6", name: "c", score: 6000 },
  { position: "3-6", name: "d", score: 6000 },
  { position: "3-6", name: "e", score: 6000 },
  { position: "3-6", name: "f", score: 6000 },
  { position: "7", name: "g", score: 4000 },
  { position: "8", name: "h", score: 3000 },
  { position: "9-10", name: "i", score: 2500 },
  { position: "9-10", name: "j", score: 2500 },
  { position: "11-12", name: "k", score: 1000 },
  { position: "11-12", name: "l", score: 1000 },
];

I've tried a lot of different approaches but nothing works, unfortunately. Any tips how to achieve this? Thanks in advance.

This is probably the closest that I got:

function placement() {
  let repeat=0;
  for (let i = 0; i < data.length; i++) {
    let counter = 0;
    for (let j = 0; j < data.length; j++) {
      if (data[i].score == data[j].score) {
        if(data[i].position==data[j].position){
          repeat++;
        }
        counter++;
      }
    }
    if (counter > 1) {
      let k;
      let start=data[i].position;
      for (k = i; k < i + counter-1; k++) {
        data[k].position =
          start + "-" + data[i + counter-1].position;
      }
      i=k;
    }
  }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Assuming that the items in data are sorted by the score, i.e., the ranges are compressed into one instance per value, you can do the following:

  1. Iterate over the array using Array#reduce while updating a Map having the score as the key, and its start and end positions as the value.
  2. Then, using Array#map, iterate over the array again, and for each item, set the position to the values saved in the Map:

const data = [
  { position: 1, name: "a", score: 9000 },
  { position: 2, name: "b", score: 8000 },
  { position: 3, name: "c", score: 6000 },
  { position: 3, name: "c", score: 6000 },
  { position: 4, name: "d", score: 6000 },
  { position: 5, name: "e", score: 6000 },
  { position: 6, name: "f", score: 6000 },
  { position: 7, name: "g", score: 4000 },
  { position: 8, name: "h", score: 3000 },
  { position: 9, name: "i", score: 2500 },
  { position: 10, name: "j", score: 2500 },
  { position: 11, name: "k", score: 1000 },
  { position: 12, name: "l", score: 1000 },
];

const scoreMap = data.reduce((map, { position, score }) => {
  const { start } = map.get(score) || {};
  if(start) (map.get(score)).end = position;
  else map.set(score, { start: position });
  return map;  
}, new Map);

const res = data.map(e => { 
  const { start, end } = scoreMap.get(e.score);
  return { ...e, position: end ? `${start}-${end}` : `${start}` };
});

console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

You could group by score first and then add the position for each group.

const
    data = [{ position: 1, name: "a", score: 9000 }, { position: 2, name: "b", score: 8000 }, { position: 3, name: "c", score: 6000 }, { position: 3, name: "c", score: 6000 }, { position: 4, name: "d", score: 6000 }, { position: 5, name: "e", score: 6000 }, { position: 6, name: "f", score: 6000 }, { position: 7, name: "g", score: 4000 }, { position: 8, name: "h", score: 3000 }, { position: 9, name: "i", score: 2500 }, { position: 10, name: "j", score: 2500 }, { position: 11, name: "k", score: 1000 }, { position: 12, name: "l", score: 1000 }],
    result = data
        .reduce((r, o, i, a) => {
            if (!i || a[i - 1].score !== o.score) r.push([]);
            r[r.length - 1].push(o);
            return r;
        }, [])
        .flatMap(a => a.map((o, i, b) => b.length === 1
            ? o
            : { ...o, position: `${b[0].position}-${b[b.length - 1].position}` }
        ));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!