How to Combine Two Arrays Together

How to merge two arrays in JavaScript and de-duplicate items

To just merge the arrays (without removing duplicates)

ES5 version use Array.concat:

var array1 = ["Vijendra", "Singh"];
var array2 = ["Singh", "Shakya"];

array1 = array1.concat(array2);

console.log(array1);

How do I concatenate or merge arrays in Swift?

You can concatenate the arrays with +, building a new array

let c = a + b
print(c) // [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]

or append one array to the other with += (or append):

a += b

// Or:
a.append(contentsOf: b) // Swift 3
a.appendContentsOf(b) // Swift 2
a.extend(b) // Swift 1.2

print(a) // [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]

How do I concatenate two arrays in C#?

var z = new int[x.Length + y.Length];
x.CopyTo(z, 0);
y.CopyTo(z, x.Length);

Merge two arrays together in PHP?

i think you have same length for all array for steps and instructions
here you need to store that two array in another array with the particular elements of those array with same key

//array that contain steps
$array1=array('Prep','Cook','Serve');
//array that contain instructions
$array2=array('In a large mixing bowl, crack 2 large eggs.','In a medium-sized frying pan or griddle, heat up a tablespoon of unsalted butter...','When done, transfer the french toast onto a plate.');
//calculate length of any one array because length is same
$length=count($array1);
//store that particular element in new array with keys
$array3=array();
for($i=0;$i<$length;$i++){
$array3[$i]['name']=$array1[$i];
$array3[$i]['text']=$array2[$i];
//you can add that another key and value here like $array3[$i]['image']='your image link';
}
//print the final array
echo '<pre>';
print_r($array3);

Combining two arrays side by side in Angular 7

You should use push method. The issue is that you're adding elements using C++ syntax like.

 this.newacc[i].account = this.acd[i].account;

As I aforementioned, you could use push method by passing the desired object as parameter.

newacc.push({account:acd[i].account, liabilities : acd[i].liabilities });

acs = [ { "account": "Cash In Hand", "liabilities": 0, "assets": 8031597 }, { "account": "Tax Acs", "liabilities": 988363.72, "assets": 0.98 }, { "account": "Sundry Debtor", "liabilities": 0, "assets": 551 }, { "account": "Sundry Creditor", "liabilities": 0, "assets": 0 } ];
acd = acs.filter(f => f.liabilities !== 0);acc = acs.filter(f => f.assets !== 0);
const bigger = acd.length > acc.length ? acd.length : acc.length, newacc = [];for (let i = 0; i < bigger; i++) { if (acd.length > i) newacc.push({account:acd[i].account, liabilities : acd[i].liabilities }); if (acc.length > i) newacc.push({account:acc[i].account, assets : acc[i].assets });}console.log(newacc);

combine two arrays in Javascript/react

initialNav and AdminNav are objects, not arrays. You want this instead:

const NewNav = { items: [...initialNav.items, ...AdminNav.items] };

Merging two arrays in .NET

If you can manipulate one of the arrays, you can resize it before performing the copy:

T[] array1 = getOneArray();
T[] array2 = getAnotherArray();
int array1OriginalLength = array1.Length;
Array.Resize<T>(ref array1, array1OriginalLength + array2.Length);
Array.Copy(array2, 0, array1, array1OriginalLength, array2.Length);

Otherwise, you can make a new array

T[] array1 = getOneArray();
T[] array2 = getAnotherArray();
T[] newArray = new T[array1.Length + array2.Length];
Array.Copy(array1, newArray, array1.Length);
Array.Copy(array2, 0, newArray, array1.Length, array2.Length);

More on available Array methods on MSDN.

How to merge two arrays together and insert into 1 row in a database

$arr1 = [1, 2, 3];
$arr2 = [4, 5, 6];

$merged_arr = array_merge($arr1, $arr2);

$comma_seprated = implode(',', $merged_arr);

Now you can use the $comma_seprated var and put in the tierString column.



Related Topics



Leave a reply



Submit