0

I have this multiple array and i want to convert it to one array with javascript codes and get data from it with flatMap:

    [
        {
      id: '46892319372',
      user_name: 'testerOne',
      identifier: '20202'
    }
]
[
    {
      id: '15243879678',
      user_name: 'testerTwo',
      identifier: '20201'
    }
]
[
    {
      id: '02857428679',
      user_name: 'testerThree',
      identifier: '20203'
    }
]
[
    {
      id: '65284759703',
      user_name: 'testerFour',
      identifier: '20204'
    }
]

i want to convert this multiple arrays to just one array like this with javascript: I have only one variable that contains this ↓ or maybe more than 4 or less than 4 i don't know i just have a variable that contains the arrays like this in it and i can't put these to another variable or separate them with , or ... i just have this ↓

[
        {
      id: '46892319372',
      user_name: 'testerOne',
      identifier: '20202'
    },
    {
      id: '15243879678',
      user_name: 'testerTwo',
      identifier: '20201'
    },
    {
      id: '02857428679',
      user_name: 'testerThree',
      identifier: '20203'
    },
    {
      id: '65284759703',
      user_name: 'testerFour',
      identifier: '20204'
    }
]

I tried array.concat but i have only one variable with that arrays so i tried list.concat(list) but i got something like this as result :

    [
        {
          id: '46892319372',
          user_name: 'testerOne',
          identifier: '20202'
        },
        {
          id: '46892319372',
          user_name: 'testerOne',
          identifier: '20202'
        }
   ]
   [
        {
          id: '15243879678',
          user_name: 'testerTwo',
          identifier: '20201'
        },
        {
          id: '15243879678',
          user_name: 'testerTwo',
          identifier: '20201'
        }
   ]
   [
        {
          id: '02857428679',
          user_name: 'testerThree',
          identifier: '20203'
        },
        {
          id: '02857428679',
          user_name: 'testerThree',
          identifier: '20203'
        }
   ]
   [
        {
          id: '65284759703',
          user_name: 'testerFour',
          identifier: '20204'
        },
        {
          id: '65284759703',
          user_name: 'testerFour',
          identifier: '20204'
        }
    ]
  • Does this answer your question? [How to merge two arrays in JavaScript and de-duplicate items](https://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items) Note: answer covers approaches that do not de-duplicate entries including [destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) which would be the "modern" approach: `const arr3 = [...arr1, ...arr2];`. – D M Jun 01 '22 at 18:20
  • Welcome to [so]! Please take the [tour], visit the [help] and read up on [asking good questions](https://stackoverflow.com/help/asking). After doing some research and [searching](https://stackoverflow.com/help/searching) for related topics on SO, try it yourself. If you're stuck, post a [mcve] of your attempt and note exactly where you're stuck. People will be glad to help. – Scott Sauyet Jun 01 '22 at 18:22
  • @DM no thats not my answer but thank you – TheGloriesVe Jun 02 '22 at 12:17
  • @ScottSauyet I'll read that and im sorry if i did something wrong. Actually i searched before but i didn't get my answer. I don't have arrays like this i just have them as a variable – TheGloriesVe Jun 02 '22 at 12:18
  • There's nothing to be sorry for; you're new here. That material should explain how the site is meant to be used. The gist of it is that you're expected to try things by yourself, and when you do ask a question, show your best attempt and explain why it isn't working. Then people will be more willing to help. – Scott Sauyet Jun 02 '22 at 12:23
  • But a hint is that there is a method on [`Array.prototype`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) designed to do exactly what you need (assuming that you have all those elements in a single array.) – Scott Sauyet Jun 02 '22 at 12:26

2 Answers2

0

What you have here is not a multiple array (an array of arrays), but actually multiple arrays. Not sure how you got here but I am assuming you didn't create these, as the solution would be to just be to wrap these in an array and separate each array with a comma, then anything you do to flatten it will work.

Sorry if this is just too rudimentary an answer for now.

Did you create this (can you easily edit the structure) or were these returned to you?

  • No i didn't create this and i get this from my api actually i just have a var list that contains this multiple arrays or whatever it is. I have to print them like this : (id=x,name=x,identifier=x and next one id=x,name=x,identifier=x) but with this it sends them as 4 different prints(results). Thank you – TheGloriesVe Jun 02 '22 at 12:14
  • I see you reformatted it so the arrays are comma separated. If that's what you have, you can get a single array of objects with: const flatArr = arrayList.flatMap(array => array) – internetdrew Jun 02 '22 at 17:23
  • No arrays aren't comma separated. Second code in my question is what i want from the first code i wrote in my question. And the third one is what i get as result from testing `array.concat()` – TheGloriesVe Jun 02 '22 at 17:56
  • It might be helpful to post what you're getting this returned from. Because just inserting the original into my editor kicks back errors when you try to turn it into a variable, which makes me confused as to how you would even get this as a return instead of an error. – internetdrew Jun 02 '22 at 18:55
  • I get this from my api, it searchs in the twitch and brings this for me. I don't know too, this is why i asked here. Can we put this variable to an array then convert it to what i want? with code? – TheGloriesVe Jun 02 '22 at 19:53
0

Im not sure if this is what youre after but.. this only works with arrays that contain object literals.

let arrayList = [
  [{
    id: '46892319372',
    user_name: 'testerOne',
    identifier: '20202'
  }],
  [{
      id: '15243879678',
      user_name: 'testerTwo',
      identifier: '20201'
    },
    {
      id: '15243879678',
      user_name: 'testerTwo',
      identifier: '20201'
    }
  ],
  [{
    id: '02857428679',
    user_name: 'testerThree',
    identifier: '20203'
  }],
  [{
    id: '65284759703',
    user_name: 'testerFour',
    identifier: '20204'
  }]
]

function mergeArrays() {
  let arrayObject = []
  for (let arrayLists of arguments) {
    for (let array of arrayLists) {
      if (array.constructor !== Array) continue;
      for (let object of array) {
        if (object.constructor !== Object) continue;
        arrayObject.push(object);
      }
    }
  }

  return arrayObject;
}
console.log(mergeArrays(arrayList))
Aliza
  • 74
  • 4
  • Have you checked this? https://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items – Collins USHI Jun 02 '22 at 03:37
  • Actually i can't put them to these variables (array1, array2, ..) cause i didn't create it and i don't know how many of these i have. I have to get these with code and the only thing that i have is var list that contains this multiple array or whatever it is. Thank you – TheGloriesVe Jun 02 '22 at 12:13
  • I updated the code, is this what you're after? – Aliza Jun 02 '22 at 12:42
  • No it's not, in your variable you put those to another [] and separated them with , but i just have a "list" variable that contains that arrays i put there for you in my question. Thank you – TheGloriesVe Jun 02 '22 at 16:20
  • Do you mean that you have an array that contains arrays or that you have an object literal that contains arrays? Or a variable to which you're reassigning different arrays to, and you want to run a function that adds the array to the new object every time you reassign the variable? – Aliza Jun 02 '22 at 16:40
  • I mean i don't know, i don't have an array. I just have a variable "var list" that when i log it to the console, i get this arrays that i put in my question, and this is exactly what it prints. I just have that as a variable and i want to make it to that i wrote in my question. Thank you – TheGloriesVe Jun 02 '22 at 16:56