-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

5

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you ^_^ I can use in ?
@WorawutJantarik if it works then accept the answer to help another one to know that it works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.