I would like to add the values of two JavaScript arrays that have the same length to get a third array so that the the first value of the third array is the sum of the first values of the two first arrays, the second value of the third array is the sum of the second values of the two first arrays, etc. For example:
var array1 = [1,2,3];
var array2 = [4,1,0];
var array3 = array1 + array2;
I would like the result of array3 to be [1+4, 2+1, 3+0] = [5,3,3].
This is not the same question as this. I would like to add the numbers and not make sub-arrays.
I know that you can do for(i = 0; i < array1.length; i++){array3[i] = array1[i] + array2[i]} but I would like to know if there is a built-in code that does this.
Use Array#map() method
var array1 = [1, 2, 3];
var array2 = [4, 1, 0];
var array3 = array1.map(function(v, i) {
return v + array2[i];
})
console.log(array3);
For latest browser use it with ES6 arrow function
var array1 = [1, 2, 3];
var array2 = [4, 1, 0];
var array3 = array1.map((v, i) => v + array2[i])
console.log(array3);
For older browser check polyfill option of map method.
You should use generators.
function *addPairwise(a1, a2) {
let i1 = a1[Symbol.iterator](), i2 = a2[Symbol.iterator](), x1, x2;
while (1) {
x1 = i1.next();
x2 = i2.next();
if (x1.done && x2.done) return;
yield x1.done ? x2.value : x2.done ? x1.value : x1.value + x2.value;
}
}
Normally we would prefer to simply do a for...of loop over an iterable, but there's no way to do that in parallel over two iterables, so we need to get the iterators so we can use the next method on them.
This approach will allow you to do pairwise addition of any iterable including infinite streams.
console.log([...addPairwise([1,2,3], [4,1,0])]);
If you prefer you could define addPairwise to take iterators directly and call it as
addPairwise([1, 2, 3].values(), [4, 1, 0].values())
This assumes suitable support for ES6 such as node v6, Chrome 51, or babel.
var array1 = [1,2,3];
var array2 = [4,1,0];
var array3 = add(array1, array2);
console.log(array3);
function add(arr1, arr2) {
return arr1.map(function(value, index) {
return value + arr2[index];
});
}
For IE8 support:
var array1 = [1,2,3];
var array2 = [4,1,0];
var array3 = add(array1, array2);
console.log(array3);
function add(arr1, arr2) {
var newArray = [];
for(var i = 0; i < arr1.length; i++){
newArray.push(arr1[i] + arr2[i]);
}
return newArray;
}
Related
I know this is a very basic question, but I am not able to write the correct code for this. Let's say I have 3 arrays:
arr1 = [1,2,3,4]
arr2 = [5,6,7,8]
arr3 = [7,3,2,1]
I want the sum of the last index, i.e. 4+ 8 + 1 = 13
I am not able to write the correct for loop for the same.
EDIT :
My array is inside an object as shown below:
So, I will have more arrays inside the object. So how do I proceed with this?
let ree = {
arr1: [1, 2, 3, 4],
arr2: [5, 6, 7, 8],
arr3: [7, 3, 2, 1]
};
let total = 0;
for (var key in ree) {
if (Array.isArray(ree[key])) {
total += ree[key][ree[key].length - 1];
}
}
console.log(total);
try this short and easy solution
let arr1 = [1,2,3,4]
let arr2 = [5,6,7,8]
let arr3 = [7,3,2,1]
arrList = [arr1,arr2,arr3]
finalSum = arrList.reduce((acc, cur) => acc+[...cur].pop(), 0)
i created a copy of arrays in reduce as pop modifies original array.
if your array is inside object as key value pair you can get it as array like this
let yourObject = {1:arr1, 2:arr2, 3:arr3}
let arrList = Object.values(yourObject);
an array assigned to a key in an object can be accessed like this
let yourObject = {'somekey':[1,2,3]};
arrList = yourObject.somekey;
console.log(arrList);
You can do something like this using array.length property,
sum = arr1[arr1.length - 1] + arr2[arr2.length - 1] + arr3[arr3.length - 1];
If they are inside another array you can do something like this,
let parentArr = [[1,2,3,4],[5,6,7,8],[7,3,2,1]];
let sum = 0;
for(let i =0;i<parentArr.length; i++) {
sum = sum + parentArr[i][parentArr[i].length - 1];
}
console.log(sum);
If the arrays are inside an object you can do the following,
let parentObj = {'arr1': [1,2,3,4], 'arr2': [5,6,7,8], 'arr3': [7,3,2,1]};
let sum = 0;
let keys = Object.keys(parentObj);
for(let i =0;i<keys.length; i++) {
sum = sum + parentObj[keys[i]][parentObj[keys[i]].length - 1];
}
console.log(sum);
Basic thing to know before we continue :
how many arrays you have ?
can array be empty ?
if number of arrays is static i.e. 3 and they can't be empty then use sabir.alam's
else please define your requirements.
Shorter answer:
const parentArr = [[1,2,3,4],[5,6,7,8],[7,3,2,1]];
const answer = parentArr.map(e=>e[e.length-1]).reduce((x,y)=>x+y);
console.log(answer);
Initial code: var ar = [1,2,3,4,5,6]
I want to shift starting from second element and add last element. I try to using shift() and splice() but doesn't work and maybe something wrong on my code.
any idea to fix it?
thank you
var ar = [1,2,3,4,5,6];
ar.splice(5,1,"0").shift();
console.log(ar)
expected result [1,3,4,5,6,0]
Maybe a little slice and concat?
var ar = [1,2,3,4,5,6];
var result = ar.slice(0, 1).concat(ar.slice(2)).concat(0)
console.log(result)
Or maybe some destructuring:
const [first, , ...next] = [1,2,3,4,5,6]
console.log([first, ...next, 0])
var ar = [1,2,3,4,5,6];
ar.splice(1,1)
ar.push(0)
console.log(ar)
you can try ES6 rest operator
const array = [1,2,3,4,5]
const [firstItem, ...otherArray] = array
const newArray = [ ...otherArray, 0]
// or direct array assign
// const [firstItem, ...otherArray] = [1,2,3,4,5]
console.log(newArray)
var array = [1, 2, 3, 4, 5, 6];
function shift(arr, start, newElement) {
const arr1 = []
const rest = [];
for (let i = 0; i < start; i++) {
arr1.push(arr[i]);
}
for (let i = start + 1; i < arr.length; i++) {
rest.push(arr[i]);
}
rest.push(newElement)
return [...arr1, ...rest]
}
console.log(shift(array, 1, "0"))
console.log(shift(array, 2, "0"))
I have 3 arrays:
array1 = [ 'A', 'B', 'A', 'B']
array2 = [ 5, 5, 7, 5]
array3 = [true,true,true,true]
I was wondering if there is any easy way (maybe with lodash) to eliminate the duplicates and end with this:
array1 = [ 'A', 'B', 'A']
array2 = [ 5, 5, 7]
array3 = [true,true,true]
I know I can do a function and compare the previous value, but is there a more clever way to do it?
Update
Please note that I don't need to eliminate the duplicates of each array.
What I looking is a way to eliminate the duplicates "vertically"
Update 2
Please note that each "column" is a record.
record1 = ['A',5,true]
record2 = ['B',5,true]
record3 = ['A',7,true]
record1 = ['B',5,true]
TL;DR
const records = array1.map((a, i) => [a, array2[i], array3[i]]);
const index = {};
records.filter(column => {
const key = JSON.stringify(column);
return key in index ? false : index[key] = true;
});
Huh?
There are a lot of ways to solve this, with varying degrees of efficiency, and the best solution will depend on the size of your data. A simple but naΓ―ve solution iterates over each "column" and checks all of the preceding columns for equality. It looks like this:
const array1 = [ 'A', 'B', 'A', 'B'];
const array2 = [ 5, 5, 7, 5];
const array3 = [true,true,true,true];
const newArray1 = array1.slice(0,1); // column 0 is never duplicate
const newArray2 = array2.slice(0,1);
const newArray3 = array3.slice(0,1);
// loop over columns starting with index 1
outer: for (let i = 1; i < array1.length; i++) {
const a = array1[i];
const b = array2[i];
const c = array3[i];
// check all preceding columns for equality
for (let j = 0; j < i; j++) {
if (a === array1[j] && b === array2[j] && c === array3[j]) {
// duplicate; continue at top of outer loop
continue outer;
}
}
// not a duplicate; add to new arrays
newArray1.push(a);
newArray2.push(b);
newArray3.push(c);
}
console.log(newArray1);
console.log(newArray2);
console.log(newArray3);
.as-console-wrapper{min-height:100%}
As you can see, we have to check each row within each column for equality, every time. If you're curious, the complexity of this is π(π(π+1)/2) (technically π(ππ(π+1)/2), where π is 3 for three columns).
For a larger data sets it's advantageous to keep track of values you've already seen in a data structure that's quick to access: A hash, a.k.a. a JavaScript object. Since all of your values are primitive, a quick way to construct a key is JSON.stringify. Some might consider this a "hack"βand it's important to note that it will fail with values that can't be represented in JSON, e.g. Infinity or NaNβbut it's a fast and easy one with data this simple.
const array1 = ['A', 'B', 'A', 'B'];
const array2 = [5, 5, 7, 5];
const array3 = [true, true, true, true];
const newArray1 = [];
const newArray2 = [];
const newArray3 = [];
const index = {};
for (let i = 0; i < array1.length; i++) {
const a = array1[i];
const b = array2[i];
const c = array3[i];
const key = JSON.stringify([a,b,c]);
if (key in index) {
// duplicate; skip to top of loop
continue;
}
// not a duplicate; record in index and add to new arrays
index[key] = true;
newArray1.push(a);
newArray2.push(b);
newArray3.push(c);
}
console.log(newArray1);
console.log(newArray2);
console.log(newArray3);
.as-console-wrapper{min-height:100%}
The complexity of this is π(π), or maybe π(2ππ) where π, again,
is 3 for three columns, and the 2 is another π to very roughly account for the cost of JSON.stringify. (Figuring out the cost of hash access is left as an exercise for the pedants among us; I'm content to call it π(1).)
That's still pretty verbose. Part of the reason is that using three different variables for the dataβwhich is really a single "table"βleads to a lot of repetition. We can preprocess the data to make it easier to deal with. Once it's "transposed" into a single two-dimensional array, we can use Array.prototype.filter with the key technique from above, for some very terse code:
const array1 = ['A', 'B', 'A', 'B'];
const array2 = [5, 5, 7, 5];
const array3 = [true, true, true, true];
// turn "columns" into "rows" of a 2D array
const records = array1.map((a, i) => [a, array2[i], array3[i]]);
const index = {};
const newData = records.filter(column => {
const key = JSON.stringify(column);
return key in index ? false : index[key] = true;
});
console.log(newData);
.as-console-wrapper{min-height:100%}
Of course, pre-processing isn't free, so this code isn't any more performant than the more verbose version; you'll have to decide how important that is to you. If you want you can now extract the columns from newData into three variables (newData.forEach(([a,b,c]) => { newArray1.push(a); newArray2.push(b); /* ... */ })), but for many purposes the "transposed" 2D array will be easier to work with.
You can use ES6 Set https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
Set -> lets you store unique values of any type, whether primitive values or object references.
and then convert back to an array
check this snippet
const array1 = ['A','B','A','B']
const array2 = [5,5,7,5]
const array3 = [true,true,true,true]
const uniqA1= new Set(array1)
const uniqA2= new Set(array2)
const uniqA3= new Set(array3)
console.log(Array.from(uniqA1))
console.log(Array.from(uniqA2))
console.log(Array.from(uniqA3))
Hope it helps
You need to find duplicate elements with same indexes in all arrays and then filter out those elements.
var array1 = ['A', 'B', 'A', 'B'],
array2 = [5, 5, 7, 5],
array3 = [true, true, true, true];
var dupes = []
var arrays = [array1, array2, array3];
arrays.forEach(function(arr, i) {
arr.forEach((e, j) => !this[e] ? this[e] = true : dupes[i] = (dupes[i] || []).concat(j))
}, {})
var index = dupes[0].filter(e => dupes.every(a => a.includes(e)))
var result = arrays.map(e => e.filter((a, i) => !index.includes(i)))
console.log(result)
You're going to need a couple of helper functions (lodash provides them also):
let zip = (...arys) => arys[0].map((_, i) => arys.map(a => a[i]));
let uniq = (ary, key) => uniq2(ary, ary.map(key), new Set);
let uniq2 = (ary, keys, set) => ary.filter((_, i) => !set.has(keys[i]) && set.add(keys[i]))
// test
var array1 = ['A', 'B', 'A', 'B'];
var array2 = [5, 5, 7, 5];
var array3 = [true, true, true, true];
var [x, y, z] = zip(
...uniq(
zip(array1, array2, array3),
JSON.stringify
)
);
console.log(x, y, z)
Another way, with filter():
array1 = ['A', 'B', 'A', 'B'];
array2 = [5, 5, 7, 5];
array3 = [true, true, true, true];
uniqueArray1 = array1.filter(function(item, pos) {
return array1.indexOf(item) == pos;
})
uniqueArray2 = array2.filter(function(item, pos) {
return array2.indexOf(item) == pos;
})
uniqueArray3 = array3.filter(function(item, pos) {
return array3.indexOf(item) == pos;
})
console.log(uniqueArray1);
console.log(uniqueArray2);
console.log(uniqueArray3);
One method I can think of is using an object to keep track, which will also coincidentally remove any duplicates as keys have to be unique. The only thing is I can think of how to extract it back into an array for now. I will think about it tomorrow.
This utilizes jquery for deep cloning. If you want it only in vanilla javascript, you could probably just implement a deep clone function.
var array1 = [ 'A', 'B', 'A', 'B'];
var array2 = [ 5, 5, 7, 5];
var array3 = [true,true,true,true];
all_arrays = [array1, array2, array3];
let obj = {};
for (let i = 0; i < all_arrays[0].length; i++)
{
let new_obj = recursive_objects(all_arrays, 0, i)
$.extend(true, obj, new_obj);
}
console.log(obj);
function return_array(array, temp_obj)
{
let keys = Object.keys(temp_obj);
for (let key of keys)
{
}
}
function recursive_objects(arrays, arrays_index, index)
{
let obj = {}
if (arrays_index < arrays.length)
{
obj[arrays[arrays_index][index]] = recursive_objects(arrays, ++arrays_index, index);
}
return obj;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
I am splitting an array into two arrays arr1 & arr2. Then I want to sum the values and bind into another array like arr3. How can I do this?
var arr1 = [1,2,3,4]
var arr2 = [2,3,4,4]
var arr3 = [3,5,7,8]
this works fine with static arrays, but my problem is
var TotalOfArray = [];
var temparray = [];
for (i = 0, j = x.length; i < j; i += chunk) {
temparray = x.slice(i, i + chunk);
if (temparray.length == chunk) {
console.log("before loop: "+TotalOfArray.length);
if (TotalOfArray.length == 0) {
for (i in temparray) {
TotalOfArray[i] = temparray[i];
}
console.log("after loop: "+TotalOfArray.length);
} else {
for (i in temparray) {
TotalOfArray[i] = TotalOfArray[i] + temparray[i];
}
}
}
}
As you can see, x will be the main array which I am splicing into a temparray array, so every time it will splice with array length 31 and than I want to do sum, chunk = 31 as of now. But it's not going into ELSE part.
Equal Length Arrays:
This is just a more simple version, that assumes equal lengths from both arrays. For a solution that works with variable length arrays, read further on.
var arr1 = [1, 2, 3, 4];
var arr2 = [2, 3, 4, 4];
var arr3 = arr1.map(function(a, i) {
return a + arr2[i];
});
console.log(arr3);
Variable Length Arrays:
This is going to be a more robust solution regardless. But it finds the array with the most values in it, and uses that for the map. Then, if undefined values are found in the other array, it will use 0 as the other number.
var arr1 = [1, 2, 3, 4];
var arr2 = [2, 3, 4, 4, 5];
var sorted = [arr1, arr2].sort((a, b) => b.length - a.length);
var arr3 = sorted[0].map(function(a, i) {
return a + (sorted[1][i] || 0);
});
console.log(arr3);
Use for loop:
var arr1 = [1,2,3,4]
var arr2 = [2,3,4,4]
var arr3 = []
for (i in arr1) { arr3[i] = arr1[i] + arr2[i]; }
console.log(arr3)
If two arrays have equal length you can can use Array.prototype.map();
var data1 = [1,2,3,4],
data2 = [2,3,4,4],
result;
result = data1.map(function(value, i){
return value + data2[i];
});
console.log(result);
var arr1 = [1,2,3,4]
var arr2 = [2,3,4,4]
var arr3 = []
for (var i = 0; i < arr1.length; i++) {
arr3.push(arr1[i] + arr2[i])
}
Assuming arr1 and arr2 have the same length.
So I have these two questions considering the JavaScript language:
Is there any way to append to an array without using the push() function or any other built in functions in the language?
Is there any way to merge two arrays together without using the concat() function or any other built in functions in the language?
For the first part you can always use the length property of the array, to add the next element:
a = ['a', 'b', 'c', 'd'];
a[a.length] = 'e';
// a is now ["a", "b", "c", "d", "e"]
To do the latter, merge the arrays, without a function you can just loop thru the arrays, should pick the largest one to loop on. But yeah, as the comments state. There's usually not a good reason to do so.
Here are the alternatives for you:
To add the item to the array without push call:
arr[arr.length] = value;
To concatenate one array to another without concat call:
for (var i = 0; i < arr2.length; arr1[arr1.length] = arr2[i++]);
Not sure if this is what you are looking for or why but arr[arr.length] = 1; is the answer both of your questions.
var myArr = [];
myArr[myArr.length] = 1;
myArr[myArr.length] = 2;
myArr[myArr.length] = 3;
var myArr1 = [...]; // has items;
var myArr2 = [...]; // has items;
var mergedArr = [];
for(var i = 0; i < myArr1.length){
mergedArr[mergedArr.length] = myArr1[i];
}
for(var i = 0; i < myArr2.length){
mergedArr[mergedArr.length] = myArr2[i];
}
Yes, you can do so without using .push, concat, or .length. ES6 allows the use of the spread syntax (...):
var arr = [1, 2]; // arr is 1, 2
var arr = [...arr, 3]; // arr is now 1, 2, 3
let arr1 = [1, 2];
let arr2 = [3, 4];
arr = [...arr1, ...arr2]; // arr is now 1, 2, 3, 4
function pop(arr) {
let finalVar = arr[arr.length - 1];
arr.length = arr.length - 1;
return finalVar;
}
I hope this can solve your problem:
const addItem = (arr, ...arguments) => {
for (let i = 0; i < arguments.length; i++) {
arr[arr.length] = arguments[i];
}
return arr;
};
let myArr = [];
console.log(addItem(myArr, 'Chaewon')); // ['Chaewon'])
console.log(addItem(myArr, 'Liz')); // ['Chaewon', 'Liz']);
<html lang="en">
<head>
<script>
let data = [20, 80, 79, 21, 40];
let newEl = 26;
let position = 2;
console.log(data);
for(let i = data.length-1; i >= 0; i--){
console.log(data[i]);
if (i >= position) {
data[i + 1]= data[i];
if (i === position) {
data[i]= newEl;
}
}
}
console.log(data)
</script>
</head>
<body>
</body>
</html>