我有两个数组:
let names1 = ["om","karan","ranjett","rocky"] <br>
let names2 = ["vaibhu","Shrushti","rekha","kunal"]
如何遍历两个数组并生成一个新对象。从names1和names2中获取一个元素和一个随机数,并将它们压入对象
如:-
obj = {
{
n1:"om"n2:"vaibhu"randomnumber: 12},
//剩余数组
}
映射到它上面?
let names1 = ["om","karan","ranjett","rocky"]
let names2 = ["vaibhu","Shrushti","rekha","kunal"]
const combined = names1.map((item, index) => ({
n1: item,
n2: names2[index],
randomnumber: Math.ceil(Math.random() * 100)
}));
console.log(combined);
/*
[
{ n1: 'om', n2: 'vaibhu', randomnumber: 49 },
{ n1: 'karan', n2: 'Shrushti', randomnumber: 87 },
{ n1: 'ranjett', n2: 'rekha', randomnumber: 1 },
{ n1: 'rocky', n2: 'kunal', randomnumber: 74 }
]
*/