Last active
July 7, 2022 11:29
-
-
Save keon/72b518b743d365d85f68 to your computer and use it in GitHub Desktop.
use of map to reorganize JSON object array in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var data = [ | |
{ | |
"answer_content": "2322323", | |
"user_id": 49, | |
"user_name": "soemthing" | |
}, | |
{ | |
"answer_content": "22222", | |
"user_id": 50, | |
"user_name": "soemthing" | |
} | |
]; | |
var mapped = data.map(function(obj) { | |
return { | |
"answer_content": obj.answer_content, | |
"author": { | |
"user_id": obj.user_id, | |
"user_name": obj.user_name | |
}}; | |
}); | |
console.log(mapped); | |
///RESULT /// | |
[[object Object] { | |
answer_content: "", | |
author: [object Object] { | |
user_id: 49, | |
user_name: "soemthing" | |
} | |
}, [object Object] { | |
answer_content: "22222", | |
author: [object Object] { | |
user_id: 50, | |
user_name: "soemthing" | |
} | |
}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment