Javascript:将单引号附加到单个对象结果

时间:2014-09-29 14:13:20

标签: javascript object

我有一个具有多个属性的对象,如果有多个值,我想用单引号和逗号包装结果请看下面的小提琴。

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,我需要它作为' test4',& #39; TEST5'' TEST6'' TEST7'

1 个答案:

答案 0 :(得分:1)

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

这是任何使用数组连接方法/函数的语言的常用习语。