当前位置: 首页 > 知识库问答 >
问题:

javascript - 数组组装,求大佬指导?

牟恺
2023-12-27
let arrayData = [  {    name: 'k1',    percentage: '0%'  },  {    name: 'k2',    percentage: '0%'  },  {    name: 'k3',    percentage: '1%'  },  {    name: 'k4',    percentage: '2%'  },  {    name: 'k5',    percentage: '0%'  },  {    name: 'k6',    percentage: '3%'  }]

我想要的结果:相同进度的不要叠加一起,让它往前走一格,第2个0%让它往前走+1变成1%,第3个0%让它往前走2格+2变成2%,若1%,2%已存在,在前面的基础2%,加+1,变成3%,4%,以此类推!

备注:一般只有0%和100%才会出现多个相同的,最多4个相同的数据

newArrayData = [  {    name: 'k1',    percentage: '0%'  },  {    name: 'k2',    percentage: '1%'  },  {    name: 'k5',    percentage: '2%'  },  {    name: 'k3',    percentage: '3%'  },  {    name: 'k4',    percentage: '4%'  },  {    name: 'k6',    percentage: '5%'  }]

共有3个答案

康锦
2023-12-27
let arrayData = [  {    name: 'k1',    percentage: '0%',  },  {    name: 'k2',    percentage: '0%',  },  {    name: 'k3',    percentage: '1%',  },  {    name: 'k4',    percentage: '2%',  },  {    name: 'k5',    percentage: '0%',  },  {    name: 'k6',    percentage: '3%',  },];arrayData = arrayData  .sort((a, b) => {    return parseInt(a.percentage) - parseInt(b.percentage);  })  .reduce((total, current, index, arr) => {    if (index === 0) {      total.push(current);      return total;    }    const last = total[total.length - 1];    const item = { ...current };    const next = arr[index + 1];    if (last.percentage === current.percentage) {      item.percentage = `${parseInt(item.percentage) + 1}%`;      if (next && next.percentage === current.percentage) {        next.percentage = `${parseInt(item.percentage) + 1}%`;      }    }    if (parseInt(item.percentage) < parseInt(last.percentage)) {      item.percentage = `${parseInt(last.percentage) + 1}%`;    }    total.push(item);    return total;  }, []);console.log(arrayData);


郑和泰
2023-12-27
let arrayData = [  {    name: "k1",    percentage: "0%",  },  {    name: "k2",    percentage: "0%",  },  {    name: "k3",    percentage: "1%",  },  {    name: "k4",    percentage: "2%",  },  {    name: "k5",    percentage: "0%",  },  {    name: "k6",    percentage: "3%",  },  {    name: "k7",    percentage: "11%",  },  {    name: "k8",    percentage: "3%",  },  {    name: "k9",    percentage: "3%",  },  {    name: "k10",    percentage: "3%",  },];let list = [];arrayData.forEach((item) => {  let count = item.percentage.replace("%", "");  if (!list[count]) {    list[count] = count;  } else {    let newCount = list.findIndex((el) => el === undefined);    if (newCount < 0) {      newCount = list.length;    }    item.percentage = newCount + "%";    list[newCount] = newCount;  }});console.log(arrayData);

image.png

邰博远
2023-12-27
  let arrayData = [    {      name: "k1",      percentage: "0%",    },    {      name: "k2",      percentage: "0%",    },    {      name: "k3",      percentage: "1%",    },    {      name: "k4",      percentage: "2%",    },    {      name: "k5",      percentage: "0%",    },    {      name: "k6",      percentage: "3%",    },  ];  let obj = {};  // 首先处理非0%和100%的情况  arrayData.forEach((item, index) => {    if (item.percentage !== "0%" && item.percentage !== "100%") {      let count = obj[item.percentage] || 0;      obj[item.percentage] = count + 1;      let newP = parseInt(item.percentage) + count;      arrayData[index].percentage = newP + "%";    }  });  // 处理0%的情况  let count = 0;  arrayData.forEach((item, index) => {    if (item.percentage === "0%") {      arrayData[index].percentage = count + "%";      count++;    }  });  console.log(arrayData);

image.png

 类似资料: