'How to get common values of multiple nested array of objects
i want to get common values by name not by id, from every view. for eg: here 'view 6' and 'view 2' is common.
-here is my response (allDeviceView):
[
{
"ip": "111.11.11.11",
"views": [
{
"id": 3,
"name": "View 2",
"thumbnail": "http://media.fanconnect.tv/production/app/635_879656_thumbnail.jpg"
},
{
"id": 4,
"name": "View 3",
"thumbnail": "http://media.fanconnect.tv/production/app/635_879656_thumbnail.jpg"
},
{
"id": 5,
"name": "View 4",
"thumbnail": "http://media.fanconnect.tv/production/app/635_879656_thumbnail.jpg"
},
{
"id": 6,
"name": "View 5",
"thumbnail": "http://media.fanconnect.tv/production/app/635_879656_thumbnail.jpg"
},
{
"id": 7,
"name": "View 6",
"thumbnail": "http://media.fanconnect.tv/production/app/635_879656_thumbnail.jpg"
}
]
},
{
"ip": "222.22.22.22",
"views": [
{
"id": 2,
"name": "View 1",
"thumbnail": "http://media.fanconnect.tv/production/app/635_879656_thumbnail.jpg"
},
{
"id": 3,
"name": "View 2",
"thumbnail": "http://media.fanconnect.tv/production/app/635_879656_thumbnail.jpg"
},
{
"id": 7,
"name": "View 6",
"thumbnail": "http://media.fanconnect.tv/production/app/635_879656_thumbnail.jpg"
}
]
},
{
"ip": "144.44.44.44",
"views": [
{
"id": 3,
"name": "View 2",
"thumbnail": "http://media.fanconnect.tv/production/app/635_879656_thumbnail.jpg"
},
{
"id": 7,
"name": "View 6",
"thumbnail": "http://media.fanconnect.tv/production/app/635_879656_thumbnail.jpg"
}
]
}
]
-i want to get the common objects from views by name not by id, so here 'view 6' and 'view 2' is common. currently i am getting values only if they are occurring more than one time, but there can be multiple objects so i have to get common in each of views.
-expected output:
{
"id": 3,
"name": "View 2",
"thumbnail": "http://media.fanconnect.tv/production/app/635_879656_thumbnail.jpg"
},
{
"id": 7,
"name": "View 6",
"thumbnail": "http://media.fanconnect.tv/production/app/635_879656_thumbnail.jpg"
}
-i tried doing it by :
const deviceViewsMaps = allDeviceView?.map((d: any) => d.views).flat();
const flattened = [...deviceViewsMaps];
/*eslint-disable */
const counts = flattened.reduce(
(map, { name }) => map.set(name, (map.get(name) || 0) + 1),
new Map(),
);
/*eslint-enable */
const names: any = [];
const found = flattened.filter(({ name }) => {
if (counts.get(name) > 1 && !names.includes(name)) {
names.push(name);
return true;
}
return false;
});
console.log(found, 'data');
setFilteredDeviceView(found);
Solution 1:[1]
const deviceViewsMaps = allDeviceView?.map((d: any) => d.views || '[]').flat();
const deviceNameMap = allDeviceView?.map((d: any) => d.ip);
const flattened = [...deviceViewsMaps];
/*eslint-disable */
const counts = flattened.reduce(
(map, { name }) => map.set(name, (map.get(name) || 0) + 1),
new Map(),
);
/*eslint-enable */
const names: any = [];
const found = flattened.filter(({ name }) => {
//---------------------
//here i have increased count with number of ip length. i.e : deviceNameMap.length - 1
//---------------------
if (counts.get(name) > deviceNameMap.length - 1 && !names.includes(name)) {
names.push(name);
return true;
}
return false;
});
setFilteredDeviceView(found);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | priya pathak |
