如何获取多个嵌套对象数组的公共值
How to get common values of multiple nested array of objects
我想从每个视图中按名称而不是按 ID 获取公共值。例如:这里 'view 6' 和 'view 2' 很常见。
-这是我的回复 (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"
}
]
}
]
-我想通过名称而不是 id 从视图中获取常见对象,所以这里 'view 6' 和 'view 2' 是常见的。
目前,我仅在它们出现不止一次时才获取值,但可以有多个对象,因此我必须在每个视图中都通用。
-预期输出:
{
"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"
}
-我试过这样做:
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);
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);
我想从每个视图中按名称而不是按 ID 获取公共值。例如:这里 'view 6' 和 'view 2' 很常见。
-这是我的回复 (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"
}
]
}
]
-我想通过名称而不是 id 从视图中获取常见对象,所以这里 'view 6' 和 'view 2' 是常见的。 目前,我仅在它们出现不止一次时才获取值,但可以有多个对象,因此我必须在每个视图中都通用。
-预期输出:
{
"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"
}
-我试过这样做:
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);
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);