3

I have a result from Q.all() which is something like this -

promise = [Arr1,Arr2,Arr3....]

Each Arr can be either null or an array of plain JS objects.

I want to join all these arrays to one big array;

I can loop over and use array concat method to join them.

Is there any other elegant solution which is inbuilt in JS ?

Here is sample array -

    [
      {
        "endDate": "2015-06-11 14:52:00",
        "quantity": 75,
      },
      {
        "endDate": "2015-06-11 14:42:00",
        "quantity": 78,
      },
      {
        "endDate": "2015-06-01 14:43:00",
        "quantity": 69,
      },
      {
        "endDate": "2015-05-14 13:38:00",
        "quantity": 85,
      }
    ]

I have these libraries available as well lodash, angular

4
  • Can you add sample array Commented Jun 12, 2015 at 6:45
  • Your sample is just one array, but your question says you have an array of them (e.g., nested arrays). Commented Jun 12, 2015 at 6:54
  • @T.J.Crowder yes its sample of one array. All the other array follow same structure Commented Jun 12, 2015 at 6:55
  • If you have a simple loop that does what you want, I would stick with it. I bet it will be more readable than the LoDash solutions that have been proposed. Commented Jun 12, 2015 at 6:56

2 Answers 2

3

I believe that would be a combination of flattenDeep (to do the flattening) and without (to remove nulls — at least, I think you wanted to remove nulls; if not, take without out):

var result = _.without(_.flattenDeep(yourArray), null);

Live Example:

// NOTE: You said you had an array with arrays in it, so I've taken
// the one array you gave and used it as two entries in the array
// below (with some minor mods). Note also the nulls.
var yourArrays = [
    [
        {
            "endDate": "2015-06-11 14:52:00",
            "quantity": 75
        },
        {
            "endDate": "2015-06-11 14:42:00",
            "quantity": 78
        },
        {
            "endDate": "2015-06-01 14:43:00",
            "quantity": 69
        },
        {
            "endDate": "2015-05-14 13:38:00",
            "quantity": 85
        }
    ],
    null,
    null,
    [
        {
            "endDate": "2015-07-11 14:52:00",
            "quantity": 12
        },
        {
            "endDate": "2015-07-11 17:42:00",
            "quantity": 34
        },
        {
            "endDate": "2015-07-01 13:43:00",
            "quantity": 56
        },
        {
            "endDate": "2015-08-14 12:38:00",
            "quantity": 85
        }
    ]
];
var result = _.without(_.flattenDeep(yourArrays), null);
document.body.insertAdjacentHTML(
"beforeend",
"<pre>" + JSON.stringify(result, null, 2) + "</pre>"
);
<script src="https://cdn.rawgit.com/lodash/lodash/3.0.1/lodash.min.js"></script>

Sign up to request clarification or add additional context in comments.

3 Comments

I prefer compact() over without() for removing falsey values.
@AdamBoduch: I don't know he wants to remove all falsey values. :-) Actually, I'm not quite sure where I got the idea he wanted to remove nulls, but I'm fairly sure he does...
@T.J.Crowder, Yes I actually wanted to remove nulls thats why I mentioned there are possible null values as well. But without me explicitly mentioning you used without().Great
0

you cam simple use .map function.

var promise = [[0,1],[2,3],[],[6,7]]
var combineArr = promise.map(function(value, index, array) {
    return array[index];
  });
alert(combineArr);  

DEMO

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.