How to combine 3 multidimentional arrays using GAS? - javascript

I got prepared 3 arrays, which I'd like to combine into one:
let array1 =
[
["Col1", "Col2", "Col3"],
["Data1","Data2","Data3"]
]
let array2 =
[
["Col5", "Col10", "Col21"],
["Data5","Data10","Data21"]
]
let array3 =
[
["Col8", "Col11"],
["Data8","Data11"]
]
How can I make it like this?
resultingArray =
[
["Col1", "Col2", "Col3","Col5", "Col10", "Col21","Col8", "Col11"],
["Data1","Data2","Data3","Data5","Data10","Data21","Data8","Data11"]
]
where order of the columns doesn't matter.
I've tried using push(), ... the spread operator and concat(), but I haven't been able to apply it properly.
thank you!

Description
You can Array.concat each row of the 3 arrays into 1 row.
Script
let combined = array1.map( (row,i) => row.concat(array2[i],array3[i]) );
console.log(combined);
Console.log
12:12:18 PM Notice Execution started
12:12:19 PM Info [ [ 'Col1', 'Col2', 'Col3', 'Col5', 'Col10', 'Col21', 'Col8', 'Col11' ],
[ 'Data1',
'Data2',
'Data3',
'Data5',
'Data10',
'Data21',
'Data8',
'Data11' ] ]
12:12:18 PM Notice Execution completed
Reference
Array.concat()
Array.map()

Maybe something like that
let array1 =
[
["Col1", "Col2", "Col3"],
["Data1", "Data2", "Data3"]
]
let array2 =
[
["Col5", "Col10", "Col21"],
["Data5", "Data10", "Data21"]
]
let array3 =
[
["Col8", "Col11"],
["Data8", "Data11"]
]
const arrays = [array1, array2, array3]
const res = arrays.reduce((acc, [cols, data]) => [[...acc[0], ...cols], [...acc[1], ...data]], [[], []])

function myFunction() {
let a1 = [["Col1", "Col2", "Col3"], ["Data1", "Data2", "Data3"]];
let a2 = [["Col5", "Col10", "Col21"], ["Data5", "Data10", "Data21"]];
let a3 = [["Col8", "Col11"], ["Data8", "Data11"]];
Logger.log([[...a1[0],...a2[0],...a3[0]],[...a1[1],...a2[1],...a3[1]]]);
}

Related

How can I shuffle the elements of two arrays with a certain condition?

There are two arrays, I need to get all possible options for shuffling them, but with one condition. You can take only the first element of each array for shuffling, after which this element is deleted. How can this be implemented?
If there is only 2 arrays, and you can take only the first element, there isn't many possibles options, but only one.
And for this is a simple implementation :
[array1, array2] = [ [ array2[0] ], [ array1[0] ] ];
For exemple if you have :
const array1 = [1, 2, 3];
const array2 = ["a", "b", "c"];
[array1, array2] = [ [ array2[0] ], [ array1[0] ] ];
console.log(array1); // ["a"]
console.log(array2); // [1]
array1[0] get the first element of array (you can read it like : element at index 0 of array1).
So you will get for this exemple the value 1.
Then you put this element inside a new array : [ array1[0] ]
So you have a new array with just the first element, and get rid of the rest like you wanted : [1].
There is others ways to do it, for example : array1.slice(0, 1).
this will return a new array with the element for index 0 to 1 of array1.
[array1, array2] = [ [ array2[0] ], [ array1[0] ] ]; is now equivalent of [array1, array2] = [ ["a"], [1] ];.
It's called a destructuring assignment.
It's almost the same as writing :
array1 = [ array2[0] ];
array2 = [ array1[0] ];
But if you do that, array1 and array2 will be the same, because you get the value of array1 after it's reassignment.
So you have to pass through a third variable :
const tempValue = [ array1[0] ];
array1 = [ array2[0] ];
array2 = tempValue;
Same result, but it's more simple ans more elegant to use :
[array1, array2] = [ [ array2[0] ], [ array1[0] ] ];

append array to array javascript

i have an empty array and need to append array through a nested for loop.
const arr1=[ [ 1,3 ], [ 5,4 ], [ 8,6, 11 ], [ 15, 19, 12 ] ];
const arr2=[ [21,23],[ 3, 2 ], [ 1,4 ], [ 5, 6 ,11 ] ];
const arr3=[];
console.log('Lost assests counts');
for (let i=0;i<arr1.length;++i)
{
const arrTemp=[]
arrTemp.push(arr1[i].length)
for (let j=i+1;j<arr2.length;++j){
const intersection = arr2[j].filter(element => arr1[i].includes(element));
arrTemp.push(intersection.length)
}
console.log(arrTemp);
}
This returns the below output(this is the output of arrTemp inside the loop):
Instead of the above output, Can I have it like the below way?
[[2,1,1,0],[2,1,1],[3,2],[3]]
Did you just want to push the values to arr3? In that case, instead of printing the value within the loop:
console.log(arrTemp);
Push it to the target array in the loop:
arr3.push(arrTemp);
Then after the loop, print the whole thing:
console.log(arr3);
Full example:
const arr1=[ [ 1,3 ], [ 5,4 ], [ 8,6, 11 ], [ 15, 19, 12 ] ];
const arr2=[ [21,23],[ 3, 2 ], [ 1,4 ], [ 5, 6 ,11 ] ];
const arr3=[];
console.log('Lost assests counts');
for (let i=0;i<arr1.length;++i)
{
const arrTemp=[]
arrTemp.push(arr1[i].length)
for (let j=i+1;j<arr2.length;++j){
const intersection = arr2[j].filter(element => arr1[i].includes(element));
arrTemp.push(intersection.length)
}
arr3.push(arrTemp); // I changed this
}
console.log(arr3); // I added this

