0

New to javascript and I'm struggling with a simple map method.

I'd like to iterate over two arrays of objects, and set state to include the result of each mapping. Currently, state is being overwritten with each new item.

Desired output:

refData = {
 user: [
  {key: 1, value: "A"},
  {key: 2, value: "B"}
 ],
 product: [
  {key: 1, value: "A"},
  {key: 2, value: "B"}
 ],
 role: [
  {key: 1, value: "A"},
  {key: 2, value: "B"}
 ]
}

Current output:

refData = {
 user: [
  {key: 1, value: "A"},
  {key: 2, value: "B"}
 ]
}

refData = {
 product: [
  {key: 1, value: "A"},
  {key: 2, value: "B"}
 ]
}

refData = {
 role: [
  {key: 1, value: "A"},
  {key: 2, value: "B"}
 ]
}

Code that produced the current, incorrect output:

const [refData, setRefData] = React.useState();

  useEffect(() => {

    const refDataArr = [
      { url: "users", field: "user" },
      { url: "products", field: "product" },
      { url: "roles", field: "role" }
    ]

    const fetchRefData = () => {
      try {
        refDataArr.map(async (o, index) => {
          let url = o.url
          let field = o.field
          const res = await axios.get(`http://.../${url}/ `);
          const data = res.data.data

          const resArr = Object.values(data).map((item, index) => {
            return {
              key: item._key,
              value: item[field]
            }
          });
          setRefData({  //overwritten with each returned item
            ...refData, 
            [field]: resArr, 
          })
        })
      } catch (err) {
        console.log(err);
      }
    }
    fetchRefData();
  }, []);

Any help would be really appreciated!

3
  • why did you ask it twice? Commented Nov 7, 2019 at 3:14
  • Can you show the implementation of setRefData function? Commented Nov 7, 2019 at 3:21
  • I agree a better way is to build up the array entirely before calling setRefData. Commented Nov 7, 2019 at 4:14

1 Answer 1

1

A way to do that is to build the json object first and then set that to the state.

  React.useEffect(() => {
    ..........
    let newRefData = {};
    ..........
    try {
      refDataArr.map(async(o, index) => {
          let url = o.url
          let field = o.field
          const res = await axios.get(`http://.../${url}/ `);
          const data = res.data.data

        const resArr = Object.values(data).map((item, index) => {
          return {
            key: item._key,
            value: item[field]
          };
        });
        newRefData[field] = resArr;
      });
      setRefData(newRefData);
    } catch (err) {
      console.log(err);
    }
    ..........
  }, []);
Sign up to request clarification or add additional context in comments.

Comments

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.