Modify all but first element of multidimensional array [duplicate] - javascript

This question already has answers here:
How to round all the values in an array to 2 decimal points
(4 answers)
How can I skip a specific Index in an array in a for loop javascript
(1 answer)
Closed 5 years ago.
Let's say there is this array of arrays:
theArray = [["name1", 12.23423, 54.243, 6.23566, 5675.552, ...],
["name2", 345.8655, 92.9316, ..],
["name3", 99.56756, 52.988, 3.09889, ...],
...
];
Each sub-array starts with a string and it is followed by numbers. My aim is to reduce the numbers to a shorter form.
I know that this can be done using .toFixed(2) in order to have only two digits after the dot by I don't know how to access them because of the string in the front.
I want them to remain as numbers because I must use them as data for a chart.
Do you have any suggestions?

You could keep the value of the first item.
var array = [["name1", 12.23423, 54.243, 6.23566, 5675.552], ["name2", 345.8655, 92.9316], ["name3", 99.56756, 52.988, 3.09889]],
result = array.map(a => a.map((v, i) => i ? +v.toFixed(2) : v));
console.log(result);

Simple solution by just using two nested for loops with Index 0 and Index 1.
theArray = [["name1", 12.23423, 54.243, 6.23566, 5675.552],
["name2", 345.8655, 92.9316],
["name3", 99.56756, 52.988, 3.09889],
];
for(var i=0;i<theArray.length;i++) {
for(var k=1;k<theArray[i].length;k++) {
theArray[i][k] = parseFloat(theArray[i][k].toFixed(2));
}
}
console.log(theArray);

Related

Turn array containing array of numbers into formatted version [duplicate]

This question already has an answer here:
JS: Join Array of arrays
(1 answer)
Closed 1 year ago.
Trying to solve this puzzle in JavaScript:
Input: An array containing x amoung of arrays of numbers. Heres example:
[
[1,4],
[6,8],
[10]
]
Expected Output: I would want to run some sort of code to turn it into:
1-4,6-8,10
I've tried join("-") and tried the same thing within a forEach() loop but can't quite get it to work
You can use map the array and join each item by a dash, then join the resulting array.
const arr = [
[1, 4],
[6, 8],
[10]
]
const res = arr.map(e => e.join('-')).join()
console.log(res)
You can use map to iterate over the array, join up each nested array with a dash, and then finally join up the array that map returns into a comma-delimited string.
const arr = [[1,4],[6,8],[10]];
const out = arr.map(a => a.join('-')).join(',');
console.log(out);

Is there any way to plus multi array number in javascript? [duplicate]

This question already has answers here:
How to calculate the sum of multiple arrays?
(6 answers)
Closed 1 year ago.
I have an array. With each item in array is an array number. And the length of each array is the same. For example:
var data = [[1,2,4,1], [2,2,1,3], [1,1,2,2], ...]
And the result I want to have:
=> res = [4, 5, 7, 6]
res is the result of adding arrays according to the corresponding index. And of course my data may also contain lots of items.
I have referenced through the lodash.unzipWith. But it doesn't seem viable. With any advice. please let me know. Sorry for my weak English
You can use reduce and write something like this, without lodash or anything
const data = [[1,2,4,1], [2,2,1,3], [1,1,2,2]]
const sumArrs = (arrs) => {
return arrs.reduce((prev, curr) => {
return curr.map((num, i) => num + (prev[i] || 0))
}, [])
}
console.log(sumArrs(data))

Array.reduce() stacking numbers rather than summing them? [duplicate]

This question already has answers here:
Sum of a javascript array returns a string concatenation of all the numbers [closed]
(3 answers)
Closed 3 years ago.
I'm trying to reduce an array of numbers into the sum of all the numbers combined. At the moment I'm using Array.reduce to try and achieve this, but what I'm finding is that this function only stacks the array's values to create one massive number rather than summing them all together.
// Function used to get the sum of all numbers in array
function getSum(total, num){
return total + num;
// Reduce Var
var easternSum = scoreEastern.reduce(getSum);
// Dynamic array based on user input
var scoreEastern = dataSet
.filter(scoreEastern => scoreEastern.Course === 'eastern')
.map(({Score}) => Score);
// Empty array that scoreEastern var is assigned to
var dataSet = [];
Because my array is dynamic, it's based on what the user inputs into a form, there's no set array. But let's say the array is:
var scoreEastern = [10, 20, 30]
The reduce var easternSum will result in the number 102,030. What I want is 60.
I think maybe scoreEastern doesn't have the data that you expect all the time? You mentioned that it is dynamic. This snippet appears to work for the use case you posted in your question.
const scoreEastern = [10, 20, 30];
console.log(scoreEastern.reduce((prev, curr) => prev + curr));

JS: How to count all values in nested array [duplicate]

This question already has answers here:
Sum all integers in a multidimensional array javascript
(9 answers)
Closed 5 years ago.
I'm using a nested array like this:
const data = [
[0],
[2],
[[1], 3]
1
]
Is it possible to count all values together. In this example the result should be 7 (0+2+1+3+1).
And is it also possible to count how many arrays are used? This would be 5 arrays
const sumUp = array => array.reduce((sum, el) => sum + (Array.isArray(el) ? sumUp(el) : +el), 0);
This uses a recursive approach with reduce.

Get duplicates from array of objects by value [duplicate]

This question already has answers here:
How to combine an array in javascript
(3 answers)
Closed 6 years ago.
I have an array of objects:
var arr = [
{number: "AL-32021611", b: "7500"},
{number: "AL-32021612", b: "Continental"},
{number: "AL-32021612", b: "R3"},
{number: "AL-32021612", b: "7500"}
];
Is there a way that I can get all the number coincidences and get insted of number values the 'b' values in a var?
for example
//loop
result = ["Continental", "R3", "7500"]
what i want is for example i recive the number and then i search all the coincidences with that number value and what i exactly need is all the values from the coincidences
Using ES6 features:
let result = Array.from(new Set(arr.map(el => el.b)));
or
let result = [...new Set(arr.map(el => el.b))];
Array.from()
Set
Array.prototype.map()
Arrow Functions
Spread Operator ...
Str has a nice one-line answer for you, but you can also do it explicitly with a simple for loop. See below.
As you have it,
result = {"Continental", "R3" , "7500"};
is not a valid object. You could use a for loop and push the b values into a new array that would look like:
result = ["Continental", "R3" , "7500"];
Your for loop would look like:
var result = [];
for(var n of arr) {
result.push(arr[n].b);
}
return result;

Categories