输入数据中的数组或字符串 (lodash)

Array or string in input data (lodash)

我在输入中有名称的字符串或数组。如何在不使用 if-else 的情况下将 console.log 名称列表作为字符串获取?我只能使用 lodash 库。

我不明白为什么需要 lodash 来做这样的事情,但这也许能满足您的需要:

function consoleLogList(input) {

  _.each(_.concat([], input), function (name) {

    console.log(name)

  })
}

consoleLogList('Single') // string
consoleLogList(['John', 'Mike', 'Bill']) // or array
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>

希望对您有所帮助。

感谢您的回答!我这样做了:

function printNames(names) {

    console.log(_.toString(names)); 

}; 

printNames([1,2,3]); 

printNames('1,2,3');

比我想象的要容易