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"))
Related
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.
This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Execute JavaScript code stored as a string
(22 answers)
Closed 1 year ago.
i have a string as below and i would like to extract the array between the quotes.
mystring = "['str1','str2']"
I tried it with eval and i do not want to use eval in my code. is there any other neat way to do this ?
function parseString(string) {
return string
.split(",")
.map((str) => str.replace("[", "").replace("]", "").replaceAll("'", ""));
}
This assumes none of array indexes includes character ",".
This question already has answers here:
Get Substring between two characters using javascript
(24 answers)
Closed 4 years ago.
I need to iterate over strings that are inside an array, to get a sub-string in each string.
Substrings are between "()"
Something like this..
let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];
//return
//'cat','red','apple'
How I could do that?
You can do this using substring and lastIndexOf functions.
let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];
myArray.forEach(function(e){
console.log(e.substring(e.lastIndexOf("(") + 1, e.lastIndexOf(")")))
})
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);
This question already has answers here:
How to check whether a string contains a substring in JavaScript?
(3 answers)
Closed 5 years ago.
I need JavaScript algorithm that can match substring of a sting?
subStringFinder('abbcdabbbbbck', 'ab')
should return index 0
and
subStringFinder('abbcdabbbbbck', 'bck') should return index 10
Could you please tell me how to write this code?
--EDIT:
Thanks to #Jonathan.Brink I wrote that code and it did the trick:
function subStringFinder(str, subString) {
return str.indexOf(subString);
}
subStringFinder('abbcdabbbbbck', 'bck') // -> 10
You are looking for the indexOf function which is available via the built-in string type (as well as array).
Example:
var str = "abbcdabbbbbck";
var n = str.indexOf("bck");
// n is 9
Probably, rather than having a custom subStringFinder function it would be better to just use indexOf.