How to check if an array of strings contains all elements of the array?

I have an array of strings array1 , and array2 to compare.
How to check for the occurrence of all elements in strict accordance and create a new array.
I understand that in my case it is necessary to apply filter + includes.
I have seen the answer for string.
An example of what is required in my case and the expected result:
array1 [ 'apple_mango_banana', 'mango_apple_banana', 'apple_banana' ]
array2 [ 'apple', 'mango' ]
result ['apple_mango_banana', 'mango_apple_banana' ]
You said array of strings but your code looks like an array of arrays of strings.
Assumining it's the latter, do you do it like this:
let array = [['apple', 'mango', 'banana'], ['mango', 'apple', 'banana'], ['apple', 'banana']]
let comp = ['apple', 'mango']
let ans = []
for (el of array) {
if (comp.every(y => el.includes(y))) {
ans.push(el)
}
}
console.log(ans)
I'm sure you can find a one-liner but this way works.
const array1 = [ 'apple_mango_banana', 'mango_apple_banana', 'apple_banana' ]
const array2 = [ 'apple', 'mango' ]
const goal = [ 'apple_mango_banana', 'mango_apple_banana' ]
let result = array1.filter(item1 => array2.every(item2 => item1.includes(item2)))
console.log(result)

How to add an property to an existing object inside an array

For example this is the original array
[
{name:xyz,id:123 },
{name:abc,id:232},
]
Now here is an another array
[
{value:'anything'},
{value:'anything12312'}
]
Now in new array or original array the output should be this
[
{value:'anything',name:xyz,id:123},
{value:'anything12312', name:abc,id:232}}
]
How can I achieve this
you mean like this ??
Use map,index and spread as :
let a = [
{name:"xyz",id:123 },
{name:"abc",id:232},
]
let b = [
{value:'anyrhing'},
{value:'anything12312'}
]
let res = a.map((el,idx)=> ({...el,...b[idx]}));
console.log(res)
Use object destructuring.
const arr1 = [
{name:'xyz',id:123 },
{name:'abc',id:232},
]
const arr2 = [
{value:'anyrhing'},
{value:'anything12312'}
]
const arr3 = [];
for(let i=0;i<arr1.length;i++)
arr3.push({...arr2[i],...arr1[i]});
A1=[
{name:"xyz",id:123 },
{name:"abc",id:232},
];
A2=[
{value:'anyrhing'},
{value:'anything12312'}
];
obj1={
...A2[0],
...A1[0]}
//another method to merge objects
obj2=Object.assign(A2[1],A1[1]);
//The result you needed
A3=[obj1,obj2];

Flatten an array grouped by nested elements in JavaScript

I have a tricky question... I have an array looks like this:
[
[ [ 'Attribute1', 'Attribute1Synonym1' ], [ 'Attribute2' ] ],
[ [ 'Attribute3' ] ],
[ [ 'Attribute2' ] ]
]
My result should be:
[
'Attribute1 Attribute2',
'Attribute1Synonym1 Attribute2',
'Attribute3',
'Attribute2'
]
The tricky thing is:
the result array has to grouped by the sub-sub-array
the crux is, the first index is an array(1) of arrays(2) of arrays(3)
and i would to like flatten the array by level 3 (array(3)) and at the result should be every possible combination between the upper level.
At level 2 (the first index) is an array with ('Attribute1' and 'Attribute1Synonym1')
so the result should be:
'Attribute1 Attribute2'
and
'Attribute1Synonym1 Attribute2'
the 'Attribute2' comes from the upper level
if the second index of level 2 ['Attribute2'] has also multiple indexes
for example ['Attribute2Synonym5']
the result should be:
'Attribute1 Attribute2'
'Attribute1Synonym1 Attribute2'
'Attribute1 Attribute2Synonym5'
'Attribute1Synonym1 Attribute2Synonym5'
and so on...
This works against your provided example, but I'm going to guess it's fragile against more complex arrays:
const deep = [ [ [ 'Attribute1', 'Attribute1Synonym1' ], [ 'Attribute2' ] ],
[ [ 'Attribute3' ] ],
[ [ 'Attribute2' ] ] ];
const flat = [];
deep.forEach(element => {
const left = element[0];
const right = element[1];
left.forEach(leftElement => {
if(right){
right.forEach(rightElement => {
flat.push(leftElement + ' ' + rightElement);
});
} else {
flat.push(leftElement);
}
})
});
Maybe like this:
var input_arr=[ [ [ 'Attribute1', 'Attribute1Synonym1' ], [ 'Attribute2' ] ],
[ [ 'Attribute3' ] ],
[ [ 'Attribute2' ] ] ];
var output_arr=[];
for(var key1 in input_arr){
var sub_input_arr=input_arr[key1];
for(var key2 in sub_input_arr){
var sub_sub_input_arr=sub_input_arr[key2];
for(var key3 in sub_sub_input_arr){
output_arr.push(sub_sub_input_arr[key3]);
}
}
}
console.log(output_arr);

Categories