0

I want to concatenate multiple array's into one. My result here is giving multiple array of objects. I want to push all them into a single array. All of them are array's of 5. I want to push each of the array of objects into 1 giant array. Is that possible?

let data = [{"site_nm": "gs Universe", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "gs Gameplay", "date": "2019-10-01", "sigh": 4, "yo": 3, "wokay": 2}, {"site_nm": "gs Universe Trailers", "date": "2019-10-01", "sigh": 5, "yo": 5, "wokay": 0}, {"site_nm": "TR", "date": "2019-10-01", "sigh": 4, "yo": 4, "wokay": 0}, {"site_nm": "gs", "date": "2019-10-01", "sigh": 5, "yo": 5, "wokay": 2}, {"site_nm": "GB", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 1}, {"site_nm": "cn", "date": "2019-10-01", "sigh": 4, "yo": 4, "wokay": 1}, {"site_nm": "Roadshow", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "TV", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "ZD", "date": "2019-10-01", "sigh": 2, "yo": 2, "wokay": 0}, {"site_nm": "Carfection", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "gs Trailers", "date": "2019-10-01", "sigh": 3, "yo": 3, "wokay": 0}, {"site_nm": "gs News", "date": "2019-10-01", "sigh": 3, "yo": 3, "wokay": 0}, {"site_nm": "gs Mobile", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "gs Universe Trailers", "date": "2019-10-02", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "gs Gameplay", "date": "2019-10-04", "sigh": 6, "yo": 6, "wokay": 2}, {"site_nm": "Roadshow", "date": "2019-10-02", "sigh": 2, "yo": 2, "wokay": 0}, {"site_nm": "gs", "date": "2019-10-02", "sigh": 3, "yo": 3, "wokay": 2}, {"site_nm": "TR", "date": "2019-10-02", "sigh": 4, "yo": 3, "wokay": 0}, {"site_nm": "cn Highlights", "date": "2019-10-03", "sigh": 8, "yo": 8, "wokay": 0}, {"site_nm": "gs Universe", "date": "2019-10-02", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "cn en Espa\u00f1ol", "date": "2019-10-02", "sigh": 6, "yo": 6, "wokay": 0}, {"site_nm": "gs Trailers", "date": "2019-10-02", "sigh": 2, "yo": 2, "wokay": 0}, {"site_nm": "ZD", "date": "2019-10-02", "sigh": 4, "yo": 4, "wokay": 1}, {"site_nm": "cn", "date": "2019-10-05", "sigh": 5, "yo": 5, "wokay": 1}, {"site_nm": "TV", "date": "2019-10-02", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "CH", "date": "2019-10-02", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "GB", "date": "2019-10-02", "sigh": 3, "yo": 3, "wokay": 3}, {"site_nm": "DLNow", "date": "2019-10-02", "sigh": 1, "yo": 0, "wokay": 0}, {"site_nm": "gs News", "date": "2019-10-02", "sigh": 2, "yo": 2, "wokay": 0}]

let sites = [...new Set(data.map(({
        site_nm
      }) => site_nm))].sort();


let dates = [...new Set(data.map(({
        date
      }) => date))].sort();

sites.forEach(myFunction);

