我有一个旅行对象,看起来像这样:
Array (1)
0 {id: 1, vehicle: {freeSeats: 2, __typename: "Vehicle"}, startLocation: "{\"type\":\"Point\",\"coordinates\":[8.217462,53.13975]}", endLocation: "{\"type\":\"Point\",\"coordinates\":[8.258844,53.119525]}", timeOfDeparture: "2020-06-16T11:48:00.869Z", …}
type TripListProps = {
trips: any; //TODO FIX
//trips: Array<Trip>,
friendIds: Array<number>,
};
export const TripList: React.FunctionComponent<TripListProps> = ({ trips, friendIds }) => {
console.log('trips', trips);
if (trips.length > 0) {
return (
<View style={{ height: 500 }}>
<FlatList
data={trips}
horizontal={false}
scrollEnabled
renderItem={({ item }) => <TripContainer trip={item} friendIds={friendIds} />}
keyExtractor={(trip: any) => trip.id.toString()}
/>
</View>
)
} else {
return (<View style={styles.noTripsFound}>
<Text style={styles.text}>No trips found</Text></View>);
}
};
其原始类型为Array<Trip>
。但是,在这里,我将其传递给另一个组件TripContainer
,该组件需要使用以下形式的行程:
trip: {
driver: {
firstName: string;
rating: number;
id: number;
};
timeOfDeparture: any;
}
因此,如果我将TripListProps
从trips: any
更改为trips: Array<Trip>
,则会收到错误消息。
有什么方法可以从整个数组对象中仅提取这部分吗?
答案 0 :(得分:0)
您可以在lodash中使用_.pick
来仅选择有钥匙的钥匙
var object = { 'a': 1, 'b': '2', 'c': 3 };
_.pick(object, ['a', 'c']);
// => { 'a': 1, 'c': 3 }