如何检查两个数组中的可用数据并返回新数组。例如,我想比较一个数组中的数据并检查另一个数组,如果可用,则它将返回带有count的新数组。下面是两个数组和我的预期结果代码。
const restaurant = [
{ name: 'La mesa', cuisine: ['chiness', 'arabic'] },
{ name: 'Purnima', cuisine: ['thai'] },
{ name: 'Red Bull', cuisine: ['french', 'arabic'] },
{ name: 'Pasta', cuisine: ['indian'] },
];
const cuisine = [
{ name: 'chiness' },
{ name: 'arabic' },
{ name: 'thai' },
{ name: 'french' },
{ name: 'italian' },
{ name: 'indian' },
{ name: 'mexican' },
];
// Expected Output a new array like this below
const myCuisine = [
{ name: 'chiness', restaurant: 1 },
{ name: 'arabic', restaurant: 2 },
{ name: 'thai', restaurant: 1 },
{ name: 'french', restaurant: 1 },
{ name: 'italian', restaurant: 0 },
{ name: 'indian', restaurant: 1 },
{ name: 'mexican', restaurant: 0 },
];
谢谢
答案 0 :(得分:2)
您可以同时使用函数map
,reduce
和some
来构建所需的输出,如下所示:
const restaurant = [ { name: 'La mesa', cuisine: ['chiness', 'arabic'] }, { name: 'Purnima', cuisine: ['thai'] }, { name: 'Red Bull', cuisine: ['french', 'arabic'] }, { name: 'Pasta', cuisine: ['indian'] }],
cuisine = [ { name: 'chiness' }, { name: 'arabic' }, { name: 'thai' }, { name: 'french' }, { name: 'italian' }, { name: 'indian' }, { name: 'mexican' }],
myCuisine = cuisine.map(({name}) => ({name, restaurant: restaurant.reduce((r, {cuisine}) => r + cuisine.some(c => c === name) , 0)}));
console.log(myCuisine)
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:1)
通过地图和过滤器,这种方式:
document.body.innerHTML += '<ecg-line></ecg-line>';
ecgLine((bang) => setInterval(() => bang(), 1000));
答案 2 :(得分:1)
您可以绘制美食地图并过滤餐馆,以获取餐馆数量
cuisine.map((cuisineObject) => {
const numberOfRestaurants = restaurant.filter((restaurantObject) => restaurantObject.cuisine.includes(cuisineObject.name)).length
return {
...cuisineObject,
restaurant: numberOfRestaurants
}
})
答案 3 :(得分:0)
您可以使用.reduce()
构建一个存储每种美食的所有频率的对象,然后在cuisine
数组上使用.map()
,如下所示:
const restaurant = [ { name: 'La mesa', cuisine: ['chiness', 'arabic'] }, { name: 'Purnima', cuisine: ['thai'] }, { name: 'Red Bull', cuisine: ['french', 'arabic'] }, { name: 'Pasta', cuisine: ['indian'] }, ];
const cuisine = [ { name: 'chiness' }, { name: 'arabic' }, { name: 'thai' }, { name: 'french' }, { name: 'italian' }, { name: 'indian' }, { name: 'mexican' }, ];
const cusineFreq = restaurant.reduce((o, {cuisine}) => {
cuisine.forEach(type => o[type] = (o[type] || 0) + 1);
return o;
}, {});
const res = cuisine.map(o => ({...o, restaurant: (cusineFreq[o.name] || 0)}));
console.log(res);
如果restaurant
很大,这种创建对象进行查找的方法特别有用,因为它允许O(n + k)而不是O(n * k)的时间复杂度。因此,与嵌套循环相比,它可以提供更好的整体性能,并且更具可伸缩性。
答案 4 :(得分:0)
使用map
,flatMap
和filter
编辑:使用flatMap
代替map.flat
cuisine.map(({name}) => ({name: name,restaurant: restaurant.flatMap(v => v.cuisine).filter(v=>v === name).length}))
答案 5 :(得分:0)
首先,我们可以将带有美食信息的餐厅格式化为一个对象,然后使用相同的对象来找出提供特定美食的餐厅数量。可以使用Array.reduce
和Array.map
来实现。
const restaurant = [{name:'La mesa',cuisine:['chiness','arabic']},{name:'Purnima',cuisine:['thai']},{name:'Red Bull',cuisine:['french','arabic']},{name:'Pasta',cuisine:['indian']}];
const cuisine = [{name:'chiness'},{name:'arabic'},{name:'thai'},{name:'french'},{name:'italian'},{name:'indian'},{name:'mexican'}];
const getFormattedList = (cuisines, restaurants) => {
return cuisines.map(cuisine => {
return {
...cuisine,
restaurant: restaurants[cuisine.name] || 0
}
})
}
const formatRestaurantCuisines = (restaurants) => {
return restaurants.reduce((result, restaurant) => {
restaurant.cuisine.forEach(cuisine => {
result[cuisine] = (result[cuisine]||0) + 1;
})
return result;
}, {});
}
//Formatted object to convert the restaurant with cuisine info to count
const formattedObj = formatRestaurantCuisines(restaurant);
console.log(formattedObj);
console.log(getFormattedList(cuisine, formattedObj))
.as-console-wrapper {
max-height: 100% !important;
}