2014-09-29 70 views
0

我有一個具有多個屬性的對象,如果有多個值,我想用單引號和逗號包裝結果,請參閱下面的提琴。Javascript:將單引號添加到單個對象結果中

http://jsfiddle.net/efv7sh9d/1/

 var obj = { 
     WKF001: ['test1'], 
     WKF002: ['test2','test3'], 
     WKF003: ['test4','test5','test6','test7'], 
     WKF004: ['test8','test8','test9','test10','test11'], 
     WKF005: ['test12','test13','test14','test15','test16'] 
    } 

    function returnCodes(wkf, obj) { 
    for (var d in obj) { 
     if (d.indexOf(wkf) > -1) { 
      return obj[d]; 
     } 
     } 
     return 'Unknown'; 
     } 

    var wkftest1 = "WKF001"; 
    var wkftest2 = "WKF002"; 
    var wkftest3 = "WKF003"; 

document.write(returnCodes(wkftest3, obj).join("','")); 

這將返回TEST4' , 'TEST5', 'TEST6', 'TEST7,我需要它是 'T​​EST4',' TEST5' , 'TEST6', 'TEST7'

+0

'「'」+ joincode +「'」'? – 2014-09-29 14:14:13

回答

1
document.write("'" + returnCodes(wkftest3, obj).join("','") + "'"); 

這是數組連接方法/函數在任何語言中的常見習慣用法。

+0

呃,我真的遇到過這個,並沒有測試它 – 2014-09-29 14:18:42

相關問題