2

I've been playing with json objects & using ReactJS. One of the json object represented by variable 'res'. How do I convert 'res' into 'b'. I'm also getting my json object 'res' from an api.

I've reciprocated my problem over this link: https://codesandbox.io/s/epic-leaf-sznhx?file=/src/App.js

const [res, setResult] = useState();
  useEffect(() => {
    (async () => {
      axios.get("https://corona.lmao.ninja/v2/countries").then(response => {
        setResult(response.data);
      });
    })();
  }, []);

How do I convert this:


        const a = 
        {
          "menu_1": {
            "id": "1",
            "menuitem": [{
              "value": "0",
              "onclick": "0()"
            }, {
              "value": "0",
              "onclick": "0()"
            }]
          },
          "menu_2": {
            "id": "2",
            "menuitem": [{
              "value": "2",
              "onclick": "2()"
            }]
          }
        }

into this json object:

const b =

    {
      "popup": {
        "menuitem": [
          {
            "value": "0",
            "onclick": "0()"
          },
          {
            "value": "0",
            "onclick": "0()"
          },
          {
            "value": "2",
            "onclick": "2()"
          }
        ]
      }
    }

2 Answers 2

3

You could do something like the following:

const a = {
  menu_1: {
    id: "1",
    menuitem: [{
      value: "1",
      onclick: "1()",
    }, ],
  },
  menu_2: {
    id: "2",
    menuitem: [{
      value: "2",
      onclick: "2()",
    }, ],
  },
};

let b = Object.keys(a).reduce(
  (p, c) => {
    for (let item of a[c].menuitem) {
      p.popup.menuitem.push(item);
    }
    return p;
  }, {
    popup: {
      menuitem: []
    }
  }
);

console.log(b);

I'm assuming that object a might have more than one menuitem in the array.

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

3 Comments

I'm getting this error: TypeError: Invalid attempt to iterate non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method.
My console.log prints this { "popup": { "menuitem": [] } }
Can you print what are you passing inside the Object.keys()
1

You can use reduce this way

const a = {
  "menu_1": {
    "id": "1",
    "menuitem": [{
      "value": "0",
      "onclick": "0()"
    }, {
      "value": "0",
      "onclick": "0()"
    }]
  },
  "menu_2": {
    "id": "2",
    "menuitem": [{
      "value": "2",
      "onclick": "2()"
    }]
  }
}

const res = Object.values(a).reduce((all, {
  menuitem
}) => {
  all.popup.menuitem = [...all.popup.menuitem, ...menuitem]

  return all
}, {
  popup: {
    menuitem: []
  }
})

console.log(res)

3 Comments

My console.log prints this { "popup": { "menuitem": [] } }
I have just added a sandbox link and more info for reference
you should create a helper function then you in the promise when getting your data you can called it to format the data then put it in the state

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.