0

I am populating an array dynamically which is based on all the ids on the page's video tags.

How can I remove the word dog from each array item's value? Here is the populated array so far, I just need to remove the word dog for each array item:

var players = new Array();
$("video").each(function(){ 
    players.push($(this).attr('id'));
});

So it would go from:

["video_12_dog", "video_13_dog"]

to:

["video_12", "video_13"]
8
  • What would the array look like before and after this removal? Have you tried anything yet? Commented May 12, 2013 at 15:10
  • It would go from: ["video_12_dog","video_13_dog"] to: ["video_12","video_13"] Commented May 12, 2013 at 15:11
  • Squint that's not my question. My question is not to REMOVE a whole item, but to remove part of it's value! Commented May 12, 2013 at 15:13
  • 1
    You can edit the before-and-after arrays into your question, so that people immediately see it. Will the strings within the array always be formatted that way? Also, people may be reluctant to help without you showing some attempt. Commented May 12, 2013 at 15:16
  • 1
    If I had a dollar for every time someone used $(this).attr('id') instead of this.id, I'd be ... slightly well off. Commented May 12, 2013 at 15:25

1 Answer 1

4

You could iterate over the array and call replace() on each element (as string):

function removeWord(arr, word) {
    for (var i = 0; i < arr.length; i++){
        arr[i] = arr[i].replace(word,'');
    }
}
var aaa = ['value_1_dog', 'value_dog_2', 'dog_value3'];
removeWord(aaa, 'dog');
console.log(aaa); // ["value_1_", "value__2", "_value3"]

var question = ["video_12_dog","video_13_dog"]
removeWord(question, '_dog');
console.log(question); // ["video_12","video_13"]

Fiddle: http://jsfiddle.net/9KPkh/

Sign up to request clarification or add additional context in comments.

Comments

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.