Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform Array to Object #57

Open
turbopasi opened this issue Sep 22, 2021 · 3 comments
Open

Transform Array to Object #57

turbopasi opened this issue Sep 22, 2021 · 3 comments

Comments

@turbopasi
Copy link

If part of the data looks like this

someArray : [
  { id : "A", value : "10" },
  { id : "B", value : "20" },
  { id : "C", value : "30" }
]

can this part be transformed to

someObject : {
  A : { id : "A", value : "10" },
  B : { id : "B", value : "20" },
  C : { id : "C", value : "30" }
}

?

Maybe if the run function in an operate block gets passed the whole item you could do something like this

operate : [
  {
    run : function (val, item) {
      item.someObject[val.id] = val;
      return val;
    },
    on : "someArray"
  }
]

Just a naive idea basically but I wondered if there is a functionality already built-in. Mapping an Array into an Object is something I use very often.

@turbopasi
Copy link
Author

turbopasi commented Sep 22, 2021

👆 I played around and turned out changing this

123
124   this.setValue(item,method.on,fn(this.getValue(item,method.on), context ));
125

to this

123
124   this.setValue(item,method.on,fn(this.getValue(item,method.on), context, item));
125

along with using nested maps already gave me the possibility to transform an array into an object using a specified key. Would this be considered a good idea ?

@bozzltron
Copy link
Owner

Sorry for the delay. Are you asking if it is a good idea in general? Or if it is a good idea to incorporate into the package for others to use?

@turbopasi
Copy link
Author

Initially I was asking if there is already a way to transform an Array into an Object. If not - yes I believe this would be a great addition also for others. The workaround I found after that was just a quick fix for my use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants