reactjs - 如何使用打字稿将数组中的对象推入一个对象数组并做出反应?
问题描述
我有如下数据,
const items = [
{
id: '1',
color: 'green',
name: 'item1',
polygons: [
{
id: '1',
coordinates: [
{
latitude: '25.00',
longitude: '-25.99',
}
{
latitude: '15.00',
longitude: '-25.99',
}
{
latitude: '25.00',
longitude: '-35.99',
}
],
}
]
subItems: [
{
id: '1',
name: 'subitem-1',
color: 'green',
polygons: [
{
id: '2',
coordinates: [
{
latitude: '25.00',
longitude: '-25.99',
}
{
latitude: '15.00',
longitude: '-25.99',
}
{
latitude: '25.00',
longitude: '-35.99',
}
],
}
]
}
],
},
{
id: '2',
color: 'red',
name: 'item2',
polygons: [
{
id: '3',
coordinates: [
{
latitude: '25.00',
longitude: '-25.99',
}
{
latitude: '15.00',
longitude: '-25.99',
}
{
latitude: '25.00',
longitude: '-35.99',
}
],
}
]
subItems: [
{
id: '2',
name: 'subitem-1',
color: 'red',
polygons: [
{
id: '5',
coordinates: [
{
latitude: '25.00',
longitude: '-25.99',
}
{
latitude: '15.00',
longitude: '-25.99',
}
{
latitude: '25.00',
longitude: '-35.99',
}
],
}
]
}
],
}
]
现在我想获取 Item 和 subitem 的多边形以及 Item 和 subItem 的颜色,并将其放入如下数组中,
const polygons = [
{
id: '1',
color: 'green', //this comes from item.color
coordinates: [
{
latitude: '25.00',
longitude: '-25.99',
}
{
latitude: '15.00',
longitude: '-25.99',
}
{
latitude: '25.00',
longitude: '-35.99',
}
],
},
{
id: '2',
color: 'green', //this comes from subItems color
coordinates: [
{
latitude: '25.00',
longitude: '-25.99',
}
{
latitude: '15.00',
longitude: '-25.99',
}
{
latitude: '25.00',
longitude: '-35.99',
}
],
},
{
id: '3',
color: 'red', //this comes from Item color
coordinates: [
{
latitude: '25.00',
longitude: '-25.99',
}
{
latitude: '15.00',
longitude: '-25.99',
}
{
latitude: '25.00',
longitude: '-35.99',
}
],
},
{
id: '4',
color: 'red', //this comes from subItems color
coordinates: [
{
latitude: '25.00',
longitude: '-25.99',
}
{
latitude: '15.00',
longitude: '-25.99',
}
{
latitude: '25.00',
longitude: '-35.99',
}
],
},
]
现在的问题是如何将 Item 和 subItems 中的多边形放入对象数组中。
我已经尝试过类似下面的方法,它只会放置 Item 中的多边形。
const polygons = React.useMemo(() => {
return Items.reduce((acc: Something[], Item) => {
(Item.polygons || []).forEach(polygon => {
acc.push({ ...polygon, color: Item.color });
});
return acc;
}, []);
}, [Items]);
这里Something[]的类型如下
export interface Polygon {
id: string;
coordinate: Coordinate[];
item: Item;
}
export interface Something extends Polygon {
color: string;
}
上面的代码只从 Item 中获取多边形,但也应该从 Item 中的每个 subItem 中获取多边形。
我如何使用打字稿来做到这一点并做出反应。有人可以帮我解决这个问题。谢谢。
解决方案
嗨,您必须将项目映射并推送到新数组, 这是一个工作示例
这是代码,
let polygons = [];
items.map(i =>{
polygons.push(
{
id:i.id,
color: i.color,
coordinates: i.polygons[0].coordinates
}
)
polygons.push(
{
id:i.subItems[0].id,
color: i.subItems[0].color,
coordinates: i.subItems[0].polygons[0].coordinates
}
)
});
推荐阅读
- arrays - 在 MATLAB 中有效地保存超过 2GB 的结构数组
- javascript - 未读取 Javascript 对象
- scala - Spark - 拖动前一天的值
- scala - spark 构建路径与不兼容版本的 Scala (2.11.0) 交叉编译
- python - Keras 网络预测错误,但准确度高
- sql - 在 postgres/oracle 中使用 OFFSET 和 fetch 子句的 sql 查询行为并删除记录
- android - 将 SQLite 中的 textview 值格式化为小数点后 2 位
- deep-learning - 为什么词级语言模型应该有助于 ASR 中的波束搜索解码?
- php - 如何在 Google 日历活动中更新与会者列表?
- javascript - 我应该将提供程序 redux 放在哪里?