简体   繁体   中英

Javascript: append single quotes to individual object results

I have an object with multiple properties, I want to wrap the results in single quotes and commas if there are multiple values please see the following fiddle.

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("','"));

This will return test4','test5','test6','test7 and I need it to be 'test4','test5','test6','test7'

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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