对 JavaScript 上的数组元素进行排序

Sort array elements on JavaScript

我有一个数组,每个子数组包含不同顺序的不同位置:

[
  ["apple(2)", "banana(5)"],
  ["peach(3)", "banana(1)"],
  ["apple(1)"]
]

我需要在 JavaScript (ES6) 上对其进行排序,我希望得到这样的数组:

[
  ["apple(2)", "banana(5)", "peach(0)"],
  ["apple(0)", "banana(1)", "peach(3)"],
  ["apple(1)", "banana(0)", "peach(0)"]
]

每个子数组的顺序应该相同。如果子数组没有某个位置,我需要用 0 值添加它。我可以使用 map()sort() 之类的功能还是需要手动比较它?

首先获取唯一词。然后遍历数组数组以检查单词是否存在。如果不存在,则根据您的条件创建单词,如果存在,则将原始单词放入 tmp 数组。最后对每次迭代进行排序。顺便说一句,我使用正则表达式替换方法来获取单词。

const data = [
  ['apple(2)', 'banana(5)'],
  ['peach(3)', 'banana(1)'],
  ['apple(1)'],
];
const words = [...new Set(data.flat().map((x) => x.replace(/[^a-z]/gi, '')))];
const ret = data.map((x) => {
  const tmp = [];
  const newX = x.map((y) => y.replace(/[^a-z]/gi, ''));
  for (let i = 0, l = words.length; i < l; i += 1) {
    if (newX.includes(words[i])) tmp.push(x.shift());
    else tmp.push(`${words[i]}(0)`);
  }
  return tmp.sort();
});
console.log(ret);

您必须循环才能获得使用的密钥。然后,您必须第二次循环以填充缺失的键。有很多种方法,这是一种。

var data = [
  ["apple(2)", "banana(5)"],
  ["peach(3)", "banana(1)"],
  ["apple(1)"]
];

// match string and number
var re = /([^(]+)\((\d+)\)/;

// Loop over and find all of the keys
var grouped = data.reduce((info, subset, index) => {
  subset.forEach(item => {
    // find the key and count
    var parts = item.match(re);
    // have we seen this key?
    if (!info[parts[1]]) {
      // if not create an array
      info[parts[1]] = Array(data.length).fill(0);
    }
    // set the key index with the count
    info[parts[1]][index] = parts[2];
  })
  return info;
}, {});

// loop over the groups and fill in the set
Object.entries(grouped).forEach(([key, counts], colIndex) => {
  counts
    .forEach((cnt, rowIndex) => {
      data[rowIndex][colIndex] = `${key}(${cnt})`;
    })
});

console.log(data);

这里是函数式编程方法,使用 Mapreduce:

const data = [['apple(2)', 'banana(5)'],['peach(3)', 'banana(1)'],['apple(1)'],];

// Create a Map with default values for each name, i.e. with "(0)":
let names = new Map(data.flat().map(item => [item.replace(/\d+/, ""), item.replace(/\d+/, "0")]));
let result = data.map(row =>
    [...row.reduce((map, item) => 
        map.set(item.replace(/\d+/, ""), item), // Overwrite default
        new Map(names) // Start with clone of original Map
    ).values()]
);
console.log(result);