I'm fetching data from two links, a list of coins, and a list of events. The data gets rendered in the render section.
componentDidMount() {
Promise.all([
fetch('https://coindar.org/api/v2/coins?access_token=36174:fy7i3eWjxllsdwhl80c'),
fetch('https://coindar.org/api/v2/events?access_token=36174:fy7i3eWjxllsdwhl80c&page=1&page_size=30&filter_date_start=2018-10-01&filter_date_end=2020-07-01&sort_by=views&order_by=1')])
.then(([res1, res2]) => Promise.all([res1.json(), res2.json()]))
.then(([data1, data2]) => this.setState({
events: data2,
coins: data1,
coinid: [],
isLoaded: true,
}));
}
render() {
var {isLoaded, events, coins,coinid} = this.state;
return (
<div className="App">
<ul>
{events.map(events => (
<li key={events.id} value={events.coin_id} > Name: {events.caption} Coin id: {events.coin_id} <!-- Image here.. coins.image_32 --> </li>))}
</ul>
<ul>
{coins.map(coins => (
<li key={coins.id}> Name: {coins.name} : Coin id: {coins.id}</li>))}
</ul>
</div>
);
}
}
The URLs can be copy pasted to see the data, what I'm trying to achieve is a list of events (second URL) with their images and coin name (first URL). In other languages the first step could be to use a where
clause (where events.coin_id == coins.id
) however, I'm not sure how to go about it with React.
Looks like nothing related to react, it's pure javascript implementation is enough to achieve our desire functionality.
just introduce 2 more state properties like matchEvents
and matchCoins
, apply where clause (where events.coin_id == coins.id)
I'm not sure you need to 2 filters here, just modify based on your needs.
componentDidMount() {
Promise.all([
fetch('https://coindar.org/api/v2/coins?access_token=36174:fy7i3eWjxllsdwhl80c'),
fetch('https://coindar.org/api/v2/events?access_token=36174:fy7i3eWjxllsdwhl80c&page=1&page_size=30&filter_date_start=2018-10-01&filter_date_end=2020-07-01&sort_by=views&order_by=1')])
.then(([res1, res2]) => Promise.all([res1.json(), res2.json()]))
.then(([data1, data2]) => this.setState({
events: data2,
coins: data1,
matchEvents: data2.filter(event => {
return data1.some(coin => events.coin_id == coin.id)
}),
matchCoins: data1.filter(coin => {
return data2.some(event => events.coin_id == coin.id)
}),
coinid: [],
isLoaded: true,
}));
}
render() {
var { isLoaded, events, coins, coinid, matchEvents, matchCoins } = this.state;
return (
<div className="App">
<ul>
{matchEvents.map(events => (
<li key={events.id} value={events.coin_id} > Name: {events.caption} Coin id: {events.coin_id} <!-- Image here.. coins.image_32 --> </li>))}
</ul>
<ul>
{matchCoins.map(coins => (
<li key={coins.id}> Name: {coins.name} : Coin id: {coins.id}</li>))}
</ul>
</div>
);
}