通过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",""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",""]],[properties,values] = arr,result = values.reduce((a,v,i) => Boolean(v) ? a.concat([properties[i],": ",v].join("")) : a,[]);
      
console.log(result);

,

arr = [
    ["First Name",""]
];
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);

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...