javascript - 在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"]
意思是,从另一个数组中删除那些不配对的。希望这是有道理的,并感谢您的帮助!
解决方案
您可以使用该函数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);
推荐阅读
- node.js - 带有 webpack 的 Vue.js:如何修复错误 EBUSY:资源繁忙或锁定
- java - 有没有办法通过Java Springboot在mysql中设置会话特定的系统变量?
- python - 如何为基于 CLI 的应用程序编写可安装的 python 包
- ansible - ansible重新启动机器时如何向用户显示消息
- java - 如何配置 DBeaver 以显示我的架构?
- python - 对相似类型的测试进行参数化
- javascript - 访问 locaStorage 中的另一个环境
- design-patterns - 以数据库为中心的架构与疲惫的架构
- csv - gocsv UnmarshalBytes 失败
- cluster-analysis - 使用节点的属性进行聚类