How do I zip two arrays in JavaScript?
Use the map
method:
var a = [1, 2, 3]
var b = ['a', 'b', 'c']
var c = a.map(function(e, i) {
return [e, b[i]];
});
console.log(c)
How to zip two arrays object in Javascript
Not sure if this is the most efficient way to do this, but it works for the example provided. This method relies on some relatively newer features of JavaScript, but ES6 is widely supported, so it hopefully won't be an issue in your case.
First, isolate the values in arr1 to be used as the object properties for the final result.
Then, re-map the objects of the second array by extracting the values from each object using Object.values() and reduce that to an object with the property names from the first array.
var arr1 = [{a: 'QQQ'}, {b: 'WWW'}];
var arr2 = [{a: 'EEE', b: 'RRR'}, {a: 'TTT', b: 'YYY'}];
var keys = arr1.reduce((valueArray,obj) => [...valueArray, ...Object.values(obj)],[]);
var results = arr2.map(o => Object.values(o).reduce((newObj,v,i) => ({...newObj,[keys[i]]:v}),{}),[])
console.log(results);
How to zip two arrays into one array which every array contains another array
You had the right basic idea. Issue you had is you were mixing up the indexes. And you were not returning anything in the map calls.
const productTitles = [
['a', 'aa'],
['b', 'bb'],
];
const productQuantity = [
[1, 11],
[2, 22],
];
const merged2 = productTitles.map((row, rowIndex) =>
row.map((value, columnIndex) => [value, productQuantity[rowIndex][columnIndex]]));
console.log(merged2);
// Use flatMap if you do not want a nested array
const mergedFlat = productTitles.flatMap((row, rowIndex) =>
row.map((value, columnIndex) => [value, productQuantity[rowIndex][columnIndex]]));
console.log(mergedFlat);
zip two arrays Javascript
Will a straightforward loop do it?
array3 = new Array();
for(var i = 0; i < array1.length; i++)
{
array3.push(array1[i]);
array3.push(array2[i]);
}
Merging (zipping) two arrays of different length with map() in JavaScript
.map
will necessarily create a new array which is the same length as the array being iterated over. Because countries
has 3 items, your output will also have 3 items (an array of arrays) if you use countries.map
.
Using .map
won't work here, because none of your input arrays are the same length as the desired output array. Either .push
to an outside variable, like you already know about, or use .reduce
and push to the accumulator:
const countries = ['US', 'FR', 'IT']
const area = [100, 105, 110, 115, 120, 125, 130]
const merged = countries.reduce((merged, c) => {
area.forEach((a) => {
merged.push(c + a);
});
return merged;
}, []);
console.log(merged);
Systematacally combining arrays
Do like:
const a = ['one', 'two', 'three'], b = ['a', 'b', 'c'], c = ['1', '2', '3'], combo = [];
for(let i=0,l=a.length; i<l; i++){
combo.push([a[i], b[i], c[i]]);
}
console.log(combo);
Related Topics
Scrollintoview() Causing the Whole Page to Move
Jquery Drag/Resize with CSS Transform Scale
Using Jquery to Know When @Font-Face Fonts Are Loaded
What Is the Best Method to Reduce the Size of My JavaScript and CSS Files
How to Access the Value of Invalid/Custom CSS Properties from JavaScript
Vertical Centering Variable Height Image While Maintaining Max-Width/Height
Setting Rounded Corners for Svg:Image
Jquery Animate() and Backgroundcolor
Repeat Animation Every 3 Seconds
Jquery Event That Triggers After CSS Is Loaded
Can You Have a JavaScript Hook Trigger After a Dom Element's Style Object Changes
How to Animate a Progressive Drawing of Svg Path
How to Read an External Local Json File in JavaScript
Why Use Named Function Expressions