1

I have to decompose words into collections of letters in Javascript. Then I need to be able to i) check whether a letter belongs to the collection and ii) to remove letters for this collection progressively.

For example, the word 'cool' would be decomposed into 'c', 'o', 'o', 'l'. If I remove 'o', 'a' and 'l', I should be left with 'c', 'o' only.

What is the proper data structure to achieve this in Javascript?

3
  • Take a look at the underscore.string library. Commented Feb 5, 2013 at 17:03
  • You can choose between arrays and objects, both having their advantages and disadvantages. Commented Feb 5, 2013 at 17:04
  • Proper? Proper?! It can be done many ways, what's this talk of proper? You have two primitive types, Object (which a bit like a dictionary) and Array, from both of which you can build on more complicated collections. Commented Feb 5, 2013 at 17:07

3 Answers 3

4

A string!

var str = 'cool';

To check if a character is in the string:

if(str.indexOf('o') > -1) // the character is in the string

To remove the first occurrence of a character:

str = str.replace('o','');

If you want to modify the string object to provide more "logical" representations of these actions you could do this:

String.prototype.contains = function(char){
    return this.indexOf(char) > -1;
};
String.prototype.remove = function(char){
    return this.replace(char,'');
};

which you could use like this:

if ('cheese'.contains('che'))
   alert('Velviva!');

var str = 'hurt'.remove('r');   // 'hut'
    str = 'banana'.remove('n'); // 'baana'
    str = 'happy'.remove('c');  // 'happy'
Sign up to request clarification or add additional context in comments.

5 Comments

I was wondering why someone would not use the object built for handling groups of characters...
@aepheus right? The OP should probably use jQuery because it is great and does everything.
I was trying to say I don't know why someone wouldn't use the string object (i.e. the method you are advising here). Not that they should use some library object, or even an array. Why waste the performance and space when you already have a collection of characters with associated functions.
I was being sarcastic. See here
@Shmiddty JQuery's character sequence manipulation plugin isn't very robust IMO, I suggest underscore.string
3

An array! You can get one by simply splitting the string like this:

var arr = "word".split("");

You can use the methods from Array.prototype to manipulate the collection like you said. For example, to remove the character at index 1:

arr.splice(1,1);

To make it a string again:

arr.join('');

If you just want to read a particular character from a string, you can just access its index directly:

var str = "word";
str[1]; // "o"

There are also other string methods you can use to achieve the same result you'd get with an array (see Shmiddty's answer).

4 Comments

This doesn't mention how he would test if a character is in the "collection", how he would remove a character from the collection, or anything actually useful...
True, but I can scan the array and set a corresponding entry to "" for example. I am happy enough!
@Shmiddty I thought the question was asking for a data structure only. I can add some example. I like your answer too, as well as how you paraphrased my first sentence :).
The answer is much improved now, +1!
2

JavaScript's object can be used for that, for instance:

var cool = "cool".split(""); // `cool` is an array of single-char strings
var crossIndex = {};
cool.forEach(function(entry) {
    crossIndex[entry] = true;
});

Now crossIndex has flags for whether it has a given letter, so:

var letter = "c"; // for instance

if (crossIndex[letter]) {
    // It has it

    // If you want to remove it:
    delete crossIndex[leter];
}
else {
    // It doesn't have it
}

1 Comment

The nice thing about this is you could use the syntax if (letter in crossIndex)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.