Convert array of strings to array of numbers [duplicate] - javascript

This question already has answers here:
convert string array to integer array
(4 answers)
Closed 4 years ago.
I have a jQuery array ["3434", "3433"]
And I would like to make it like [3434, 3433]

No need for jQuery. Supposing you're sure they're all numbers (that is, we're not checking for errors), you can map them with Number, which converts them from string to numbers.
["3434", "3433"].map(Number);
Considering that Number returns NaN in case of error, you may then want to filter the result to remove undesired elements.
let nums = ["3434", "3433", "foo"].map(Number).filter(n => !isNaN(n));
console.log(nums);

Related

Excluding Punctuation Marks in JavaScript include method [duplicate]

This question already has answers here:
Check if an array of strings contains a substring [duplicate]
(5 answers)
How do you search an array for a substring match?
(15 answers)
Closed 4 months ago.
Is there a better way of checking if a word exists in an array of strings without including Punctuation Marks?
what I have tried,
const sArray=['Lorem','Ipsum','typesetting-','industry.','Ipsum?','has' ]
console.log(sArray.toString().replaceAll(".","").includes("industry")) //true
console.log(sArray.includes("industry")) //false
Something like this?
sArray.some(str => str.includes('industry'))
If you do a lot of operations on the array, I suggest creating a new reference of the array after replacing, then dealing with the new array.
const sArray=['Lorem','Ipsum','typesetting-','industry.','Ipsum?','has' ]
const trasformedArray = sArray.map(i => i.replaceAll('.', ''));
console.log(trasformedArray.includes("industry"))

convert array into array with key and parenthesis in the form [{fruit:"apple"},{fruit:"banana"},{fruit:"mango}] [duplicate]

This question already has answers here:
Convert array of strings into an array of objects
(6 answers)
JS : Convert Array of Strings to Array of Objects
(1 answer)
Javascript string array to object [duplicate]
(4 answers)
Converting string array to Name/Value object in javascript
(4 answers)
Closed 1 year ago.
could you please tell me how to convert an array which is
["apple","banana", "mango"]
to
[{fruit:"apple"},{fruit:"banana"},{fruit:"mango}]
with the parenthesis
something similar to this but with parenthesis
JavaScript Add key to each value in array
You're still looking for the .map() function, just like that other question. The only difference is that you want to return an object from the callback, not just a string.
For example:
let input = ["apple","banana", "mango"];
console.log(input.map(x => ({fruit: x})));
As an aside, {} are not parentheses, they are curly braces.

why the first number element stored in an array is wrong in Javascript [duplicate]

This question already has answers here:
Why JavaScript treats a number as octal if it has a leading zero
(3 answers)
Closed 1 year ago.
const arr = [0123, 1456];
console.log(arr);
The result is [ 83, 1456 ], but I think it should be [0123, 1456].
The first element is wrong. I don't know why.
Numbers starting with zero are octal integers. You might want to use 123 as your first member of the array.

Why does it show value 5150 and not 155? [duplicate]

This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 4 years ago.
So im quite new to javascript and i tried making something simple as entering a value and add 150 to it but it wont show number + 150?
Pictures below
Code
Output
That's because you are concatenating strings. You need to convert the strings to integers by using parseInt(), and then add the numbers.
https://www.w3schools.com/jsref/jsref_parseint.asp
var fullprice = parseInt(price) + 150;
Because you're actually concatenating a string with a number. The input value is a string, so before operating with it, parse it to int with parseInt
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

What happens when we declare Array(4) in JavaScript? [duplicate]

This question already has answers here:
Javascript new Array and join() method
(5 answers)
Closed 6 years ago.
console.log(Array(4).join("hi"));
>> "hihihi"
I don't get what exactly is happening here?
join() is the opposite of split(). whereas split separates an array by the delimiting character you pass it, join will instead combine all the elements delimiting each one with whatever parameter you pass.
In this case the array is simply Array(4), so 4 undefined elements. combining these will yield "undefinedhiundefinedhiundefinedhiundefined".
Since js doesn't actually treat undefined as anything in this case, it turns it into an empty string and all you get is hihihi
edit: reference for my last statement from the join() documentation:
The string conversions of all array elements are joined into one string. If an element is undefined or null, it is converted to the empty string.

Categories