2

how can i map over 2 Array of object in React and render to one Table. what are option for this issue. should i merge these two array into one ? Please check Json Data

let URL1 = "http://api_url/users"
let URL2 = "http://api_url/users-card"

const promise1 = axios.post(URL1, inputValue , {headers: {'Content-Type': 'text/plain'}});
const promise2 = axios.post(URL2, inputValue , {headers: {'Content-Type': 'text/plain'}});

Promise.all([promise1, promise2]).then(function(values) {
  setDataSetOne(values[0]);
 setDataSetTwo(values[1]);
});
 <TableContainer>
      <Table>
        <TableHead>
          <TableRow>
            <TableCell>Dessert (100g serving)</TableCell>
            <TableCell align="right">Calories</TableCell>
            <TableCell align="right">Fat&nbsp;(g)</TableCell>
          </TableRow>
        </TableHead>
         <TableBody>

             <TableCell>DataOne</TableCell>
              <TableCell>DataOne</TableCell>
              <TableCell>DataTwo</TableCell>

       </TableBody>
      </Table>
    </TableContainer>

Json Data 1st

[{
    "name": "adnan hassan",
    "count": 6960
}, {
    "name": "adnan",
    "count": 69666660
}]

2nd Json Data

[{
    "keyword_idea_metrics": {
        "competition": "LOW",
        "avg_monthly_searches": "6600",
        "competition_index": "22",
        "low_top_of_page_bid_micros": "53135896",
        "high_top_of_page_bid_micros": "278963954"
    },
    "keyword_annotations": {
        "concepts": []
    },
    "text": "dubai homes",
    "_text": "text"
}]

Edit: Json Data has been added

4
  • 2
    Please update your question to show the shape of the data in the arrays, and the end result you'd like to see for how they would be merged/displayed in your UI. Commented Mar 21, 2022 at 20:03
  • Probably something like this users.map(user => { const card = cards.find(card => card.userId === user.id) // Now return JSX using card or user properties }). Commented Mar 21, 2022 at 20:06
  • @Willow Json Data Has been added Please check it Commented Mar 21, 2022 at 20:30
  • What do your two input sources have to do with one another? And what does either one have to do with your food/nutrition output table? You need to put more work into your question, I think. Commented Mar 21, 2022 at 21:08

1 Answer 1

1

You can use one object to contain two states like this:

import { useEffect, useState } from 'react';
import axios from 'axios';

const App = () => {
  // use one object to contain two states
  const [data, setData] = useState({ dataOne: '', dataTow: '' });
  // fetch data
  useEffect(() => {
    const fetchData = async () => {
      const dataOne = await axios.post(URL1, inputValue, {
        headers: { 'Content-Type': 'text/plain' },
      });
      const dataTow = await axios.post(URL2, inputValue, {
        headers: { 'Content-Type': 'text/plain' },
      });
      setData({ dataOne, dataTow });
    };
    fetchData();
  }, []);
  // display data
  return (
    <TableContainer>
      <Table>
        <TableHead>
          <TableRow>
            <TableCell>Dessert (100g serving)</TableCell>
            <TableCell align="right">Calories</TableCell>
            <TableCell align="right">Fat&nbsp;(g)</TableCell>
          </TableRow>
        </TableHead>
        <TableBody>
          <TableCell>{data.dataOne}</TableCell>
          <TableCell>{data.dataTow}</TableCell>
        </TableBody>
      </Table>
    </TableContainer>
  );
};

export default App;

I hope it helpful.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Prayx Thank you so much for your comment. It helped me a lot. I solved this issue by both json data merge with lodash.com/docs/4.17.15#mergeWith

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.