2013-03-28 69 views
0

說我有一個字符串數組。我想用一個固定的(公共)字符串來預先給每個字符串加上一個字符串。 我知道在Javascript中,字符串可以像strM = str1 + str2 + str3 + ... + strNstrM = concat(str1, str2, str3, ..., strN)連接起來。考慮這段代碼。如何預先使用'this'字符串

var defImgDirPath = 'res/img/'; 
$([ 
    'home-icon-dark.png', 
    'home-icon-light.png' 
]).each(function() { 
    /* 
    * Prepend each string with defImgDirPath 
    */ 
}); 

現在,我不能做this = defImgDirPath + this;(我是愚蠢到試圖)

而且,我想return (defImgDirPath + this);但將不能工作。

我在想像this.prependString(defImgDirPath);這樣的函數,但是有這樣一個函數嗎?如果不是,我該如何寫一個?

注意:我知道這可以通過for循環很容易和簡單地完成,但有什麼好玩的呢? :)

+0

我可能是模糊與你在問什麼,但你什麼時候需要使用它的路徑?你是否試圖在img標籤中使用它?如果路徑前綴是常量,您是否可以在需要使用它時連接? – Bmo 2013-03-28 10:46:53

+0

您必須將參數傳遞給'.each' http://jsfiddle.net/hj2XJ/ – sofl 2013-03-28 10:47:54

+1

使用jQuery並不總是有意義的...當您可以使用常規js時,您應該。 – 2013-03-28 10:52:07

回答

1
var defImgDirPath = 'res/img/'; 
var images = [ 
    'home-icon-dark.png', 
    'home-icon-light.png' 
]; 
$(images).each(function(idx, val) { 
    images[idx] = defImgDirPath + val; 
}); 

console.log(images); 
+0

**正是**我正在尋找!非常感謝。 – 2013-03-28 10:52:20

0

問題是,您正在更改數組中的項目的副本,因此該數組將不受影響。

遍歷數組中的索引,並把結果返回數組中:

var arr = [ 'home-icon-dark.png', 'home-icon-light.png' ]; 

for (var i = 0; i < arr.length; i++) { 
    arr[i] = defImgDirPath + arr[i]; 
} 

如果你真的想使用jQuery的循環方法,那麼你就需要使用map,讓你以數組形式獲得結果。請注意,您需要使用回調函數的參數,如this是不是該數組中的項目:

arr = $.map(arr, function(str){ 
    return defImgDirPath + str; 
}); 
+0

我在問題中說過,我知道使用'for'循環可以很容易地完成它。我想用'$ .each'來做。但是,我不知道'$ .map'。儘管如此,'$ .each'看起來更像_elegant_(至少對我而言)。感謝你的回答。乾杯! – 2013-03-28 10:51:34

+0

@ZiaUrRehman:您可以使用'$ .each'而不是常規循環來獲取迭代的索引,但它不是很優雅,因爲回調函數會在閉包中獲取對數組的引用,而是直接改變它返回結果。 – Guffa 2013-03-28 11:09:53

+0

你的意思是'$ .each'不會返回一個前綴爲'defImgDirPath'的字符串數組? – 2013-03-28 11:16:18

1

在JavaScript的(ECMA 5)的最新標準,你可以做到這一點沒有jQuery的:

var defImgDirPath = 'res/img/'; 

['home-icon-dark.png','home-icon-light.png'].map(function(i) { 

     return defImgDirPath + i; }); 

編輯:此外,jquery的地圖功能工作也類似。

+1

這意味着它在IE8中不起作用。 – Guffa 2013-03-28 10:51:25

+0

但是,是的,它在最新ie ie ...我從一個MS視頻撿起來:) – loxxy 2013-03-28 10:52:01

+0

我認爲'$。map'只是一個jQuery函數。它也可以使用純Javascript嗎? – 2013-03-28 10:53:30

0

在Javascript中,this始終引用一個對象(當前上下文對象),因此它永遠不會是一個字符串,因此嘗試將它與另一個字符串連接將失敗,正如您找到的。

但是,沒有必要爲你正在做的事情使用jQuery或對象;你只需在每個字符串數組的開頭添加一個已知值即可。

讓我們把它改寫使用標準的JavaScript,而jQuery的併發症...

var defImgDirPath = 'res/img/'; 
var images = [ 
    'home-icon-dark.png', 
    'home-icon-light.png' 
]; 

for(var count=0; count<images.length; count++) { 
    images[count] = defImgDirPath + images[count]; 
} 

我已經把圖像數組變量,使其更容易看到什麼實際的循環代碼在做什麼。正如你所看到的,這是一個非常簡單的循環;這裏不需要jQuery魔術。

希望幫助。

0

this.prependString(defImgDirPath);

號字符串是在JavaScript中不可變的。級聯到一個新的字符串就可以了,並用它.concat會是什麼樣

var newString = defImgDirPath.concat(this); // no static function! 

如果不存在,我怎麼寫呢?

你不能,因爲assigning object to "this"是不可能的。

相反,你必須分配給你的工作(使用static $.each,而不是在收集工作)的陣列的屬性:

var arr = [ 
    'home-icon-dark.png', 
    'home-icon-light.png' 
]; 
$.each(arr, function(i) { 
    arr[i] = defImgDirPath + this; 
}); 
arr[0] // res/img/home-icon-dark.png 

我也試過return (defImgDirPath + this);

您將需要使用map爲,創建一個新的數組:

var arr = $.map([ 
    'home-icon-dark.png', 
    'home-icon-light.png' 
], function(str) { 
    return defImgDirPath + str; 
}); 

當然,它可能沒有jQuery的更容易在所有:

for (var i=0; i<arr.length; i++) { 
    arr[i] = defImgDirPath + arr[i]; 
} 

而且還map可作爲本地函數(而不是在過時的瀏覽器):

var arr = [ 
    'home-icon-dark.png', 
    'home-icon-light.png' 
].map(function(str) { 
    return defImgDirPath + str; 
}); 
+0

當使用'map'方法時,必須使用回調函數的參數,因爲'this'指向'window對象。 – Guffa 2013-03-28 10:53:00

+0

@Guffa:謝謝,修正。如果使用'this',將需要'$([...]).map(function(){return defImgDirPath + this;})。get()': – Bergi 2013-03-28 10:59:36

相關問題