![](/img/trans.png)
[英]Finding index of value in two dimensional array of objects in javascript
[英]Remove value of a combined two dimensional array by index in Javascript
这里有一个 js 新手问题。 我需要删除一个没有按索引配对或连接值的组合二维数组的值。 抱歉,我不知道这方面的正确术语。 仅以我的示例为例:
arr = [
["First Name", "Last Name", "Email", "Address", "Position", "Age", "Birthday"],
["John", "Doe", "john@doe.com", "", "", "34", ""]
];
res = arr.reduce((x, y) => x.map((v, i) => v + ': '+ y[i]));
console.log(res); //["First Name: John", "Last Name: Doe", "Email: john@doe.com", "Address: ", "Position: ", "Age: 34", "Birthday: "]
所以,我需要从数组中删除"Address: "
"Position: "
"Birthday: "
,剩下的是:
["First Name: John", "Last Name: Doe", "Email: john@doe.com", ""Age: 34"]
意思是,从另一个数组中删除那些不配对的。 希望这是有道理的,感谢您的帮助!
像这样的东西
arr = [
["First Name", "Last Name", "Email", "Address", "Position", "Age", "Birthday"],
["John", "Doe", "john@doe.com", "", "", "34", ""]
];
res = arr
.reduce((x, y) => x
.map((v, i) => { if (y[i]) return v + ': '+ y[i]}))
.filter(Boolean);
console.log(res);
您可以使用函数Array.prototype.reduce
并在空白空间(这是假的)上应用强制以跳过这些值。
const arr = [ ["First Name", "Last Name", "Email", "Address", "Position", "Age", "Birthday"], ["John", "Doe", "john@doe.com", "", "", "34", ""]], [properties, values] = arr, result = values.reduce((a, v, i) => Boolean(v) ? a.concat([properties[i], ": ", v].join("")) : a, []); console.log(result);
arr = [ ["First Name", "Last Name", "Email", "Address", "Position", "Age", "Birthday"], ["John", "Doe", "john@doe.com", "", "", "34", ""] ]; res = arr.reduce(function(x, y){ return x.map(function(v, i) { if (!['Birthday', 'Age'].includes(v)) return v + ': '+ y[i]; }).filter(x => x !== undefined); }); console.log(res);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.