Home > Enterprise >  Object transformation question in javascript
Object transformation question in javascript

Time:07-13

I was asked this question in my recent interview they asked me to transform the given object into the expected output.

var data = {
      data1 : "431",
      data2 : "321",
      data1 : "2134"
}

//excepted output
data1 = ["431", "2134"]
data2 = ["321"]

And I am not sure about how to approach this.

CodePudding user response:

This is impossible, because your first statement destroys the data1 : "431" information.

Its important to notice that this:

var data = {
      data1 : "431",
      data2 : "321",
      data1 : "2134"
}

is identical to this:

var data = {
      data2 : "321",
      data1 : "2134"
}

So what you want is not possible.

  • Related