Reputation: 1026
I'm trying to simplify a javascript function so it's more readable
Currently I'm trying to change this function below.
export default function generateSentimentLineGraphPoints(items) {
/**
* Generate array of 24 Zeros
*/
let values = Array.apply(null, Array(24)).map(Number.prototype.valueOf, 0);
const nOfEntries = Array.apply(null, Array(24)).map(
Number.prototype.valueOf,
1
);
.....
}
to more simply use this function,
function generateTime() {
/**
* Generate array of 24 Zeros
*/
let values = Array.apply(null, Array(24)).map(Number.prototype.valueOf, 0);
const nOfEntries = Array.apply(null, Array(24)).map(
Number.prototype.valueOf,
1
);
return values, nOfEntries;
}
with,
function generateTime() {
/**
* Generate array of 24 Zeros
*/
let values = Array.apply(null, Array(24)).map(Number.prototype.valueOf, 0);
const nOfEntries = Array.apply(null, Array(24)).map(
Number.prototype.valueOf,
1
);
return values, nOfEntries;
}
This isn't working for some reason - i'm not sure why, it won't load the graph properly. Any ideas?
Upvotes: 0
Views: 42
Reputation: 1075597
return values, nOfEntries;
returns the value of nOfEntries
, it doesn't return values
at all. A JavaScript function can only return one thing. You're using the comma operator there, which evaluates its left-hand operand (values
), throws away the result, then evaluates its right-hand operator (nOfEntries
) and takes that result as the operation result.
To return multiple values, wrap them in an array or object:
return [values, nOfEntries];
or
return {values, nOfEntries};
Side note: That code to create an array filled with a given value is unnecessarily complex. Back in the day it probably should have just been a function that uses a loop, but these days you can use Array.prototype.fill
:
let values = Array(24).fill(0);
Upvotes: 1