-1

i have a this simple object:

{
    "props[EYE][0]": "Yellow",
    "props[EYE][1]": "Blue",
    "props[BODY][0]": "BATMAN",
    "props[BODY][1]": "SUPERMAN"
}

now i want to change this object to something like this

{
    "props-EYE": ["Yellow", "Blue"],
    "props-BODY": ["BATMAN", "SUPERMAN"]
}

the [EYE] and [BODY] are Dynamic and it's not a static data... what is the best way to convert?

thank you for your help...

I try to spilt keys and make new ones but the function still finds the first value.

  • Please see [Fastest way to flatten / un-flatten nested JavaScript objects](https://stackoverflow.com/q/19098797/1048572) and try whether it works for you. If it doesn't, show us the code of your attempt and we can help you find the problem. – Bergi Nov 26 '22 at 19:40
  • 1
    What's generating that original object? Maybe that could be changed to give you the data you want. – Andy Nov 26 '22 at 19:56
  • My guess is that the OP has mischaracterized the input. I strongly suggest an edit where the input is logged to the console and then console output is pasted here. – danh Nov 26 '22 at 20:48

1 Answers1

-2

A quick solution constructing an object by looping through the original object and getting the prop names like EYE or BODY (that are dynamic) that you extract from the original object's keys (like from props[EYE][0]) along with the values found (like Yellow):

  • if the key props-prop-name where prop-name is extracted (like EYE for example) already exists in the new object, then you push the value (like Blue) to the array found there (under the props-EYE key for example).
  • otherwise you initialize an array that contains the value at the current iteration (like Blue).

To quickly extract the key-value pairs from the original object, the method Object.entries can be used along with Array.prototype.reduce to loop through those key-value pairs.

Here's a demo to illustrate

const beforeObj = {
    "props[EYE][0]": "Yellow",
    "props[EYE][1]": "Blue",
    "props[BODY][0]": "BATMAN",
    "props[BODY][1]": "SUPERMAN"
  },
  /**
   * get the key-value pairs from "beforeObj" and then loop through them using the reduce method where the argument "k" is the key (like "props[EYE][0]" and "v" is the corresponding value (like "Yellow")
   */
  afterObj = Object.entries(beforeObj).reduce((a, [k, v]) => {
    /**
     * "prop" holds the key to put in the newly created object (example: "prop" will contain "props-EYE" when the current key is "props[EYE][0]") 
     */
    const prop = 'props-' + k.replace(/.+?\[(\w+)\].*/, '$1');
    /** 
     * if we already has the current prop (like "props-EYE"), then just push the current value to the arrayu found there.
     * Otherwise, initialize a new array that contains the current value
     */
    !!a[prop] ? a[prop].push(v) : a[prop] = [v];
    // return the object for the next iteration 
    return a;
  }, {});

// print the result
console.log(afterObj);
ThS
  • 4,597
  • 2
  • 15
  • 27
  • This ignores the indices from the keys, and doesn't work if the object is enumerates in a different order – Bergi Nov 26 '22 at 20:57
  • @Bergi Did the OP speak about the indices or the ordering whatsoever ? He didn't. I followed what the OP stated in his question, nothing more nothing less (I won't improvise any scenario unless the OP tells us so). – ThS Nov 26 '22 at 21:04
  • The OP didn't speak about what parts of the input is dynamic at all, unfortunately, making the question rather unclear. It's not unreasonable to make guesses about order and continuity of the indices, but still it should be made explicit in the answer that the code relies on such an assumption to work. – Bergi Nov 26 '22 at 21:17
  • @Bergi I understand your point but answers are likely or rather usually will build upon the details found in the question and not assumptions of our own even though sometimes we might have better reasoning than the data/details being found in the questions. Anyway, my answer tried to be as short and descriptive as possible that any reader can easily understand it (and i hope i have succeed on that). – ThS Nov 27 '22 at 00:18