简体   繁体   English

从数组中的每个字符串值中删除一个公共单词

[英]Remove a common word from each string value in an array

I am populating an array dynamically which is based on all the id s on the page's video tags. 我正在动态填充一个基于页面video标签上所有id的数组。

How can I remove the word dog from each array item's value? 如何从每个数组项目的值中删除“ dog ”一词? Here is the populated array so far, I just need to remove the word dog for each array item: 到目前为止,这是已填充的数组,我只需要为每个数组项删除单词dog

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"]

You could iterate over the array and call replace() on each element (as string): 您可以遍历数组并在每个元素(作为字符串)上调用replace() ):

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/ 小提琴: http : //jsfiddle.net/9KPkh/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM