2

I am having an array

myArray= ["{ depth: 1, display: 'X' }", "{ depth: 2, display: 'X.X' }", "{ depth: 3, display: 'X.X.X' }", "{ depth: 4, display: 'X.X.X.X' }", "{ depth: 5, display: 'X.X.X.X.X' }", "{ depth: 6, display: 'X.X.X.X.X.X' }"]

I need my output array like this

expectedResult = [{ depth: 1, display: 'X' }, { depth: 2, display: 'X.X' }, { depth: 3, display: 'X.X.X' }, { depth: 4, display: 'X.X.X.X' }, { depth: 5, display: 'X.X.X.X.X' }, { depth: 6, display: 'X.X.X.X.X.X' }]

I tried with this

myArray.map(item => {
               const container = {};
               container[item.depth] = item.display;
               console.log(JSON.stringify(container));
               return container;
             })

But it gives undefined. How can I solve this?

5
  • 3
    You want JSON.parse(), not JSON.stringify. That being said, it's not going to work because the strings in the array are not valid JSON. You need to amend them so that they are valid JSON, however if you can edit the response then it'd make more sense to just not use JSON at all. Commented May 13, 2020 at 14:26
  • Working backwards to get the 'JSON' you would need: var x = { depth:1, display:"x" };JSON.stringify(x) gives '{"depth":1,"display":"x"}' Commented May 13, 2020 at 14:32
  • If it's only and always "depth" and "display", you could use reg ex to extract the values and create a new object. Commented May 13, 2020 at 14:35
  • 1
    You don't need JSON.parse() or RegEx, you can do it with arr.map(item => (new Function('return ' + item))() ), which is flexible to any object, which is in a string format that JavaScript can parse. Commented May 13, 2020 at 14:39
  • 2
    That was not my intention, but I could see how you could read it like that. My apologies. I should have phrased it "an alternative to X & Y is Z". Did not mean to offend you. I just wanted to point out that Javascript has a powerful way to parse strings. There are always several ways to achieve the outcome. One doesn't make another invalid. Commented May 13, 2020 at 14:53

1 Answer 1

2

We could do this by creating a function with a string constructor (this is not the same as using eval):

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#Never_use_eval!

const myArray= ["{ depth: 1, display: 'X' }", "{ depth: 2, display: 'X.X' }", "{ depth: 3, display: 'X.X.X' }", "{ depth: 4, display: 'X.X.X.X' }", "{ depth: 5, display: 'X.X.X.X.X' }", "{ depth: 6, display: 'X.X.X.X.X.X' }"];

const myOutput = myArray.map(item => {
  /* 
   * according to mozilla using Function("return something")
   * is better then eval() - and doesn't use eval
   * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#Never_use_eval!
  */ 
  return (new Function('return ' + item))();

})

console.log(myOutput)
.as-console-wrapper { max-height: 100% !important; top: 0; }

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.