【问题标题】:How to remove double quotes from an array of strings如何从字符串数组中删除双引号
【发布时间】:2019-07-08 12:17:39
【问题描述】:

假设我有下面的数组

var someStuff = [
  "start", { one: "one"},
  "setSomething", "foobar",
  "someBool", true
];

我怎样才能把它变成如下

var someStuff = [
  MainFunc.start({one: "one"}),
  MainFunc.setClientId('foobar'),
  MainFunc.someBool(true)
];

【问题讨论】:

  • 什么是 MainFunc?
  • 听起来像是 X/Y 问题。你为什么不把 someStuff 做成一个合适的对象?

标签: javascript arrays regex


【解决方案1】:

假设您想要一个包含来自多个函数调用的结果的数组,这就是您获得它的方法。您需要遍历数组,从 MainFunc 对象中获取每个函数,然后每次都调用它。

我包含了对正在调用的不存在函数的处理,这恰好在您的示例中(可能不是故意的)。

var MainFunc = {
  start       : function(x) { return "start called with " + x.toString(); },
  setClientId : function(x) { return "setClientId called with " + x.toString(); },
  someBool    : function(x) { return "someBool called with " + x.toString(); }
}

var someStuff = [
  "start", { one: "one" },
  "setSomething", "foobar",
  "setClientId", "foobar",
  "someBool", true
];

var result = Process(someStuff);

function Process(arr) {
  var result = [];
  for (var i = 0; i < arr.length; i += 2) {
    let fnToCall = MainFunc[arr[i]];
    let fnResult = fnToCall ? fnToCall(arr[i + 1]) : "(unknown: '" + arr[i] + "')";
    result.push(fnResult);
  }
  return result;
}

console.log(result);

【讨论】:

    【解决方案2】:

    这样试试(你需要使用箭头函数,否则函数会被直接调用):

    var someStuff = [
      "start", { one: "one"},
      "setSomething", "foobar",
      "someBool", true
    ];
    
    let output = []
    
    for(let i=0, j=0; i<someStuff.length; i+=2, j++) {
      let functionName = someStuff[i];
      let functionArgs = someStuff[i+1];
    
      output[j] = () => MainFunc[functionName](functionArgs);
    }
    
    console.log(output);

    【讨论】:

      【解决方案3】:

      我想你可能想这样做

      function mainFunc() {
        this.start = function(obj) { console.log("start",obj) };
        this.setSomething = function(obj) { console.log("set",obj) };
        this.someBool =     function(obj) { console.log("bool",obj) };
      }
      
      var someStuff = {
        "start": { "one":"one"},
        "setSomething": "foobar",
        "someBool": true
      }; 
      var mf = new mainFunc();
      for (var a in someStuff) {
        mf[a](someStuff[a])
      }  

      【讨论】:

        【解决方案4】:

        为了更好的可读性,定义一个包含动作及其参数的对象数组。

        const mainFunc = {
          start: (param) => console.log('start', param),
          setSomething: (param) => console.log('SetSomething', param),
          someBool: (param) => console.log('someBool', param)
        }
        
        var someStuff = [   
          { action: "start", param: { one: "one"} },
          { action: "setSomething", param: "foobar" },
          { action: "someBool",  param: true }
        ];
        
        someStuff.forEach(stuff => {
            if (typeof mainFunc[stuff.action] === 'function') {
              mainFunc[stuff.action](stuff.param);
            }
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-10
          • 2023-04-04
          • 2013-10-09
          • 2012-09-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多