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);
Combine two arrays as pairs
If both arrays have the same length, you can do the follow:
let a = [1,2,3,4,5]let b = [6,7,8,9,10]let combine = a.map((e, i) => [e, b[i]]);console.log(combine);
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 to combine two arrays for a specific index without concatenation?
I think that you're looking for numpy.insert
array1 = np.arange(95,320,4)
array2 = np.arange(0,360,2)
for i, value in enumerate(array1):
index = i+48+i*2
array2 = np.insert(array2, index, array1[i])
combine two arrays in Javascript/react
initialNav
and AdminNav
are objects, not arrays. You want this instead:
const NewNav = { items: [...initialNav.items, ...AdminNav.items] };
How to conditionally combine two numpy arrays of the same shape
We could use NumPy built-in np.maximum
, made exactly for that purpose -
np.maximum(array1, array2)
Another way would be to use the NumPy ufunc np.max
on a 2D
stacked array and max-reduce
along the first axis (axis=0)
-
np.max([array1,array2],axis=0)
Timings on 1 million datasets -
In [271]: array1 = np.random.randint(0,9,(1000000))
In [272]: array2 = np.random.randint(0,9,(1000000))
In [274]: %timeit np.maximum(array1, array2)
1000 loops, best of 3: 1.25 ms per loop
In [275]: %timeit np.max([array1, array2],axis=0)
100 loops, best of 3: 3.31 ms per loop
# @Eric Duminil's soln1
In [276]: %timeit np.where( array1 > array2, array1, array2)
100 loops, best of 3: 5.15 ms per loop
# @Eric Duminil's soln2
In [277]: magic = lambda x,y : np.where(x > y , x, y)
In [278]: %timeit magic(array1, array2)
100 loops, best of 3: 5.13 ms per loop
Extending to other supporting ufuncs
Similarly, there's np.minimum
for finding element-wise minimum values between two arrays of same or broadcastable shapes. So, to find element-wise minimum between array1
and array2
, we would have :
np.minimum(array1, array2)
For a complete list of ufuncs
that support this feature, please refer to the docs
and look for the keyword : element-wise
. Grep
-ing for those, I got the following ufuncs :
add, subtract, multiply, divide, logaddexp, logaddexp2, true_divide,
floor_divide, power, remainder, mod, fmod, divmod, heaviside, gcd,
lcm, arctan2, hypot, bitwise_and, bitwise_or, bitwise_xor, left_shift,
right_shift, greater, greater_equal, less, less_equal, not_equal,
equal, logical_and, logical_or, logical_xor, maximum, minimum, fmax,
fmin, copysign, nextafter, ldexp, fmod
Javascript how to join two arrays having same property value?
I think you can use array#reduce
to do something like this perhaps:
var array1 = [ {'label':"label1",'position':0}, {'label':"label3",'position':2}, {'label':"label2",'position':1}, ];
var array2 = [ {'label':"label1",'value':"TEXT"}, {'label':"label2",'value':"SELECT"} ];
var array3 = array2.reduce((arr, e) => { arr.push(Object.assign({}, e, array1.find(a => a.label == e.label))) return arr;}, [])
console.log(array3);
Merge two numpy arrays
Use np.array
and then np.concatenate
,
import numpy as np
first = np.array([[650001.88, 300442.2, 18.73, 0.575,
650002.094, 300441.668, 18.775],
[650001.96, 300443.4, 18.7, 0.65,
650002.571, 300443.182, 18.745],
[650002.95, 300442.54, 18.82, 0.473,
650003.056, 300442.085, 18.745]])
second = np.array([[1],
[2],
[3]])
np.concatenate((first, second), axis=1)
Where axis=1
means that we want to concatenate horizontally.
That works for me
Merge two arrays and modify them in place (javascript)
You could find the object and update quantity
or push a new object.
const
original = [{ id: 4, quantity: 4 }, { id: 2, quantity: 2 }, { id: 76, quantity: 2 }],
selected = [{ id: 2, quantity: 1 }, { id: 100, quantity: 7 }],
merged = [...original, ...selected].reduce((r, { id, quantity }) => {
const item = r.find(q => q.id === id);
if (item) item.quantity += quantity;
else r.push({ id, quantity });
return r;
}, []);
console.log(merged);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Related Topics
How to Perform Static Code Analysis in PHP
PHP Regex to Get Youtube Video Id
Sending Email Via PHP Mail Function Goes to Spam
How to Minify PHP Page HTML Output
How Do PHP Sessions Work? (Not "How Are They Used")
PHP, Get File Name Without File Extension
Using an Array as Needles in Strpos
PHPmailer - Smtp Error: Password Command Failed When Send Mail from My Server
Reading Very Large Files in PHP
How to Make an Asynchronous Get Request in PHP
What's the Maximum Value For an Int in PHP
String Comparison Using '==' Vs. 'Strcmp()'
Http Authentication Logout Via PHP