1

Say I have strings in the following format: HAMILTON.Angelica, WICKED.Glinda, HAMILTON.Eliza, HAMILTON.AndPeggy, WICKED.Elphaba

I want to create an array in this format: HAMILTON=[{Angelica, Eliza, AndPeggy}] WICKED=[{Glinda, Elphaba}]

I want it to be iterative, so I want to detect which array the string will be included one at a time. Say for example, first would be HAMILTON.Angelica, I want my code to detect if there is an array called HAMILTON (create one if there's none) and push the string after the "."

2
  • {Angelica, Eliza, AndPeggy} is not a valid javascript construct Commented Jan 25, 2023 at 10:45
  • Hello leenmanwelmeerunduh, welcome to StackOverflow. The question is straightforward (the title is formatted as a question and is concise). And your problem is well-defined. For future questions, try adding a minimal reproducible example, and format it for readability when presenting arrays. Questions in StackOverflow are meant to be re-usable by folks with a similar problem and can be viewed by thousands of people. This first question is clear, researchable and answerable but needs formatting. Thanks for your contribution! :) Commented Jan 27, 2023 at 1:01

1 Answer 1

1

I suggest adding the strings to an array to make the process easier so you'll need to do some iteration, and I'd recommend using an object for holding the final information as they're designed for structures like this. You key/value pairs can be the show name, and an array of characters assigned to that object key.

  1. Put your strings into an array. Make sure you quote the strings.
  2. Create a new object
  3. Iterate over the array
  4. Split each string into an array of [show, character].
  5. If the show in the current iteration doesn't exist in the object, create it, and set its value as an array
  6. Add the current character to the show array

// Array of strings
const arr = ['HAMILTON.Angelica', 'WICKED.Glinda', 'HAMILTON.Eliza', 'HAMILTON.AndPeggy', 'WICKED.Elphaba'];

// New object
const result = {};

// Iterate over the array
for (const str of arr) {

  // Split each string on the period to get an
  // array of one show and one character
  const [ show, character ] = str.split('.');

  // If the show doesn't exist already on the object as a key
  // create it and set its value as an array
  result[show] ??= [];

  // Finally push the character into the array
  result[show].push(character);
}

console.log(result);

// To get separate variables (they would be arrays, not objects)

const { HAMILTON, WICKED } = result;

console.log(HAMILTON);
console.log(WICKED);

Additional documentation

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

2 Comments

Hi. Thank you for this, I am wondering, and what I really need, is how can I make HAMILTON and WICKED as separate objects with the characters inside it.
Well, they wouldn't be objects they'd just be named arrays: const { HAMILTON, WICKED } = result after you run that code. See Destructuring assignment.

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.