[英]How to extract an array from an object with property and array in typescript?
[英]extract object from array - typescript
我有一趟 object 看起來像這樣:
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 采用這種形式:
trip: {
driver: {
firstName: string;
rating: number;
id: number;
};
timeOfDeparture: any;
}
因此,如果我將TripListProps
從trips: any
更改為trips: Array<Trip>
,我會收到錯誤消息。
有什么辦法可以從整個數組 object 中只提取這一部分?
你可以在lodash 中使用 _.pick _.pick
選擇那些有鑰匙的人
var object = { 'a': 1, 'b': '2', 'c': 3 };
_.pick(object, ['a', 'c']);
// => { 'a': 1, 'c': 3 }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.