查找并替换多维数组中的每个元素 [关闭]
Posted
技术标签:
【中文标题】查找并替换多维数组中的每个元素 [关闭]【英文标题】:Lookup and replace each element in a multidimensional array [closed] 【发布时间】:2019-08-02 15:10:39 【问题描述】:我有这个数组,它的第一列作为 id:
var mainarray = [
["1001","P6","P8"],
["1002","P7"],
["1003","P7","P8","P10"],
["1004","P6","P10"],
];
我想用下面“查找”数组中的相应动物替换每个“P”元素:
var arraylookup= [
["P6","dog"],
["P7","cat"],
["P8","mouse"],
["P9","horse"],
["P10","cow"],
];
...这将给我以下新数组
var newarray= [
["1001","dog","mouse"],
["1002","cat"],
["1003","cat","mouse","cow"],
["1004","dog","cow"],
];
我已经成功地把自己完全弄糊涂了。感谢您的帮助。
【问题讨论】:
arraylookup
数据格式使用起来可怕。你能修改那个对象吗?一个更简单的选项是这样的:"P6":"dog","P7":"cat"
...
哦,这可能解释了为什么我还不能得到任何工作。是的,可以根据您的建议进行修改。
我确定有原因,但我看不出反对票的意义。我正在尝试在这里学习,我认为这是 SO 的重点。我没有提交任何尝试,因为我没有任何有价值的东西可以展示。不是抱怨,只是观察。
【参考方案1】:
您可以在mainarray
上使用map 来将条目中的项目映射到它们的查找值。
要轻松查找值,请创建如下映射:
const lookup =
P6: "dog",
P7: "cat",
P8: "mouse",
P9: "horse",
P10: "cow"
;
您可以使用reduce 从您的arraylookup
数组中生成它。或者只是将arraylookup
重新格式化为代码中的对象。
您可以使用解构从数组条目中提取项目。
const arr = [
["1001","P6","P8"],
["1002","P7"],
["1003","P7","P8","P10"],
["1004","P6","P10"],
];
const arrLookup = [
["P6","dog"],
["P7","cat"],
["P8","mouse"],
["P9","horse"],
["P10","cow"],
];
const lookup = arrLookup.reduce((acc, [k, v]) => ( ...acc, [k]: v ), );
const result = arr.map(([id, ...rest]) => [id, ...rest.map(x => lookup[x])]);
console.log(result);
【讨论】:
【参考方案2】:首先您应该将arraylookup
转换为一个对象,即:
const objLookUp=
"P6": "dog"],
"P7": "cat",
...
;
const arrayLookUp= [
["P6","dog"],
["P7","cat"],
["P8","mouse"],
["P9","horse"],
["P10","cow"],
];
const objLookUp = arrayLookUp.reduce((r, [key, value]) => (r[key]= value, r), );
console.log(objLookUp);
那么您将输入数组转换为所需的格式会容易得多:
const mainArray = [
["1001","P6","P8"],
["1002","P7"],
["1003","P7","P8","P10"],
["1004","P6","P10"],
];
const arrayLookUp= [
["P6","dog"],
["P7","cat"],
["P8","mouse"],
["P9","horse"],
["P10","cow"],
];
const objLookUp = arrayLookUp.reduce((r, [key, value]) => (r[key]= value, r), );
const result = mainArray.map(
arr => arr.map(v => v in objLookUp ? objLookUp[v] : v)
);
console.log(result);
.as-console-wrapper max-height: 100% !important; top: 0;
【讨论】:
【参考方案3】:您可以像这样使用find
和map
和startsWith
:
const array = [
["1001","P6","P8"],
["1002","P7"],
["1003","P7","P8","P10"],
["1004","P6","P10"],
];
const lookup = [
["P6","dog"],
["P7","cat"],
["P8","mouse"],
["P9","horse"],
["P10","cow"],
];
const result = array.map(arr =>
let replaced = arr.map(s =>
if (s.startsWith("P"))
var newValue = lookup.find(a => a[0] == s)[1];
return newValue;
return s;
);
return replaced;
);
console.log(result);
等效于 ES5:
var array = [
["1001", "P6", "P8"],
["1002", "P7"],
["1003", "P7", "P8", "P10"],
["1004", "P6", "P10"],
];
var lookup = [
["P6", "dog"],
["P7", "cat"],
["P8", "mouse"],
["P9", "horse"],
["P10", "cow"],
];
var result = array.map(function(arr)
var replaced = arr.map(function(s)
if (s.startsWith("P"))
var newValue = lookup.find(function(a)
return a[0] == s;
)[1];
return newValue;
return s;
);
return replaced;
);
console.log(result);
【讨论】:
以上是关于查找并替换多维数组中的每个元素 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章