简体   繁体   English

在 Javascript 中创建一个空的二维数组,带有键

[英]Create an empty two dimensional array in Javascript, with keys

I'm trying to create a matrix in Javascript, but one which has predefined keys for the X and Y axis.我正在尝试在 Javascript 中创建一个矩阵,但它具有预定义的 X 和 Y 轴键。

For example I have the following keys例如我有以下键

const x = [2,4,6,8]
const y = [10,20,40,60,80]

I found the following snippet which creates an empty two dimensional array with 4 rows and 5 columns我找到了以下代码片段,它创建了一个 4 行 5 列的空二维数组

[...Array(4)].map(x=>Array(5).fill(0))       

I'm wondering if it's possible to create these arrays (objects), but using provided keys to go with it.我想知道是否可以创建这些 arrays (对象),但使用提供的 go 密钥。

So the end result would look like this.所以最终的结果应该是这样的。

{
    2 => {10 => 0, 20 => 0, 40 => 0, 60 => 0, 80 => 0}
    4 => {10 => 0, 20 => 0, 40 => 0, 60 => 0, 80 => 0}
    6 => {10 => 0, 20 => 0, 40 => 0, 60 => 0, 80 => 0}
    8 => {10 => 0, 20 => 0, 40 => 0, 60 => 0, 80 => 0}
}

You could create the wanted objects by looping and reducing the data.您可以通过循环和减少数据来创建想要的对象。

The callback of Array#reduce takes as first parameter the accumulator , here it is an object, and as second parameter the value of the iterating array. Array#reduce的回调将accumulator作为第一个参数,这里是一个 object,第二个参数是迭代数组的值。

As startValue for the reduce , it take an (kind of empty) object and usdes this object to add properties.作为reducestartValue ,它需要一个(一种空的) object 并使用这个 object 来添加属性。 To have this object (aka accumulator) ready for the next loop (and final result), it has to be returned.为了让这个 object(又名累加器)为下一个循环(和最终结果)做好准备,它必须被返回。

 var x = [2, 4, 6, 8], y = [10, 20, 40, 60, 80], result = x.reduce((r, k) => { r[k] = y.reduce((q, l) => { q[l] = 0; return q; }, {}); return r; }, {}); console.log(result);

You could map the y array to get the entries for a row object.您可以map y数组来获取row object 的条目。 Use Object.fromEntries() to get an object from the entries.使用Object.fromEntries()从条目中获取 object。 Then map the x array to get the output object with a copy of each row object as value然后map x数组得到 output object 与每一row的副本 ZA8CFDE6331BD54B66AC96F8911

 const x = [2, 4, 6, 8], y = [10, 20, 40, 60, 80], row = Object.fromEntries( y.map(v => [v, 0]) ), output = Object.fromEntries( x.map(key => [key, {...row }]) ) console.log(output)

Cloning is required because modifying one of the rows will update the other values since they are all pointing to the same reference需要克隆,因为修改其中一行将更新其他值,因为它们都指向同一个引用

You can try following.您可以尝试以下。

  • Since you have same object repeated as child object, create it first.由于您将相同的 object 重复为子 object,因此请先创建它。
  • Now loop over parent keys and using spread or Object.assign , set it as object.现在遍历父键并使用 spread 或Object.assign ,将其设置为 object。

This way you have less number of iteration and code looks clean这样你的迭代次数更少,代码看起来很干净

 const x = [2,4,6,8] const y = [10,20,40,60,80] const innerObj = y.reduce((acc, item) => { acc[ item ] = 0; return acc; }, {}); const result = x.reduce((acc, item) => { acc[ item ] = {...innerObj }; return acc; }, {}); console.log(result)

You could use forEach loop:您可以使用 forEach 循环:

 const x = [2, 4, 6, 8] const y = [10, 20, 40, 60, 80]; let results = {}; x.forEach(k => { let inner = {}; y.forEach(v => inner[v] = 0); results[k] = inner; }); console.log(results);

x.reduce((a,v)=>Object.assign(a, {[v]: Object.fromEntries(y.map(v=>[v,0]))}), {});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM