Using Javascript I am trying to create one array from a JSON object. Problem is is that I am trying to get the acceptedAnswers which is an array. I figured map was the best function for this. However what is returned is an array of arrays. I want to get on array of the values only. Thanks for your help.
Code
let categories = [
{
"categoryID": "1",
"categoryName": "Fruits with seeds",
"acceptedAnswers": [
"2","5"
]
},
{
"categoryID": "2",
"categoryName": "Fruits without seeds",
"acceptedAnswers": [
"1","3","4"
]
},
{
"categoryID": "3",
"categoryName": "Blue Fruit",
"acceptedAnswers": [
"1"
]
}
]
JS
let catAcceptedAnswerArray = categories.map(function (cat) {
return cat.acceptedAnswers
})
console.log(catAcceptedAnswerArray)
Returns:
0: ["2","5"]
1: ["1","3","4"]
2: ["1"]
I am trying to return:
0: ["2","5","1","3","4","1"]
Here is a fiddle