我試圖循環javascript中的一個鍵:值數組中的對象以便使用string.prototype .replace方法,用於針對GulpJS concat目的的js文件的路徑數組。無法返回object.keys foreach循環中與string.prototype.replace方法不匹配的對象
這裏的目標是抽出GULP可用的路徑列表,但目前他們需要在這樣做之前處理它們。爲此,我使用它在subs數組中的相應替換值替換paths數組中現有的「佔位符」值,並循環直到所有路徑都是文字。
的問題是,我希望控制檯返回由環固定尚未處理的路徑/ 即」 ../test4'(字面路徑),所以我可以忽略他們,做的東西與他們一旦循環完成。
這裏是我到目前爲止有一些測試數據用於演示目的:
function fixPaths(substitutionsArray, pathArray) {
var fixedPaths = [];
pathArray.forEach(function (path, index, array) {
console.log('processing line ' + (index + 1) + '... ' + path);
Object.keys(substitutionsArray).forEach(function (placeholder) {
var substitution = substitutionsArray[placeholder];
var fixPath = path.replace(placeholder, substitution);
if (fixPath == path) {
console.log('No replacement has been found in path ' + path + ' for placeholder ' + placeholder);
} else {
console.log('Replacement found in path ' + path + ' for placeholder ' + placeholder + ' resulting in ' + fixPath);
fixedPaths.push(fixPath);
}
}
)
});
console.log(fixedPaths);
return fixedPaths;
}
var subs = {
"@[email protected]": "/test1",
"@[email protected]": "/test2",
"@[email protected]": "/test3",
"@[email protected]": "/components"
};
var paths = ['@[email protected]/test1', '@[email protected]/test2', '@[email protected]/test3', '../test4', '../../test5'];
result = fixPaths(subs, paths);
console.log(result);
return;
就如何更好地實現這一目標有什麼建議?
非常感謝本 -