Javascript 中的数组转换

array of arrays conversion in Javascript

我需要创建一个数组数组。 值得注意的是,数据库非常大,如果任何属性没有对应的值,它就会发送一个空字符串。我试过 map 和 reduce 但我没有成功:

任何帮助将不胜感激。

下面我展示了一个预期输出的例子:

outputExpected = [
  ["id", 1, 2],
  ["name", "name1", "name2"],
  ["price", 6.95, 998.95],
  ["promoPrice", 5.91, 333.91],
  ["category", "test1 | test2", "test3 | test4"],
]

有什么方法可以有效地解决这个问题?

这是我的代码:

let arrayObj = [{
    "id": 1,
    "name": "name1",
    "price": 6.95,
    "promoPrice": 5.91,
    "category": ["test1, test2"]
  },
  {
    "id": 2,
    "name": "name2",
    "price": 998.95,
    "promoPrice": 333.91,
    "category": ["test3, test4"]
  }
]

const headers = ["id", "name", "price", "promoPrice", "category"]
const result1 = headers.concat(arrayObj.map((obj) => {
  return headers.reduce((arr, key) => {
    arr.push(obj[key]) return arr;
  }, [])
}))

console.log(result1)

将数组缩减为 Map。在每次迭代中,使用 Object.entries() 将对象转换为 [key, value] 对的数组。使用 Array.forEach() 迭代条目并将它们添加到映射中。使用 Array.from():

将 Map 的值迭代器转换为数组

const arr = [{"id":1,"name":"name1","price":6.95,"promoPrice":5.91,"category":["test1", "test2"]},{"id":2,"name":"name2","price":998.95,"promoPrice":333.91,"category":["test3", "test4"]}]

const result = Array.from(arr.reduce((acc, o) => {
  Object.entries(o)
    .forEach(([k, v]) => {
      if(!acc.has(k)) acc.set(k, [k])
      
      acc.get(k).push(Array.isArray(v) ? v.join(' | ') : v)
    })
  
  return acc
}, new Map()).values())

console.log(result)

构造一个 init 数组,其中包含所有可能的键和所需的顺序,然后根据索引为每个键使用 Array.reduceArray.forEachArray.push 值。

const arrayObj = [
  {
  "id":1,
  "name":"name1",
  "price":6.95,
  "promoPrice":5.91,
  "category":["test1", "test2"]
  },
  {
    "id":2,
    "name":"name2",
    "price":998.95,
    "promoPrice":333.91,
    "category":["test3", "test4"]
  }
]

function ConvertToArray2D (items) {
  let init = [['id'], ['name'], ['price'], ['promoPrice'], ['category']]
  if (!items) return init
  return arrayObj.reduce((pre, cur) => {
    init.forEach((key, index) => {
      pre[index].push(Array.isArray(cur[key[0]]) ? cur[key[0]].join('|') : cur[key[0]])
    })
    return pre
  }, init.slice())
}
console.log(ConvertToArray2D(arrayObj))

您可以简单地映射值并检查某项是否为数组,然后获取连接值或值本身。

const
    data = [{ id: 1, name: "name1", price: 6.95, promoPrice: 5.91, category: ["test1, test2"] }, { id: 2, name: "name2", price: 998.95, promoPrice: 333.91, category: ["test3, test4"] }],
    headers = ["id", "name", "price", "promoPrice", "category"],
    result = data
        .reduce(
            (r, o) => headers.map((k, i) => [
                ...r[i],
                Array.isArray(o[k]) ? o[k].join(' | ') : o[k]
            ]),
            headers.map(k => [k]),
        );

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

在将对象映射到与 headers 数组一致的值数组后,可以使用标准 'zip' 来处理。 (这也允许将结果回转)。

//** @see 
const zip = (...rs) => [...rs[0]].map((_, c) => rs.map((r) => r[c]));

const headers = ['id', 'name', 'price', 'promoPrice', 'category'];
const arrayObj = [{ id: 1, name: 'name1', price: 6.95, promoPrice: 5.91, category: ['test1', 'test2'] },{ id: 2, name: 'name2', price: 998.95, promoPrice: 333.91, category: ['test3', 'test4'] },];

const result = zip(
  headers,
  ...arrayObj.map((o) => headers.map(h => Array.isArray(o[h]) ? o[h].join(' | ') : o[h]))
);

console.log(result);

// which also allows it to be reversed
console.log(zip(...result));
.as-console-wrapper { max-height: 100% !important; top: 0; }

请参阅:Javascript equivalent of Python's zip function 以了解进一步的 zip 讨论。