[Vuejs]-Split a json data into two separate jsons and add them in code

0👍

Assuming you already know how to fetch your data from your API, the final result would be something like this :

const getDataAndSplit = async () => {
  const myData = await fetch(URL, etc etc); // get your data from your API
  const rItems = []; // declaring empty array to fill it later
  const sItems = []; // same
  // looping over array of key/values
  Object.entries(myData).forEach(([key, val]) => {
    if (val.startsWith('r'))
      rItems.push({label: key, value: val});
    else
      sItems.push({label: key, value: val});

  }
  return {rItems, sItems};
}

Leave a comment