按 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);
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);
您可以使用函数 Array.prototype.reduce
并对空的 space(这是虚假的)应用强制以跳过这些值。
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);
一个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);
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);
您可以使用函数 Array.prototype.reduce
并对空的 space(这是虚假的)应用强制以跳过这些值。
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);