function myFunction(item) {

  var foo = data.filter(function(e) {
          return e.site_nm == item;
        });
 
  
        let foo_dates = [...new Set(foo.map(({
          date
        }) => date))].sort();
        
  for (
        var date = moment(dates[0], "YYYY-MM-DD"); date <= moment(dates[dates.length - 1], "YYYY-MM-DD"); date = date.add(1, 'days')) {
       
        var dateFormatted = date.format("YYYY-MM-DD");

          if (!foo_dates.includes(dateFormatted)) {
            foo.push({
              "site_nm": item,
              "date": dateFormatted,
              "sigh": "",
              "yo": "",
              "wokay": ""
            });
          }
      }
    console.log(foo);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

I tried

result = [].concat(...foo);
console.log(result);

But this didnt work either. I want to use this giant array outside the function.

This is what my output looks like:

multiple arrays

I want my array to be all these array's in one

5
  • where is the multiple array? I don't see one in your code. data is a single array with multiple objects not arrays if you're referring to that. Commented Oct 21, 2019 at 0:53
  • @Addis, My console.log(foo); is creating multiple array's. Commented Oct 21, 2019 at 0:54
  • can you past the complete code? I couldn't reproduce the problem. Commented Oct 21, 2019 at 1:06
  • @Addis, This the whole code. My current result is [{1,3,4},{1,2,3},{1,2.''}] [{5,3,4},{1,7,3},{1,2.''}] ... In the console its first printing the first site's and its array of objects and then moving on to the next site and their array of objects. I just want to concatenate them all, like: [{1,3,4},{1,2,3},{1,2.''},{5,3,4},{1,7,3},{1,2.''}] Commented Oct 21, 2019 at 1:10
  • I have posted an answer considering your current explanation. Check it out. But you don't need the result = [].concat(...foo); any more. Commented Oct 21, 2019 at 1:15

2 Answers 2

2

sites.forEach should be sites.map, since while they both iterate over the elements in the array, only Array.prototype.map returns a new modified array. That's only if you want to use this modified array. And you would need to return foo from myFunction with something like

console.log(foo);
return foo;

Then, use Array.prototype.reduce to make it a huge array. Add below sites.foreach(now sites.map)

// makes [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 
// become [1, 2, 3, 4, 5, 6, 7, 8, 9]
sites.reduce((finalarray, array) => finalarray.concat(array))

Which just adds all the objects together. finalarray is just the final result, while array is the current element. The reduce method still iterates over all elements.

EDIT: Or you could use sites.flat (taken from @Addis) and assign it to itself or a new variable

// Assigns to itself
sites = sites.map(myFunction).flat()
// Assigns to a new variable
let formattedSites = sites.map(myFunction).flat()
Sign up to request clarification or add additional context in comments.

3 Comments

Hi @BazzaCipher, My result currently is like: [{1,3,4},{1,2,3}] [{5,3,4},{1,7,3}]... Where does the reduce go? And what is the finalarray and array?
Hi @BazzaCipher, Sorry about this, But its creating a new array for each site
For each new array, you could just use sites.concat(newSiteArray)
1

Use the flat() method to create a new array with all sub-array elements concatenated:

console.log(result.flat())

or

console.log([...result].flat())

let data = [{"site_nm": "gs Universe", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "gs Gameplay", "date": "2019-10-01", "sigh": 4, "yo": 3, "wokay": 2}, {"site_nm": "gs Universe Trailers", "date": "2019-10-01", "sigh": 5, "yo": 5, "wokay": 0}, {"site_nm": "TR", "date": "2019-10-01", "sigh": 4, "yo": 4, "wokay": 0}, {"site_nm": "gs", "date": "2019-10-01", "sigh": 5, "yo": 5, "wokay": 2}, {"site_nm": "GB", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 1}, {"site_nm": "cn", "date": "2019-10-01", "sigh": 4, "yo": 4, "wokay": 1}, {"site_nm": "Roadshow", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "TV", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "ZD", "date": "2019-10-01", "sigh": 2, "yo": 2, "wokay": 0}, {"site_nm": "Carfection", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "gs Trailers", "date": "2019-10-01", "sigh": 3, "yo": 3, "wokay": 0}, {"site_nm": "gs News", "date": "2019-10-01", "sigh": 3, "yo": 3, "wokay": 0}, {"site_nm": "gs Mobile", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "gs Universe Trailers", "date": "2019-10-02", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "gs Gameplay", "date": "2019-10-04", "sigh": 6, "yo": 6, "wokay": 2}, {"site_nm": "Roadshow", "date": "2019-10-02", "sigh": 2, "yo": 2, "wokay": 0}, {"site_nm": "gs", "date": "2019-10-02", "sigh": 3, "yo": 3, "wokay": 2}, {"site_nm": "TR", "date": "2019-10-02", "sigh": 4, "yo": 3, "wokay": 0}, {"site_nm": "cn Highlights", "date": "2019-10-03", "sigh": 8, "yo": 8, "wokay": 0}, {"site_nm": "gs Universe", "date": "2019-10-02", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "cn en Espa\u00f1ol", "date": "2019-10-02", "sigh": 6, "yo": 6, "wokay": 0}, {"site_nm": "gs Trailers", "date": "2019-10-02", "sigh": 2, "yo": 2, "wokay": 0}, {"site_nm": "ZD", "date": "2019-10-02", "sigh": 4, "yo": 4, "wokay": 1}, {"site_nm": "cn", "date": "2019-10-05", "sigh": 5, "yo": 5, "wokay": 1}, {"site_nm": "TV", "date": "2019-10-02", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "CH", "date": "2019-10-02", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "GB", "date": "2019-10-02", "sigh": 3, "yo": 3, "wokay": 3}, {"site_nm": "DLNow", "date": "2019-10-02", "sigh": 1, "yo": 0, "wokay": 0}, {"site_nm": "gs News", "date": "2019-10-02", "sigh": 2, "yo": 2, "wokay": 0}]

let bigArray = [];

let sites = [...new Set(data.map(({
        site_nm
      }) => site_nm))].sort();


let dates = [...new Set(data.map(({
        date
      }) => date))].sort();

sites.forEach(myFunction);

function myFunction(item) {

  var foo = data.filter(function(e) {
          return e.site_nm == item;
        });


        let foo_dates = [...new Set(foo.map(({
          date
        }) => date))].sort();

  for (
        var date = moment(dates[0], "YYYY-MM-DD"); date <= moment(dates[dates.length - 1], "YYYY-MM-DD"); date = date.add(1, 'days')) {

        var dateFormatted = date.format("YYYY-MM-DD");

          if (!foo_dates.includes(dateFormatted)) {
            foo.push({
              "site_nm": item,
              "date": dateFormatted,
              "sigh": "",
              "yo": "",
              "wokay": ""
            });
          }
      }
    bigArray.push(foo);
}

console.log(bigArray.flat());

Here I have added another array (bigArray) to hold all foos and then applied flat().

5 Comments

When I do console.log(foo.flat()); I get the same result. I think the problem with my code is that, once the first array is created, it creates the next array and the first one is voided instead of adding to the first one.
I wasn't able to reproduce your output in the first place, it would have been easier that way. Anyway try console.log([...result].flat()).
I added an image and some more context. But honestly this is my whole code
what development environment are you using? let me try other editors and come back to you.
I am using sublime

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.