React -- Why does this line append rather than add [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 15 days ago.
Improve this question
I have other ways of doing the same thing .. So I am not looking for a different solution to this problem ... I am looking for an explanation as to why if I have defined a integer, it still concatenates with .map as if it were a string.
I have a basic set of data retrieved from an API:
"data":["8","8","12","1","7","4","2"]
If I map it using
let count = response.data.metrics.data.map((item) => + parseInt(item));
I am having a hard time understanding why it's treating this as a string returning
88121743
When I feel like because I am parsing it as an integer it should add and come out with 42.
Is this just an issue just using .map? Can shortcut math functions be used here?
Here is my Reproducible Example

Your current approach using Array#map creates a new array with each element converted to a number. React renders this array as multiple text nodes, so it looks like a single string.
To sum the values, use Array#reduce with parseFloat/parseInt, the Number function, or the unary plus operator to convert each string to a number.
const visitCount = data.reduce((a,b) => a + parseFloat(b), 0);

Related

Why array is not showing proper result in react? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 19 days ago.
Improve this question
Below is the screen of my VScode, I am declaring arr1 as an array of numbers and later reversing it.Input
The problem is that on output the 1st paragraph is showing the reversed arrays instead of original one. What is the exact problem?
Output
Try this:
let reverse = Array.from(arr1).reverse()
As reverse() method modifies the original array (arr1), then you need to create a new array with Array.from(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
Reverse is a mutating method, so both arr1 and reverse reference the same array. You'll want to do something like const reverse = [...arr1].reverse() instead to ensure that you aren't mutating the original array.

JS concatenate numbers with operator sign [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
Maybe tittle is not too descriptive, but how do I get this kind of result
var string = 11+'-'+10
// expected result 11-10
console.log(string);
Every time I try to do the above I get 1, or whatever the result of the subtraction is
I will be a little bit clear about this. What I want to do with this is generate a button with onclick like this:
onClick = method(1,[11-10, 12-10])
method(id,...array){
console.log(array)
//result [1,2]
}
even if inspecting the button actually shows the correct output
In your first example, you use
11+'-'+10
In the second one, you use
11-10
There is a clear difference
Using the first method in the second code will work as expected
method(1,[11+'-'+10, 12+'-'+10])
To make it shorter just use strings
method(1,['11-10', '12-10'])

how to change my this date format "20221015T000000Z" into yyyy-mm-dd (2022-10-31)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
i want to change my date format from "20221015T000000Z" to yyyy-mm-dd. i tried looking up solutions but not able to find anything relatable.
The example date string already contains all the information you need in clear text. It's not a standard date string, but assuming the format of the string is always going to be like that, all you have to do is to extract the substrings and stitch them back together using the - separator.
This is exactly what this function does by using String's substr method and Array's join method:
const parseDateString = s => [s.substr(0,4), s.substr(4,2), s.substr(6,2)].join('-');
Usage:
const result = parseDateString('20221015T000000Z');
// do something with result, e.g. console.log(result)

How can I generate random numbers without using Math.random()? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Looking for Javascript program. Without using Math.random() method in Javascript. How do generate random no.
You can use getTime() for that like that, if length doesn't matter for your, if it does, use .slice() method but before using it convert it in string using .toString()
const randomNumber = parseInt(new Date().getTime())
console.log(randomNumber)
use timestamp
Math.floor(Date.now() /1000);
Without Math library
Date.now() /1000
Use this for 3 digit random number
console.log((new Date().getMilliseconds()));

Get the minimum value from the array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to get the minimum value from an array without changing the way the the value are aligned.
function removeSmallest(numbers){
var min=Math.min.apply(null,numbers);
var indexvalue=numbers.indexOf(min);
delete numbers[indexvalue];
return numbers;
}
Two issues:
Math.min.apply(null, numbers) should be Math.min.apply(Math, numbers) (note Math instead of null)
Normally, delete is not the right tool for arrays (see this question's answers for details; use splice to remove array entries if you're going to modify the array in place:
numbers.splice(indexvalue, 1);
You might also consider creating and returning a new array instead of mutating and returning the same array you received.

Categories