Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JavaScript – how to make form String to Array?

I have data const { data: communityData } = useQuery(SEE_ALL_COMMUNITIES_QUERY);

communtyData is Array and it has field named communityName.

if I console.log communityData.communityName[0] then ‘abs‘ comes.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I need to make array looks like below with this ‘abs

 [
   { label: "abs", value: "abs" },
   { label: "gsd", value: "gsd" },
 ]

So I use map function.

  const communityNameList = communityData.seeAllCommunities.map(
    (community, index) =>
      `label:${community.communityName}, value:${community.communityName} }`
  );

I console.log communityNameList,

 Array [
  "{ label:abs, value:abs }",
]

As you can see it has " front and behind the each object.
And I dont’ know how to put " besides abs.

Please help me

>Solution :

You don’t want to turn this into an Array but into JSON specifically if you want to get the effect you wrote the map should look like this

const communityNameList = arr.map((community, index) => {
    return {
        "label": community.communityName,
        "value": community.communityName
    };
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading