split a string to array containing pairs of two [duplicate] - javascript

This question already has answers here:
How can I split a string into segments of n characters?
(17 answers)
Closed 9 years ago.
I have a string like this: 56f7gg5cx887r7gt8r6t7.
Besides splitting it into an array of one and then looping by two i+2 and creating another array with entries containing two by two.
Is there a simpler way?
The result should be like this: ['56','f7','gg','5c','x8','87','r7','gt','8r','6t','7'].

You can use match:
'56f7gg5cx887r7gt8r6t7'.match(/(..?)/g)

Related

Remove All Strings From Array, But Keep Strings With Specific Length in JavaScript [duplicate]

This question already has answers here:
How can I only keep items of an array that match a certain condition?
(3 answers)
Closed 12 months ago.
I am trying to make Wordle in React. For that I need a list of Strings. I am getting an array of strings from this API: https://random-word-api.herokuapp.com/all
The problem is that I don't want every word. I only want words that has a length of 5. How do I remove all the strings that doesn't have a length of 5 from the array?
Assuming you have an array of words, words, use Array#filter.
const fiveLetterWords = words.filter(w => w.length === 5)

what method can I use to separate strings inside array into individual arrays (using JavaScript)? [duplicate]

This question already has answers here:
Convert string with commas to array
(18 answers)
Closed 5 years ago.
var array = ["Charizard,Pikachu,Glalie,Delfox"];
I tried to use the split() method except that only affects string variables and not arrays.
My ultimate goal is to make array = ["Charizard","Pikachu","Glalie","Delfox"];
You can retrieve the value of the array an split it using as delimiter the comma ','
var array = ["Charizard,Pikachu,Glalie,Delfox"];
console.log(array[0].split(','))

Explode string without separators [duplicate]

This question already has answers here:
How to get character array from a string?
(14 answers)
Closed 7 years ago.
I have a string like 12345 and I need to explode it to [1,2,3,4,5]. Normally, I would use split, but there are no separators in here. I could use substr (getting length, making loops), but I will probably have a lot of those strings, so it may not be good for performance. What can I do?
Simply use an empty string as a delimiter:
"12345".split('');

JavaScript / jQuery: how to split string with multiple values [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 8 years ago.
I have a string that always contains 8 variable values that are separated with a hypen (-) like in the following example:
5-2-2-2-2-2-2-1
What is the best way to split this into 8 separate values so that I can use each of them in a variable if the values can be either an integer or the value 'n/a' ?
Many thanks for any help with this, Tim.
var str = '5-2-2-2-2-2-2-1';
var parts = str.split('-');
for (var i=0;i<parts.length;i++){
console.log(parts[i]);
}
You are searching for the String.split() method

jQuery: remove duplicates from string [duplicate]

This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 8 years ago.
I have string values that contain a list of items separated with a comma, e.g. the value could be val1,val2,val3,val1,val4,val3,val2,val5.
Is there an easy way I can remove all duplicates from such a string so that each value only appears once within the string ?
E.g. in the above example I would like to get val1,val2,val3,val4,val5 instead.
Many thanks in advance, Tim.
Make an array by split string and use unique()
try like this:
data = "val1,val2,val3,val4,val5";
arr = $.unique(data.split(','));
data = arr.join(","); //get unique string back with
live demo

Categories

Resources