1

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

3 Answers 3

4

map isn't the right tool to reach for here. Simply create an array and append each object's entries to it:

const result = [];
for (const category of categories) {
    result.push(...category.acceptedAnswers);
}

Or if you can't use ES2015+ features, an ES5 version:

var result = [];
categories.forEach(function(category) {
    result.push.apply(result, category.acceptedAnswers);
});

This assumes there won't be thousands and thousands of acceptedAnswers; a single call to push has platform-specific limits (but again, they're in the thousands).

Like almost all array operations, it's possible to shoehorn this into reduce, but you don't gain anything by it:

var result = categories.reduce(function(arr, category) {
    arr.push.apply(arr, category.acceptedAnswers);
    return arr;
}, []);
Sign up to request clarification or add additional context in comments.

2 Comments

You have my sword.
Thanks for that it worked as advertised! I used your first example.
2

With ES6 you can try with:

categories
  .map(category => category.acceptedAnswers)
  .reduce((acc, ids) => acc.concat(ids), [])

2 Comments

It's hideously inefficient, but yes, you can. :-)
Slightly. :-) concat creates new array too. There's no need for any temporary arrays here.
1

You can simply reduce the Array to first level after mapping using Array#concat

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"
    ]
  }
]

let catAcceptedAnswerArray = categories.map(cat => cat.acceptedAnswers).reduce((acc, val) => acc.concat(val), []);

console.log(catAcceptedAnswerArray)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.