将两个1D数组映射到2D数组中,然后填充已知值 [英] map two 1D arrays into a 2D array and then fill with known values

查看:65
本文介绍了将两个1D数组映射到2D数组中,然后填充已知值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我,只是想知道解决此问题的最佳方法.我有两个单维数组,需要将它们合并到网格中,实际上第一个数组变成行,第二个数组变成列-两者的大小都未知.

I', just wondering about the best way to approach this problem. I have two single dimension arrays which I need to merge into a grid, effectively the first array becomes rows and the second array becomes columns - both are of unknown size.

我正在考虑嵌套循环和.push,但是还有一个更优雅的解决方案.

I am thinking nested loops and .push but is there a more elegant solution.

问题的第二部分有些棘手.每个网格单元都有一个根据行和列数据分配的键.我有一个包含此键的第三个数组,作为数组索引中对象的属性,我需要将此对象注入(或链接)到2D数组中的正确单元格.通常,大约80%的网格将在第三阵列中具有关联的对象.

The second part of the problem is slightly trickier. Each of the grid cells will have a key assigned based on the row and column data. I have a third array which contains this key, as a property of the object in the array index and I need to inject (or link) this object to the correct cell in the 2D array. Typically around 80% of the grid will have an associated object in the third array.

数据集并不庞大;最大的网格只有大约400个单元,因此可以使用迭代解决方案,但感觉很脏.是否有更好的计算机科学"方法或javascript数组方法可以让我实现自己想要的目标.

The data sets are not huge; the largest grid will only be about 400 cells so an iterative solution will work but it feels dirty. Is there a better 'computer science' method or a javascript array method that will let me achieve what I want.

如果没有,请在迭代items数组与尝试在2D数组中找到正确的单元格或将2D数组迭代到并尝试在items数组中找到匹配的对象之间进行任何重大折衷,请记住,我匹配项属性,而不是关键.

If not, is there any significant tradeoff between iterating in the items array and trying to find the correct cell in the 2D array or iterating the 2D array to and try to find the matching object in the items array, bearing in mind that I am matching against the item property and not a key.

下面的示例代码

let arrRow = [
    {"code":"S", "desc":"small"},
    {"code":"M", "desc":"med"},
    {"code":"L", "desc":"large"}
];
let arrCol = [
    {"code":"R", "desc":"Red"},
    {"code":"G", "desc":"Green"},
    {"code":"B", "desc":"Blue"}
];
let arrItems= [
    {"item":"SR", "desc":"Small Red"},
    {"item":"SB", "desc":"Small Blue"},
    {"item":"MB", "desc":"Med Blue"},
    {"item":"LB", "desc":"Large Blue"},
    {"item":"SG", "desc":"Small Green"},
    {"item":"LG", "desc":"Large Green"}
 ];

根据受访者的要求.预期的结果将是

As per respondent's request. The desired outcome would be

[
  [
    {"key":"SR", "value":{"item":"SR", "desc":"Small Red"}},
    {"key":"SG", "value":{"item":"SG", "desc":"Small Green"}},
    {"key":"SB", "value":{"item":"SB", "desc":"Small Blue"}
  ],
    {"key":"MR", "value":{}},
    {"key":"MG", "value":{}},
    {"key":"MB", "value":{"item":"MB", "desc":"Med Blue"}}
  ],
    {"key":"LR", "value":{}},
    {"key":"LG", "value":{"item":"LG", "desc":"Large Green"}},
    {"key":"LB", "value":{"item":"LB", "desc":"Large Blue"}}
  ]

]

或者,该值也可以只是item数组中元素的索引,这可能会提高内存效率

Alternatively the value could just be the index of the element in the item array which is probably more memory efficient

推荐答案

您可以使用 Map 获取数据并迭代行和列.

You could take a Map for data and iterate rows and columns.

var arrRow = [{ code: "S", desc: "small" }, { code: "M", desc: "med" }, { code: "L", desc: "large" }],
    arrCol = [{ code: "R", desc: "Red" }, { code: "G", desc: "Green" }, { code: "B", desc: "Blue" }],
    arrItems = [{ item: "SR", desc: "Small Red" }, { item: "SB", desc: "Small Blue" }, { item: "MB", desc: "Med Blue" }, { item: "LB", desc: "Large Blue" }, { item: "SG", desc: "Small Green" }, { item: "LG", desc: "Large Green" }],
    map = arrItems.reduce((m, o) => m.set(o.item, o), new Map),
    result = arrRow.map(r => arrCol.map(c => (map.get(r.code + c.code) || {}).desc || ''));

console.log(result);

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

这篇关于将两个1D数组映射到2D数组中,然后填充已知值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