-6

I have data return form controller

foo:
0:{id: 1, add_1: "123", add_2: "add1"} 
1: {id: 2, add_1: "456", add_2: "add2"}

How can I change the key to like this

foo:
id:{1,2}
add_1:{123,456}
add_2:{add1,add2}

I'm try not change like this please help me

  • That's an _object_. You can't use `map` on an object. – Andy Dec 10 '18 at 02:02
  • 3
    The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Dec 10 '18 at 02:06
  • 1
    {1,2} this isn't correct – Beginner Dec 10 '18 at 02:07
  • Research:[How is almost everything in Javascript an object?](https://stackoverflow.com/questions/9108925/how-is-almost-everything-in-javascript-an-object), [Access value in a multidimensional object with unique keys](https://stackoverflow.com/questions/35136168/how-can-i-access-a-value-in-a-multidimensional-object-with-unique-keys-in-javasc), [Create dynamic multidimensional object](https://stackoverflow.com/questions/24381543/creating-dynamically-multidimensional-object-array) & [ditto](https://stackoverflow.com/questions/11648586/create-multidimensional-object-with-multidimentional-arrays). – Tedinoz Dec 10 '18 at 03:03
  • Why is the Vuejs tag used here? There is nothing vue related in you question – Ankit Kante Nov 23 '19 at 16:25

1 Answers1

0

//perhaps, you probably do it like this :

var result = {}; 
$.each([{id: 1, add_1: "123", add_2: "add1"}, {id: 2, add_1: "456", add_2: "add2"} ], function(i, item ){
        for(var pro in item ){
            result[pro] = result[pro] || [];
            result[pro].push(item[pro]);
        }
})
console.log(result);
windfog
  • 209
  • 1
  • 8