如何解决比较两个数组对象以获取匹配的数据并返回新的数组对象
如何检查两个数组中的可用数据并返回新数组。例如,我想比较一个数组中的数据并检查另一个数组,如果可用,则它将返回带有count的新数组。下面是两个数组和我的预期结果代码。
const restaurant = [
{ name: 'La mesa',cuisine: ['chiness','arabic'] },{ name: 'Purnima',cuisine: ['thai'] },{ name: 'Red Bull',cuisine: ['french',{ 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',{ name: 'french',{ name: 'italian',restaurant: 0 },{ name: 'indian',{ name: 'mexican',];
谢谢
解决方法
您可以同时使用函数map
,reduce
和some
来构建所需的输出,如下所示:
const restaurant = [ { name: 'La mesa',cuisine: ['chiness','arabic'] },{ name: 'Purnima',cuisine: ['thai'] },{ name: 'Red Bull',cuisine: ['french',{ 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; }
,
通过地图和过滤器,这种方式:
document.body.innerHTML += '<ecg-line></ecg-line>';
ecgLine((bang) => setInterval(() => bang(),1000));
,
您可以绘制美食地图并过滤餐馆,以获取餐馆数量
cuisine.map((cuisineObject) => {
const numberOfRestaurants = restaurant.filter((restaurantObject) => restaurantObject.cuisine.includes(cuisineObject.name)).length
return {
...cuisineObject,restaurant: numberOfRestaurants
}
})
,
您可以使用.reduce()
构建一个存储每种美食的所有频率的对象,然后在cuisine
数组上使用.map()
,如下所示:
const restaurant = [ { name: 'La mesa',cuisine: ['indian'] },];
const cuisine = [ { name: 'chiness' },{ 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)的时间复杂度。因此,与嵌套循环相比,它可以提供更好的整体性能,并且更具可伸缩性。
使用map
,flatMap
和filter
编辑:使用flatMap
代替map.flat
cuisine.map(({name}) => ({name: name,restaurant: restaurant.flatMap(v => v.cuisine).filter(v=>v === name).length}))
,
首先,我们可以将带有美食信息的餐厅格式化为一个对象,然后使用相同的对象来找出提供特定美食的餐厅数量。可以使用Array.reduce
和Array.map
来实现。
const restaurant = [{name:'La mesa',cuisine:['chiness','arabic']},{name:'Purnima',cuisine:['thai']},{name:'Red Bull',cuisine:['french',{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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。