关于数组的问题,想请教一下各位彦祖们

image.png

更新:

如果是这样的格式

[
        ["一期基金", [["P"], ["B"]], [["xxx"]]],
        ["工具", [["U"], ["B"], ["U"], ["I"]]],
        ["游戏"],
        ["英雄联盟", [["I"], ["B"], ["B"]]],
        ["学习", [["I"], ["B"], ["I"], ["U"]]],
        ["一期基金", [["P"],["B"]], [["xx"]]],
]
结果为
[
        ["一期基金", [["P"], ["B"]], [["xxx"]]],
        ["游戏"],
        ["英雄联盟", [["I"], ["B"], ["B"]]],
        ["工具学习", [["B"], ["I"], ["U"]]],
        ["一期基金", [["P"],["B"]], [["xx"]]],
]

先去重,去重以后比较,如果相同就合并,其余的就不用改动了,如果第一个字母为T或者P的话即使相同也不合并,请问这个该如何去写?

阅读 1.9k
2 个回答

确实不太明确,瞎写一个看看对不对。
这里假定其中数据都是[["U"],["B"],["U"],["I"]]这种单字符形式。如果还有其他复杂的形式另说。思路是去重、flat,然后排序格式化为字符串,放到Map里比较,相同就合并。看楼主输出最后变成了B I U猜测也是经过了排序的。

PS:按评论要求改了下

const data = [
    ["工具",[["U"],["B"],["U"],["I"]]],
    ["游戏"],
    ["英雄联盟",[["B"]]],
    ["学习",[["I"],["B"],["I"],["U"]]]
]

let dict = new Map;
data.forEach(v => { 
    //如果v[1]为空就默认为空字符串
    let tmp = v.length == 1 ? "" : Array.from(new Set(v[1].flat())).sort().toString(); 
    dict.set(tmp, dict.has(tmp) ? dict.get(tmp)+v[0] : v[0]);
});

let ans = [];
dict.forEach((v,k) => {
    let tmp = [v];
    if (k !== "") tmp.push(k.split(",").map(v => [v]));
    ans.push(tmp);
})

console.log(ans);
const arr = [
  ["工具", [["a"], ["b"], ["c"]]],
  ["学习", [["c"], ["a"], ["c"]]],
];
const needArr = arr.reduce((init, current, i) => {
  if (i === 0) {
    init = current;
    return init;
  }
  current.forEach((m, index) => {
    if (index === 0) {
      init[index] += m;
    } else {
      init[index] = Array.from(new Set(init[index].concat(m).flat()));
    }
  });
  return init;
}, []);
console.log(needArr);

感觉问得有点不明确,不过也没有必要用reduce,只是第一感觉好像用这个比较好解决一点,如果需要有其他要求拓展也方便,就试着写了一下,肯定不是最好的,希望看到的大佬能指点一下~

ps:问题是一张图片挺秀的。


const arr = [
  ["工具", [["a"], ["b"], ["c"]]],
  ["学习", [["c"], ["a"], ["c"]]],
];
const needArr = arr.reduce((init, current, index) => {
  current[1] = Array.from(new Set(current[1].flat())).sort().join("");
  if (index === 0) {
    init = current;
    return init;
  } else {
    if (init[1] === current[1]) {
      init[0] += current[0];
    } else {
      // 不相等不知道干什么
    }
    init[1] = init[1].split("").map((m) => [m]);
  }
  return init;
}, []);
console.log(needArr);

如果需要原样返回的话我觉得就没有必要使用reduce了

const arr = [
  ["工具", [["U"], ["B"], ["U"], ["I"]]],
  // ["游戏"],
  // ["英雄联盟", [["B"]]],
  ["英雄联盟", [["U"], ["B"], ["U"], ["I"]]],
  ["学习", [["I"], ["B"], ["I"], ["U"]]],
];

const sortArr = arr.filter((m) => {
  if (m.length <= 1 || Array.isArray(m[0])) return false;
  m[1] = Array.from(new Set(m[1].flat())).sort().join("");
  return m;
});

const needArr = [""];
if (sortArr.length !== arr.length) {
  // 不满足条件,直接结束,使用原arr数组
} else {
  for (let i = 0; i < sortArr.length - 1; i++) {
    const item = sortArr[i];
    if (item[1] === sortArr[i + 1][1]) {
      if (needArr.length === 1) needArr.push(item[1]);
      needArr[0] += item[0];
      if (i === sortArr.length - 2) needArr[0] += sortArr[i + 1][0];
    } else {
      // 不满足条件,直接结束,使用原arr数组
      return false;
    }
  }
  needArr[1] = needArr[1].split("").map((m) => [m]);
}

console.log(needArr);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题