将 2 个数组组合成一个新的字符串
Combine 2 Array into a new string
我有 2 个 Javascripts 数组
var array1 = ["Mergen Morry","Dash Borad","Mergen Xss"];
var array2 = ["02111356","4458763","02111356"];
我想要这样的输出
Mergen Morry – 02111356 : data uploaded
Dash Borad - 4458763 : data uploaded
Mergen Xss – 02111356 : id already registered
我在考虑使用 join(),但我记得这是 2 个数组,使用 concat() 是不可能的,也许在考虑使用 map() 或 forEach(),但我还是不不知道怎么弄。
编辑:对不起,输出应该是字符串而不是数组。
您可以使用对象来保持可见 id
和 return 映射字符串的适当注释。
const
names = ["Mergen Morry", "Dash Borad", "Mergen Xss"],
ids = ["02111356", "4458763", "02111356"],
result = names.map(
(seen => (s, i) => `${s} - ${ids[i]} : ${ seen[ids[i]]
? 'id already registered'
: (seen[ids[i]] = true, 'data uploaded') }`)
({})
).join('\n');
console.log(result);
@Nina Scholz 的回答非常好,这里还有另一种方法,您可以使用 json 及其所有内容。
var array1 = ["Mergen Morry","Dash Borad","Mergen Xss"];
var array2 = ["02111356","4458763","02111356"];
var res = array1.map((item,index)=>{
return {name:item,id:array2[index]}
},[]);
// Now that you have an array with Id and namne, you could loop throw each array and display the data
console.log(res)
我有 2 个 Javascripts 数组
var array1 = ["Mergen Morry","Dash Borad","Mergen Xss"];
var array2 = ["02111356","4458763","02111356"];
我想要这样的输出
Mergen Morry – 02111356 : data uploaded
Dash Borad - 4458763 : data uploaded
Mergen Xss – 02111356 : id already registered
我在考虑使用 join(),但我记得这是 2 个数组,使用 concat() 是不可能的,也许在考虑使用 map() 或 forEach(),但我还是不不知道怎么弄。
编辑:对不起,输出应该是字符串而不是数组。
您可以使用对象来保持可见 id
和 return 映射字符串的适当注释。
const
names = ["Mergen Morry", "Dash Borad", "Mergen Xss"],
ids = ["02111356", "4458763", "02111356"],
result = names.map(
(seen => (s, i) => `${s} - ${ids[i]} : ${ seen[ids[i]]
? 'id already registered'
: (seen[ids[i]] = true, 'data uploaded') }`)
({})
).join('\n');
console.log(result);
@Nina Scholz 的回答非常好,这里还有另一种方法,您可以使用 json 及其所有内容。
var array1 = ["Mergen Morry","Dash Borad","Mergen Xss"];
var array2 = ["02111356","4458763","02111356"];
var res = array1.map((item,index)=>{
return {name:item,id:array2[index]}
},[]);
// Now that you have an array with Id and namne, you could loop throw each array and display the data
console.log(res)