首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何根据对象的相似性来改变数组中的对象参数?

如何根据对象的相似性来改变数组中的对象参数?
EN

Stack Overflow用户
提问于 2021-08-23 20:56:09
回答 2查看 62关注 0票数 3

我有一组对象:

代码语言:javascript
运行
复制
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 },
];

我试图只使用简单的JavaScript来迭代它,以获得以下结果:

代码语言:javascript
运行
复制
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 },
];

我尝试过很多不同的方法,但不幸的是,没有任何效果。有什么窍门可以实现吗?提前谢谢。

这可能是我得到的最接近的

代码语言:javascript
运行
复制
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;
    }
  }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-23 21:18:36

您可以先按score分组,然后添加每个组的位置。

代码语言:javascript
运行
复制
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);
代码语言:javascript
运行
复制
.as-console-wrapper { max-height: 100% !important; top: 0; }

票数 4
EN

Stack Overflow用户

发布于 2021-08-23 21:15:47

假设data中的项按分数排序,即将范围压缩为每个值的一个实例,则可以执行以下操作:

  1. 使用Array#reduce对数组进行迭代,同时更新以score为键的Map,并以开始和结束positions作为值。然后,
  2. 使用Array#map再次遍历数组,对于每个项,将位置设置为保存在地图中的值:H 210G 211

代码语言:javascript
运行
复制
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);

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68898921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档