0

I've got some query parameters

Exterior Color=Red,Interior Color=Mahogany

that I ultimately want to convert in to this array

            [
                {
                    Question: "Exterior Color",
                    Answer: "Red"
                },
                {
                    Question: "Interior Color",
                    Answer: "Mahogany"
                },
            ]

I've gotten as far as this

[
    "Exterior Color=Red",
    "Interior Color=Mahogany"
]

With

let optionsArray = itemOptions.split(',').filter(Boolean);

But have hit a brick wall on next steps.

1 Answer 1

1

Maybe something like this:

itemOptions.split(',').filter(Boolean).map(keyvalue => {
  const parts = keyvalue .split('=');
  if(parts.length !== 2) return null;
  return { Question: parts[0],  Answer: parts[1]  };
}).filter(Boolean);
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.