jQuery: remove duplicates from string [duplicate] - javascript

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

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)

Find first occurrence of a string in a String array in Typescript/Javascript [duplicate]

This question already has answers here:
How to find the array index with a value?
(12 answers)
split an array into two based on a index in javascript
(8 answers)
Closed 3 years ago.
I have an empty string array. I push into it new strings, and I do this one by one. Then, it will happen that the string I'm pushing is already in the array, and I want to find the index of that original string and split the array in the range:
[start, index_of_firstString].
So if we have:
myArray: string[] = [];
function(myString: string) {
this.myArray.push(myString);
}
What could be a good solution? I need to check for duplicates every time I push a new string.
You can use of indexOf function to get the first occurence of your string in your strings array,
var cars = ['ferrari','Audi','ferrari']
console.log(cars.indexOf('ferrari'))

how to convert "12A,13B,14C" to [12A,13B,14C] in javascript [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 5 years ago.
I have a string like below.
sections = "12A,13B,14C"
But I need [12A, 13B, 14C] for future use. How to create array from above string?
You have to use split method for that to split your string data into array values, have a look to updated code
var sections = "12A,13B,14C";
sections = sections.split(',');
console.log(sections);
Hope this will help! Just use split method of JS.
sections = "12A,13B,14C"
let array = sections.split(',');
console.log(array)

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(','))

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

Categories

Resources