Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge multiple arrays together if not null

I am building a React App and am using Async Actions to make request to outside API's and bring in data into my app. I have 4 arrays that I need to merge together. Since I am using Async Actions the default values if theres no data is null. I want to check to see if the array is not null and if it has a value then merge it with the others into a single array. Once I have all the data into a single array I am going to use the includes method to see if any of the values are present in the array. Using the spread operator I don't think is going to work here as it will fail as null is not an iterable. Is there a way to do this with reduce to check through each of the 4 separate arrays make sure the value is not null and then combine them together into one array.

like image 494
James Zilch Avatar asked Sep 13 '18 22:09

James Zilch


People also ask

How do I combine multiple arrays into one?

Using the spread operator or the concat() method is the most optimal solution. If you are sure that all inputs to merge are arrays, use spread operator . In case you are unsure, use the concat() method. You can use the push() method to merge arrays when you want to change one of the input arrays to merge.

Can you concat multiple arrays in JavaScript?

The concat() method concatenates (joins) two or more arrays. The concat() method returns a new array, containing the joined arrays. The concat() method does not change the existing arrays.

How do you merge two arrays using the spread Operator?

To combine two or more arrays, you can either use the functional method []. concat(arr1, arr2) or the spread operator [...arr1,...arr2]. The merging outcome is kept in a new array, making these methods immutable.


2 Answers

Your question leads you to the answer. :)

Essentially you are asking how to filter all non-array inputs and then combine (or concatenate) them together into a new array.

A couple of notes on the following approach:

  • For better stability in filtering, rather than using a blacklist (not null), use a whitelist (Array.isArray) to ensure only arrays are combined.
  • The spread operator can be used to then create an arguments list for a new array's concat method.

const arr1 = [1,2];
const arr2 = null;
const arr3 = [3,4];
const arr4 = [5];

const concat = (...arrays) => [].concat(...arrays.filter(Array.isArray));

console.log(concat(arr1, arr2, arr3, arr4));

For a bit of fun, if the combined array needs to be unique values (assuming simple types for values) then including a quick cast to a Set and back to an Array can make that happen:

const arr1 = [1,2];
const arr2 = null;
const arr3 = [3,4];
const arr4 = [4,5];

const concat = (...arrays) =>[].concat(...arrays.filter(Array.isArray));

const unique = (array) => [...new Set(array)];

const concated = concat(arr1, arr2, arr3, arr4);
const uniqued = unique(concated);

console.log({concated, uniqued});
like image 159
Jason Cust Avatar answered Oct 19 '22 00:10

Jason Cust


Here is a one line solution (ES6).

At the first part, we merge all arrays, and then filter array elements - we include only "not null" values and exclude duplicates:

const arr1 = [1, null, 6, 'q'],
      arr2 = null,
      arr3 = [1, 1, null, 1],
      arr4 = ['e', 'q', 6, 1, null];

const final = []
  .concat(arr1, arr2, arr3, arr4)
  .filter((item, i, arr) => item && arr.indexOf(item) === i);

console.log(final); // Expected output: [1, 6, "q", "e"]
like image 2
Commercial Suicide Avatar answered Oct 19 '22 01:10

Commercial Suicide