[英]Compare two array object for matched data and return new array object
How can i check the available data from two array and return new array.如何检查两个数组中的可用数据并返回新数组。 For example i want to compare data from one array and check with another array and if it available then it will return with new array with count .
例如,我想比较一个数组中的数据并检查另一个数组,如果它可用,那么它将返回带有 count 的新数组。 Below is the two array and my expected result code.
下面是两个数组和我的预期结果代码。
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 },
];
Thank you谢谢
You can use the functions map
, reduce
, and some
all together to build the desired output as follow:您可以同时使用
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; }
With map and filter, this way:使用地图和过滤器,这样:
const myCuisine = cuisine.map(
item => {
return {
...item,
restaurant: restaurant.filter(
res => res.cuisine.indexOf(item.name) >= 0
).length
}
}
);
You can map the cuisines and filter the restaurants to get the number of restaurants您可以映射美食并过滤餐厅以获取餐厅数量
cuisine.map((cuisineObject) => {
const numberOfRestaurants = restaurant.filter((restaurantObject) => restaurantObject.cuisine.includes(cuisineObject.name)).length
return {
...cuisineObject,
restaurant: numberOfRestaurants
}
})
You can build an object which stores all the frequencies of each cuisine with .reduce()
and then use .map()
on your cuisine
array like so:您可以构建一个对象,使用
.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);
This approach of creating an object for look-up is particularly useful if restaurant
is large, as it allows for a time complexity of O(n + k) rather than O(n*k).如果
restaurant
很大,这种创建用于查找的对象的方法特别有用,因为它允许 O(n + k) 而不是 O(n*k) 的时间复杂度。 Thus, it will allow for better overall performance compared to nested loops and is more scalable.因此,与嵌套循环相比,它将允许更好的整体性能并且更具可扩展性。
Using map
, flatMap
and filter
使用
map
、 flatMap
和filter
Edited: using flatMap
instead of map.flat
编辑:使用
flatMap
而不是map.flat
cuisine.map(({name}) => ({name: name,restaurant: restaurant.flatMap(v => v.cuisine).filter(v=>v === name).length}))
First we can format the restaurants with cuisine information into an object and then use the same object for finding out the number of restaurants serving the particular cuisine.首先,我们可以将带有美食信息的餐厅格式化为一个对象,然后使用相同的对象来找出提供特定美食的餐厅数量。 This can be achieved using
Array.reduce
and Array.map
.这可以使用
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; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.