我有2个结构类似的数组,并希望使用较小数组中的随机ID遍历较大的数组并为其分配属性
//Actual array length is 140
const users = [
{
name: "John Roberts",
uid: "49ikds_dm3idmssmmi9sz"
},
{
name: "Peter Jones",
uid: "fmi33_sm39imsz9z9nb"
}
]
//Actual array lenth is 424
const cars = [
{
manufacturer: "BMW",
model: "320d",
year: "2010",
user: null
},
{
manufacturer: "BMW",
model: "530d",
year: "2018",
user: null
},
{
manufacturer: "AUDI",
model: "RS6",
year: "2014",
user: null
}
]
for(let i = 0; i < cars.length; i++){
//if index is 2 or greater in this example. users[2] will be undefined
cars[i].user = users[i].uid;
}
我基本上想重新使用小的users
数组。在上面的示例中,一旦变量i
为2或更大,则users[2]
将为undefined
。
任何人都可以推荐一种优雅的解决方案来帮助我解决这个问题。
答案 0 :(得分:3)
您可以使用%
来始终拥有正确的索引并重复使用用户。
const users = [
{
name: 'John Roberts',
uid: '49ikds_dm3idmssmmi9sz',
},
{
name: 'Peter Jones',
uid: 'fmi33_sm39imsz9z9nb',
}
];
const cars = [
{
manufacturer: 'BMW',
model: '320d',
year: '2010',
user: null,
},
{
manufacturer: 'BMW',
model: '530d',
year: '2018',
user: null,
},
{
manufacturer: 'AUDI',
model: 'RS6',
year: '2014',
user: null,
},
];
for (let i = 0; i < cars.length; i += 1){
cars[i].user = users[i % users.length].uid;
}
console.log(cars);
您不会相信的ES6解决方案! (点击诱饵)
const users = [
{
name: 'John Roberts',
uid: '49ikds_dm3idmssmmi9sz',
},
{
name: 'Peter Jones',
uid: 'fmi33_sm39imsz9z9nb',
}
];
const cars = [
{
manufacturer: 'BMW',
model: '320d',
year: '2010',
user: null,
},
{
manufacturer: 'BMW',
model: '530d',
year: '2018',
user: null,
},
{
manufacturer: 'AUDI',
model: 'RS6',
year: '2014',
user: null,
},
];
const ret = cars.map((x, xi) => ({
...x,
user: users[xi % users.length].uid,
}));
console.log(ret);
答案 1 :(得分:1)
您只需要测试user[i]
var并输入默认值即可:
for(let i = 0; i < cars.length; i++){
//if index is 2 or greater in this example. users[2] will be undefined
cars[i].user = users[i] && users[i].uid || /*Default value:*/ null;
}
如果满足以下条件,您也可以使用简单的方法实现
:for(let i = 0; i < cars.length; i++){
//if index is 2 or greater in this example. users[2] will be undefined
if (users[i]) {
cars[i].user = users[i].uid;
} else {
cars[i].user = null; // Or another default
}
}
------------------更新以考虑评论-----------------
如果要在用户上循环并在完成后重新启动数组,可以使用模%
let user_idx = null
for(let i = 0; i < cars.length; i++){
user_idx = i % users.length
//if index is 2 or greater in this example. users[2] will be undefined
cars[i].user = users[user_idx].uid || null;
}
答案 2 :(得分:0)
选项1:
for(let i = 0; i < cars.length; i++){
cars[i].user = users[i % users.length].uid;
}
选项2:
for(let i = 0; i < cars.length; i++){
cars[i].user = users[Math.round(Math.random() * (users.length - 1))].uid;
}
答案 3 :(得分:0)
这很简单。只需编写以下逻辑即可。
$.grep(users, function(user, i ) { cars[i].user = user.uid;});
答案 4 :(得分:0)
var users = [
{
name: "John Roberts",
uid: "49ikds_dm3idmssmmi9sz"
},
{
name: "Peter Jones",
uid: "fmi33_sm39imsz9z9nb"
}
]
//Actual array lenth is 424
var cars = [
{
manufacturer: "BMW",
model: "320d",
year: "2010",
user: null
},
{
manufacturer: "BMW",
model: "530d",
year: "2018",
user: null
},
{
manufacturer: "AUDI",
model: "RS6",
year: "2014",
user: null
}
]
cars.map(function(car){
var randomIndex = Math.floor(Math.random() * Math.floor(users.length))
car.user = users[randomIndex].uid;
})
console.log(cars)