Reputation: 3072
I am populating an array dynamically which is based on all the id
s 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"]
Upvotes: 0
Views: 1128
Reputation: 135752
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/
Upvotes: 4