I need a hand to sum the value of array elements with the previous element(s) and return a new array.
So if we have :
let durations = [4, 3.5, 6];
then in the new array the first element is 4, the second element would be the sum of 4 + 3.5 and the third one would be 4 + 3.5 + 6; so the desired result would be [4, 7.5, 13.5]
So far it seems that reduce unexpectedly just concat the numbers and returns an array of strings !!!
let durations = [4, 3.5, 6];
let arr = [];
let durationsNew = durations.reduce((a, b) => {
arr.push(a + b);
return arr;
}, []);
console.log(durationsNew); // The desired result is [4, 7.5, 13.5]
In your code, you take the accumulator a and add the value to it. The accumulator is an array and this is converted to string by using it with a plus operator.
Instead, you could take a variable for sum and map the sum by adding the value for each element.
let durations = [4, 3.5, 6],
sum = 0,
array = durations.map(value => sum += value)
console.log(array); // [4, 7.5, 13.5]
Try this - a mixture of map() and reduce():
[4, 3.5, 6].map((num, i, arr) =>
num + arr.slice(0, i).reduce((a, b) =>
a + b, 0)); //[4, 7.5, 13.5]
The idea is to map the array to a new array, and for each number, the new number returned is the number + the sum of the array values up to that number.
If you are going to use reduce, you have to do something like this:
let durations = [4, 3.5, 6]
let durationsNew = durations.reduce((_durationsNew, duration, durationIndex) => {
if(durationIndex > 0) {
_durationsNew.push(_durationsNew[durationIndex - 1] + duration)
} else {
_durationsNew.push(duration)
}
return _durationsNew;
}, [])
console.log(durationsNew); // The desired result is [4, 7.5, 13.5]
Example: https://repl.it/repls/HandsomeVacantRate
Benchmark test with Array.map, Array.reduce, and for loop:
Related
I have some array of numbers:
var arr = [1, 7, 1, 4];
I want to increase only every first value, such that the expected output would be: [2, 7, 2, 4]
I tried some combination of map and filter but I don't understand how it can work together...
var mapuj = arr.map(x => x *2);
You can use map() and use second argument which is idnex to determine if it's at event index or not
let arr = [1, 7, 1, 4];
let output = arr.map((n, index) => index % 2 === 0 ? n * 2 : n);
console.log(output);
Given an array of integers, where the values should be sorted in the following order:
if we have an array
[1, -1, -3, 9, -2, -5, 4, 8,]
we must rearrange it this way: largest number, smallest number, 2nd largest number, 2nd smallest number, ...
[9, -5, 8, -3, 4, -2, 1, -1 ]
I get the first largest and smallest numbers, but can't figure out how to make it dynamic for all values in the array.
I know that I must take two variables, say firstSmallest and firstLargest and point them to the first and last index of the array respectively, run a loop, which I do already in the code below, and store value into new array by incrementing firstSmallest and decrementing firstLargest, but couldn't implement into code.
let unsortedArr = [1, 5, 8 , 7, 6, -1, -5, 4, 9, 5]
let output = [];
function meanderArray(unsorted){
let sorted = unsorted.sort((a, b) => a-b);
let firstSmallest = sorted[0];
let firstLargest = sorted[unsorted.length-1];
for(let i = 0; i <= sorted.length; i++){
//I should increment firstSmallest and decrement firstLargest numbers and store in output
}
return output;
}
meanderArray(unsortedArr);
console.log(output);
You could take a toggle object which takes the property of either the first item or last from an array and iterate until no more items are available.
function meanderArray([...array]) {
const
result = [],
toggle = { shift: 'pop', pop: 'shift' };
let fn = 'shift';
array.sort((a, b) => a - b);
while (array.length) result.push(array[fn = toggle[fn]]());
return result;
}
console.log(...meanderArray([1, 5, 8, 7, 6, -1, -5, 4, 9, 5]));
You can sort an array by descending, then logic is the following: take first from start and first from end, then second from start-second from end, etc.
let unsortedArr = [1, 5, 8 , 7, 6, -1, -5, 4, 9, 5]
let output = [];
function meanderArray(unsorted){
let sorted = unsorted.sort((a, b) => b-a);
let output = []
for(let i = 0; i < sorted.length/2; i++){
output.push(sorted[i])
if(i !== sorted.length - 1 - i){
output.push(sorted[sorted.length - 1 - i])
}
}
return output;
}
let result = meanderArray(unsortedArr);
console.log(result);
You can sort, then loop and extract the last number with pop() and extract the first number with shift().
let unsortedArr = [1, -1, -3, 9, -2, -5, 4, 8,]
let output = [];
function meanderArray(unsorted){
let sorted = unsorted.sort((a, b) => a - b);
for(let i = 0; i < unsortedArr.length + 2; i++){
output.push(sorted.pop());
output.push(sorted.shift());
}
console.log(output);
return output;
}
meanderArray(unsortedArr);
Fastest Meandering Array method among all solutions mentioned above.
According to the JSBench.me, this solution is the fastest and for your reference i have attached a screenshot below.
I got a different approach, but i found that was very close to one of above answers from elvira.genkel.
In my solution for Meandering Array, First I sorted the given array and then i tried to find the middle of the array. After that i divided sorted array in to two arrays, which are indices from 0 to middle index and other one is from middle index to full length of sorted array.
We need to make sure that first half of array's length is greater than the second array. Other wise when applying for() loop as next step newly created array will contains some undefined values. For avoiding this issue i have incremented first array length by one.
So, always it should be firstArr.length > secondArr.length.
And planned to create new array with values in meandering order. As next step I created for() loop and try to push values from beginning of the first array and from end of the second array. Make sure that dynamically created index of second array will receive only zero or positive index. Other wise you can find undefined values inside newly created Meandering Array.
Hope this solution will be helpful for everyone, who loves to do high performance coding :)
Your comments and suggestions are welcome.
const unsorted = [1, 5, 8, 7, 6, -1, -5, 4, 9, 5];
const sorted = unsorted.sort((a,b)=>a-b).reverse();
const half = Math.round(Math.floor(sorted.length/2)) + 1;
const leftArr = sorted.slice(0, half);
const rightArr = sorted.slice(half, sorted.length);
const newArr = [];
for(let i=0; i<leftArr.length; i++) {
newArr.push(leftArr[i]);
if (rightArr.length-1-i >= 0) {
newArr.push(rightArr[rightArr.length-1-i]);
}
}
I am trying to get the indexes of the 5 smallest values in an array.
I've tried the code below, however it gives the error: Math.min.apply(...).indexOf is not a function. I'm just not sure what I can use as an alternative?
var smallest = [];
for (var i = 0, length = distances.length; i < length; i++) {
var min = Math.min.apply(null,distances).indexOf();
if (smallest.length <= 5) {
smallest.push(min);
}
console.log(smallest);
}
Thanks!
You could get the keys, sort them with the values and take the top five.
var indices = [...distances.keys()]
.sort((a, b) => distances[a] - distances[b])
.slice(0, 5);
You can use Object.entries() to get [index, value] pairs that you can now sort by value to get the order.
const distances = [1, 4, 8, 3, 3, 5, 9, 0, 4, 2];
const indices = Object.entries(distances)
.sort(([,a],[,b]) => a - b)
.map(([index]) => +index)
.slice(0, 5)
console.log(indices);
Nina's version is better :)
You could add an index to each element, sort the data in ascending order, then splice the first 5 values:
const data = [1, 4, 8, 3, 3, 5, 9, 0, 4, 2]
const indices = data.map((e,i) => [e, i])
.sort()
.splice(0,5)
.map(([,el]) => el)
console.log(indices)
I would like to find out if swapping elements can be done using only .reduce in JavaScript. If not, what else should be used from the functional programming land?
This is not for sorting an array. I wanted to find all the permutations of the array element using .reduce which required the swap step as per this method.
You could take a function which takes an array and two indices and uses a destructuring assignment.
const swap = (array, i, j) => [array[i], array[j]] = [array[j], array[i]];
var array = [1, 2, 3];
swap(array, 0, 1)
console.log(array);
A version with reduce by taking an array of indices and swap all pairs from start to end.
const
swap = (array, ...indices) =>
indices.reduce((a, b) => ([array[a], array[b]] = [array[b], array[a]], b));
var array = [1, 2, 3];
swap(array, 0, 1)
console.log(array);
In es6, the idiomatic way to swap array elements is:
;[a[i], a[j]] = [a[j], a[i]]
Using .reduce is not appropriate for this task. You could technically do something like this:
a = a.reduce((acc, element, idx) => {
acc.push(idx === i ? a[j] : idx === j ? a[i] : a[idx])
return acc
}, [])
but it would result in convoluted code.
If your goal is to avoid mutating the original array, you can use Object.assign:
b = Object.assign([], a, {[i]: a[j], [j]: a[i]})
The reduce function reduces the array to a value of an object, as defined by the accumulator.
let array1 = [2, 5, 8, 0, 10];
let array2 = [1, 4, 9, 7, 6];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
The sort() method sorts the elements of an array in place and returns the array. The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.
var months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// expected output: Array ["Dec", "Feb", "Jan", "March"]
const sortingAccending = (a, b) => a - b
let numbers = [4, 2, 5, 1, 3];
numbers.sort(sortingAccending);
console.log(numbers);
// expected output: Array [1, 100000, 21, 30, 4]
And you answer your question, reduce can't be used for swapping elements.
You will have to either use sort for write your custom sort function
If i have an array A = [1, 4, 3, 2] and B = [0, 2, 1, 2] I want to return a new array (A - B) with values [1, 2, 2, 0]. What is the most efficient approach to do this in javascript?
const A = [1, 4, 3, 2]
const B = [0, 2, 1, 2]
console.log(A.filter(n => !B.includes(n)))
Use map method
The map method takes three parameters in it's callback function like below
currentValue, index, array
var a = [1, 4, 3, 2],
b = [0, 2, 1, 2]
var x = a.map(function(item, index) {
// In this case item correspond to currentValue of array a,
// using index to get value from array b
return item - b[index];
})
console.log(x);
For Simple and efficient ever.
Check here : JsPref - For Vs Map Vs forEach
var a = [1, 4, 3, 2],
b = [0, 2, 1, 2],
x = [];
for(var i = 0;i<=b.length-1;i++)
x.push(a[i] - b[i]);
console.log(x);
const A = [1, 4, 3, 2]
const B = [0, 2, 1, 2]
const C = A.map((valueA, indexInA) => valueA - B[indexInA])
console.log(C) // [1, 2, 2, 0]
Here the map is returning the substraction operation for each number of the first array.
Note: this will not work if the arrays have different lengths
If you want to override values in the first table you can simply use forEach method for arrays forEach. ForEach method takes the same parameter as map method (element, index, array). It's similar with the previous answer with map keyword but here we are not returning the value but assign value by own.
var a = [1, 4, 3, 2],
b = [0, 2, 1, 2]
a.forEach(function(item, index, arr) {
// item - current value in the loop
// index - index for this value in the array
// arr - reference to analyzed array
arr[index] = item - b[index];
})
//in this case we override values in first array
console.log(a);
One-liner using ES6 for the array's of equal size in length:
let subResult = a.map((v, i) => v - b[i]); // [1, 2, 2, 0]
v = value, i = index
function subtract(operand1 = [], operand2 = []) {
console.log('array1', operand1, 'array2', operand2);
const obj1 = {};
if (operand1.length === operand2.length) {
return operand1.map(($op, i) => {
return $op - operand2[i];
})
}
throw new Error('collections are of different lengths');
}
// Test by generating a random array
function getRandomArray(total){
const pool = []
for (let i = 0; i < total; i++) {
pool.push(Math.floor(Math.random() * total));
}
return pool;
}
console.log(subtract(getRandomArray(10), getRandomArray(10)))
Time Complexity is O(n)
You can also compare your answer with a big collection of arrays.