-3

I have this call that returned a response like this:

 let pokemonTypes = pokemonData.map((pokemon, i) =>
   {
   let singleType = pokemon.type;
     console.log(singleType);
   }
  );

enter image description here

Please note the arrays are COMPLETELY separate and do not have any parent array or object wrapper.

How can I grab these arrays and wrap them into one or even better concatenate all of their values into one array as in:

["fire", "grass", "water", "fire", "grass", "flying"]
Zeusox
  • 7,708
  • 10
  • 31
  • 60
  • Does this answer your question? [JavaScript: How to join / combine two arrays to concatenate into one array?](https://stackoverflow.com/questions/3975170/javascript-how-to-join-combine-two-arrays-to-concatenate-into-one-array) – ASDFGerte Mar 11 '20 at 21:37
  • @ASDFGerte No that is different. These are completely separate arrays... – Zeusox Mar 11 '20 at 21:41
  • @customcommander the returned response after mapping through the object is actually in Json format, except for the arrays are all separate because they all come from different objects – Zeusox Mar 11 '20 at 21:48
  • @customcommander please check the exact response as screenshot from the console... – Zeusox Mar 11 '20 at 21:54
  • @Zeusox It seems to me that you do have references to these arrays; they just live under different namespaces. So you can use `Array#concat` for example – customcommander Mar 11 '20 at 21:57
  • @customcommander how can I grab or view the references? – Zeusox Mar 11 '20 at 22:03

3 Answers3

1

An alternative way to approach this using a function and reduce

const first = ['bird', 'tiger'];
const second = ['bird'];
const third = ['lion'];
const fourth = ['tiger', 'duck'];

const myFunc = (...args) => {
    return args.reduce((prev, item) => {
        return [...prev, ...item];
    }, [])
}

const result = myFunc(first, second, third, fourth)
console.log(result)
// ["bird", "tiger", "bird", "lion", "tiger", "duck"]

https://jsbin.com/picutod/1/edit?js,console

patrick
  • 9,837
  • 3
  • 20
  • 27
  • Please these are completely separate arrays as in I cannot even grab them with their index... – Zeusox Mar 11 '20 at 21:42
  • @Zeusox that doesn't make any sense... You also updated the question significantly since time of writing this answer. – patrick Mar 12 '20 at 04:57
1

Does this help?

const pokemonData = [{ type: ['Fire'] }, { type: ['Grass', 'Poison'] }]
const types = pokemonData.reduce((acc, {type}) => (acc.push(...type), acc), [])
console.log(types) // [ "Fire", "Grass", "Poison" ]
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
0

You'll have to grab them as you get them.

let allPokemonTypes = [];

function onNewPokemonType(pokemonTypes) {
    console.log(pokemonTypes);
    allPokemonTypes = [...allPokemonTypes, ...pokemonTypes];
}
Brandon Dyer
  • 1,316
  • 12
  • 21
  • Could you elaborate on that? – Zeusox Mar 11 '20 at 22:02
  • That function just represents what should happen when you get your pokemon types. When you get a new one, you can add the elements to an array. This way you'll have them all together – Brandon Dyer Mar 11 '20 at 22:03