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

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];

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] ] ];

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 combine 3 multidimentional arrays using GAS?

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]]]);
}

Covert key-value javascript object into array format

I am trying to access java script object which is available in the below format:
[
{'To':'A','From':'X','Weight':5},
{'To':'A','From':'Y','Weight':7},
{'To':'A','From':'Z','Weight':6},
{'To':'B','From':'X','Weight':2},
{'To':'B','From':'Y','Weight':9},
{'To':'B','From':'Z','Weight':4}
]
How can I access above object to create array like below ?
[
[ 'A', 'X', 5 ],
[ 'A', 'Y', 7 ],
[ 'A', 'Z', 6 ],
[ 'B', 'X', 2 ],
[ 'B', 'Y', 9 ],
[ 'B', 'Z', 4 ]
]
You can
Use Array.map() and Object.values():
const arrOfArrs = arr.map( Object.values );
Use Lodash's _.map() and _.values() in the same manner:
const _ = require('lodash');
. . .
const arrOfArrs = _.map( arr , _.values );
You should, however, be aware that the order in which an object's properties are iterated over (and hence returned) is not guaranteed in any way by the Ecmascript/Javascript standard. It can vary from Javascript implementation to implementation, and can even change from execution to execution. The most common order in which things are returned is insertion order, but that's not guaranteed.
Use a map function:
const l = [
{'To':'A','From':'X','Weight':5},
{'To':'A','From':'Y','Weight':7},
{'To':'A','From':'Z','Weight':6},
{'To':'B','From':'X','Weight':2},
{'To':'B','From':'Y','Weight':9},
{'To':'B','From':'Z','Weight':4}
]
const newArray = l.map(el => [el.To, el.From, el.Weight])
console.log(newArray);
Well, you can use array method .map()
let arr = [
{'To':'A','From':'Y','Weight':7},
{'To':'A','From':'Z','Weight':6}
]
let result = arr.map(Object.values)
console.log(result)
let a = [{'To':'A','From':'X','Weight':5},{'To':'A','From':'Y','Weight':7},{'To':'A','From':'Z','Weight':6},{'To':'B','From':'X','Weight':2},{'To':'B','From':'Y','Weight':9},{'To':'B','From':'Z','Weight':4}]
let array = []
for(i in a){
let temp = [a[i]['To'], a[i]['From'], a[i]['Weight']]
array.push(temp)
}
console.log(array)
You could use this to ensure the correct order of items in inner array.
let arr = [{'To':'A','From':'X','Weight':5},{'To':'A','From':'Y','Weight':7},{'To':'A','From':'Z','Weight':6},{'To':'B','From':'X','Weight':2},{'To':'B','From':'Y','Weight':9},{'To':'B','From':'Z','Weight':4}]
let result = arr.map(obj => [obj.To, obj.From, obj.Weight]);
console.log(result);

How to get a specific array from a list of arrays

I want to get a specific array based on a code, the array is something like this:
const arr = [
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=11f18ac1-0476-4a1e-ada6-09e6566abc19",
1595619171842,
"0b7ad06f-7776-4bab-a8c6-53fd5fd5bd9b"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=b64c143d-e817-434f-bf6f-0bd0e8d9e7b5",
1595619171844,
"2f44a130-71d9-47ce-b5d5-04587c3c81fc"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=71dc5d26-75f4-4141-905e-074b0705eac4",
1595619171845,
"d7eb2a05-1f5a-48dd-b7ac-f3b071499d00"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=d3645614-0ea3-4d17-80ab-57c6c6525fab",
1595619171846,
"940fb9a7-6fdd-4f8b-a808-26a9c60114bf"
]
];
How to I get the array with code "d7eb2a05-1f5a-48dd-b7ac-f3b071499d00"?
I was using reduce to get the more recent image, but now I have no idea!
Array#find returns the first elmenent of an array that returns true for the given function.
const specificArray = arr.find(subArray => {
return subArray[2] === "d7eb2a05-1f5a-48dd-b7ac-f3b071499d00";
}
Array#find (with destructuring) is best suited for this purpose.
const res = arr.find(([,,code])=>code==="d7eb2a05-1f5a-48dd-b7ac-f3b071499d00");
const arr = [
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=11f18ac1-0476-4a1e-ada6-09e6566abc19",
1595619171842,
"0b7ad06f-7776-4bab-a8c6-53fd5fd5bd9b"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=b64c143d-e817-434f-bf6f-0bd0e8d9e7b5",
1595619171844,
"2f44a130-71d9-47ce-b5d5-04587c3c81fc"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=71dc5d26-75f4-4141-905e-074b0705eac4",
1595619171845,
"d7eb2a05-1f5a-48dd-b7ac-f3b071499d00"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=d3645614-0ea3-4d17-80ab-57c6c6525fab",
1595619171846,
"940fb9a7-6fdd-4f8b-a808-26a9c60114bf"
]
];
const res = arr.find(([,,code])=>code==="d7eb2a05-1f5a-48dd-b7ac-f3b071499d00");
console.log(res);

Categories