Background
In Google Apps Script I would like to make a new array that combines two arrays vertically as columns, not as additional rows. I understand it's pretty easy to add additional rows using .concat or .push but this extends the array vertically.
Here's the code:
var ar1 = [[1,3,5],
[2,4,6]];
var ar2 = [[7,9,11],
[8,10,12]];
Desired Outcome
When running ar3 I would like the desired output to be:
[[1,3,5,7,9,11]
[2,4,6,8,10,12]]
Things I've Tried
I think this could be run as a function through concat. I have tried something like the below to no avail:
var ar3 = ar2.forEach(function (row){ ar1.concat([row[0],row[1],row[2]]); });
Could this be made even simpler with .map and return? (I know the below is very wrong, but just an example.)
var ar3 = ar1.map(function (row){ return ar1[row].concat(ar2[row]); });
Use map's index like this:
var ar1 = [[1,3,5],
[2,4,6]];
var ar2 = [[7,9,11],
[8,10,12]];
const out = ar1.map((row,i) => row.concat(ar2[i]))
console.info(out)
You can do it like this, without using concat:
var ar1 = [
[1, 3, 5],
[2, 4, 6]
];
var ar2 = [
[7, 9, 11],
[8, 10, 12]
];
const result = ar1.map((x, i) => [...x, ...ar2[i]]);
console.log('result', result)
Related
I have two arrays in the code below - which matches is the main one and the other played which serves it is purpose for elements to filter out in the main array:
var matches = [[1,4],[3,1],[5,2],[3,4],[4,5],[2,1]];
var played = [2,5];
I need to filter out elements in matches based on played array, which means if there is any 2 or 5 in, then remove it altogether. Also the played array can be any length, min is 1.
Expected output should be
[[1,4],[3,1],[3,4]];
So I have tried this piece of code, but it doesn't yield the result I want.
var result = matches.map(x => x.filter(e => played.indexOf(e) < 0))
So anyway to achieve this?
You could check with some and exclude the unwanted arrays.
var matches = [[1, 4], [3, 1], [5, 2], [3, 4], [4, 5], [2, 1]],
played = [2, 5],
result = matches.filter(a => !a.some(v => played.includes(v)));
console.log(result);
While filtering, check that .every one of the subarray elements are not included in [2, 5]:
var matches = [[1,4],[3,1],[5,2],[3,4],[4,5],[2,1]];
var played = [2,5];
const result = matches.filter(
subarr => played.every(
num => !subarr.includes(num)
)
);
console.log(result);
Another way would be to create a Set of played to avoid iterating it again and again:
var matches = [[1,4],[3,1],[5,2],[3,4],[4,5],[2,1]];
var played = new Set([2,5]);
var out = matches.filter(a => !a.some(num => played.has(num)));
console.info(out)
Is there a function in lodash which makes a union of two arrays by modifying the first one? Union should add the element only if there are no duplicates.
Something along the lines of
a=[1,2,3,4,5,6]; _.mergeArrays(a, [6,7]);
[1,2,3,4,5,6,7]
This can be easily done with "vanilla" JavaScript. It requires ES5 (2009) only, which is implemented by all the major web browsers.
var array = [1,2,3,4,5];
var anotherArray = [6,7];
anotherArray.forEach(function(val) {
if (array.indexOf(val) === -1) {
array.push(val);
}
});
You can use spread element, Set which does not allow duplicate entries
var a = [1, 2, 3, 4, 5];
var add = [6, 7, 3, 5];
a = [...new Set([...a, ...add])];
console.log(a);
How can I create a new array without doing for each? The new array should be as follows
labels : ["Direct", "Organic Search", "Referral"]
any suggestion using javascript?
Ok. Don't want Array.forEach()? Try using Array.map function with ES6 arrow function expression for such case:
// supposing arr is your initial array
var labels = arr.map((v) => v[0]);
console.log(labels); // ["Direct", "Organic Search", "Referral"]
It's not possible to do this without any kind of loop, but as you're suggesting, a manual iteration like a foreach isn't what you're looking for, indeed.
Array.prototype.map() is the function you need in this case. ("under the hood", it's still a loop)
Here's how you can use map to get your desired result:
var myArray = [["a", 1, 2], ["b", 3, 4], ["c", 5, 6]];
var result = myArray.map(function(current){ // For each item in `myArray`
return current[0]; // return it's first element.
}); // And use that in `result`.
console.log(result)
Note that myArray.map does not change the value of myArray. You'll need to store the returned value from .map somewhere.
If you don't need to worry about supporting anything but the latest browsers, you can make this slightly shorter, using ES6:
var myArray = [["a", 1, 2], ["b", 3, 4], ["c", 5, 6]];
var result = myArray.map((c) => c[0]);
console.log(result)
You can try following
var arr1 = []; // your main array
var labels = arr1.map(function(item){
return item[0];
});
For reference, map
So I feel like I should be able to figure this one out, but for whatever reason, i've having some difficulty with it this morning.
I have an array with multiple arrays inside, and i want to loop through this big array and only list the first element in the smaller arrays.
so my array looks something like this
var array = [
[1, 2],
[1, 3],
[3, 4]
]
So, essentially I want to be able to list, (1, 1, 3). The problem for me is that when i try to approach any for loop, i am able to separate the arrays, but not able to list the first element in each smaller array.
I know this is pretty elementary, and even though i did take a look and did not find much, i do feel like this question has already been asked.
Any help with this would be wonderful.
Much thanks.
You can use map() for creating a modified array
var array = [
[1, 2],
[1, 3],
[3, 4]
];
var res = array.map(function(v) {
return v[0];
});
alert(res)
If you just want to list [1,1,3], then this might be enough:
array.map(function(item) {
return item[0];
});
Cheers,
Karol
How about :
var newArray = [];
array.forEach(function(el) {
newArray.push(el[0]);
});
console.log(newArray);
Just use for(...) instead of others for big array. It is fastest. You can see their speed difference in http://jsperf.com/find-first-from-multiple-arrray
var array = [
[1, 2],
[1, 3],
[3, 4]
], r = [];
for (var i = 0; i < array.length; i++) {
r.push(array[i][0]);
}
console.log(r);
Right now I have an array of the form [[1, 2], [3, 4], ...] and need to use an array of the keys [1, 3, ...] and was wondering if there was a javascript or d3 library function that took in the array of arrays and a function, then returned a new array according to the function. Something like this:
var data = [[1, 2], [3, 4]]
var keyArray = d3.transformArray(data, function(d) { return d[0]});
// keyArray = [1, 3]
So I can avoid looping over the data array again
var keyArray = [];
for (i = 0; i < data.length; i += 1) {
keyArray.push(data[i][0]);
}
// keyArray[1, 3]
This seems like a common enough thing to do using d3, but I wasn't sure if there's a specific name for this process of using a specific object and a function to create a new object of the same type.
you can use Array.prototype.map
x = [1,2,3].map(function(item){return item+1;});
this will result int [2,3,4]
read about this here https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map