I have two arrays that I am trying to bine in GAS, arr2 is multidimensional.
arr1 = ["Diesel", "Solar", "Biomass"]
arr2 = [
["ABC", "Nigeria", "Diesel,Solar", 35],
["DEF", "Egypt", "Solar,Diesel", 50],
["GHI", "Ghana", "Biomass,Diesel", 70]
]
What I want to do is push the elements of arr1 into arr2 at index 3 in each row, so it looks like:
newArr = [
["ABC", "Nigeria", "Diesel,Solar", "Diesel", 35],
["DEF", "Egypt", "Solar,Diesel", "Solar", 50],
["GHI", "Ghana", "Biomass,Diesel", "Biomass", 70]
]
I have tried to use .map over arr2 to .Splice each row but couldn't get it to work. Any help would be much appreciated!
I have two arrays that I am trying to bine in GAS, arr2 is multidimensional.
arr1 = ["Diesel", "Solar", "Biomass"]
arr2 = [
["ABC", "Nigeria", "Diesel,Solar", 35],
["DEF", "Egypt", "Solar,Diesel", 50],
["GHI", "Ghana", "Biomass,Diesel", 70]
]
What I want to do is push the elements of arr1 into arr2 at index 3 in each row, so it looks like:
newArr = [
["ABC", "Nigeria", "Diesel,Solar", "Diesel", 35],
["DEF", "Egypt", "Solar,Diesel", "Solar", 50],
["GHI", "Ghana", "Biomass,Diesel", "Biomass", 70]
]
I have tried to use .map over arr2 to .Splice each row but couldn't get it to work. Any help would be much appreciated!
Share Improve this question asked Apr 9, 2019 at 12:04 Ajay UbhiAjay Ubhi 4093 gold badges7 silver badges15 bronze badges 3- 3 Possible duplicate of How to insert an item into an array at a specific index (JavaScript)? – messerbill Commented Apr 9, 2019 at 12:06
- What did you try? Please share your code @Ajay – Niraj Kaushal Commented Apr 9, 2019 at 12:08
- So I did try the .Splice method in the thread above. However, I was having trouble understanding how to do it in a multidimensional array. – Ajay Ubhi Commented Apr 9, 2019 at 12:15
4 Answers
Reset to default 4Using array.splice()
and array.map()
Syntax
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
let arr1 = ["Diesel", "Solar", "Biomass"]
let arr2 = [
["ABC", "Nigeria", "Diesel,Solar", 35],
["DEF", "Egypt", "Solar,Diesel", 50],
["GHI", "Ghana", "Biomass,Diesel", 70]
]
let newArr = arr2.map((v, i) => v.splice(3, 0, arr1[i]) && v)
console.log(newArr)
You can use Array#map
and Array#splice
methods.
let arr1 = ["Diesel", "Solar", "Biomass"],
arr2 = [
["ABC", "Nigeria", "Diesel,Solar", 35],
["DEF", "Egypt", "Solar,Diesel", 50],
["GHI", "Ghana", "Biomass,Diesel", 70]
];
// iterate over the array
let res = arr2.map((arr, i) => {
// copy array values to a new array
let newA = [...arr];
// insert new element into array based on index
newA.splice(3, 0, arr1[i]);
// return new array
return newA;
})
console.log(res)
If you want to mutate original array then you can skip the array copying portion and just Array#splice
method is enough.
let arr1 = ["Diesel", "Solar", "Biomass"],
arr2 = [
["ABC", "Nigeria", "Diesel,Solar", 35],
["DEF", "Egypt", "Solar,Diesel", 50],
["GHI", "Ghana", "Biomass,Diesel", 70]
];
// iterate over the array and put the value at specific index
arr2.forEach((arr, i) => arr.splice(3, 0, arr1[i]))
console.log(arr2)
Try using the following code
arr1 = ["Diesel", "Solar", "Biomass"]
arr2 = [
["ABC", "Nigeria", "Diesel,Solar", 35],
["DEF", "Egypt", "Solar,Diesel", 50],
["GHI", "Ghana", "Biomass,Diesel", 70]
]
arr2.forEach((subArr, index) => {console.log(subArr.splice(3, 0, arr1[index]));});
console.log(arr2);
With a forEach
. Use map
if you don't want to mutate the array.
const arr1 = ['Diesel', 'Solar', 'Biomass'];
const arr2 = [
['ABC', 'Nigeria', 'Diesel,Solar', 35],
['DEF', 'Egypt', 'Solar,Diesel', 50],
['GHI', 'Ghana', 'Biomass,Diesel', 70]
];
arr2.forEach((el, i) => {
el.splice(3, 0, arr1[i]);
});
console.log(arr2);