TheDevGuy
TheDevGuy

Reputation: 703

Conversion from Object to array

I need to convert an object on certain type, I have made a fiddle to best understand. I think I have to use maybe Object.value method ? but I'm not good at this.

https://jsfiddle.net/sebastianczech/2wfapuv4/97/

I need this at result

intensity: {1: [[54,78},[45,12]],3: [[40,50], [80,12],[99,2]], 9: [[54,21],[25,14],[25,47],[87,98],[45,45]]}
  data() {
    return {
      intensity: {
        "1": [{
          "in": "54",
          "out": "78"
        }, {
          "in": "45",
          "out": "12"
        }],
        "3": [{
            "in": "40",
            "out": "50"
          }, {
            "in": "80",
            "out": "12"
          },
          {
            "in": "99",
            "out": "2"
          }
        ],
        "9": [{
            "in": "54",
            "out": "21"
          }, {
            "in": "25",
            "out": "14"
          },
          {
            "in": "25",
            "out": "47"
          }, {
            "in": "87",
            "out": "98"
          },
          {
            "in": "45",
            "out": "45"
          }
        ]
      },
    }
  },

thanks a lot

Upvotes: 0

Views: 33

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138537

 const result = Object.fromEntries(
    Object.entries(data().intensity)
      .map(([k, v]) => ([k, v.map(it => ([it.in, it.out]))]))
 );

Turn the object into an array of key-value pairs, map it to an array of key value pairs with the values being the values of the previous value objects, then turn that back into an object.

Upvotes: 2

Related Questions