如何将一个列表的列表转换为一个单一的列表?

huangapple go评论55阅读模式
英文:

Karate: How to turn a list of lists into a single list?

问题

我有一个在Karate中的列表列表,看起来像这样:
[[elemtna], [abc], [bcd]]

我想将这个列表转换成一个单一的列表,如下所示:[elemtna, abc, bcd]

我尝试了以下代码,但没有成功:

  • def mylist = existingList.map(x => x.map(y => String(y)))
英文:

I have a list of lists in Karate that looks like:
[[elemtna], [abc], [bcd]]

I'd like to convert this list into a single list like: [elemtna, abc, bcd]

I tried the following code with no success:

  • def mylist = existingList.map(x => x.map(y => String(y)))

答案1

得分: 2

你可以使用flat()方法:

[['elemtna', 'doc', 'third'], ['abc'], ['bcd']].flat() // 返回 ['elemtna', 'doc', 'third', 'abc', 'bcd']
英文:

You can use the flat() method:

[['elemtna', 'doc', 'third'], ['abc'], ['bcd']].flat() // returns ['elemtna', 'doc', 'third', 'abc', 'bcd']

huangapple
  • 本文由 发表于 2023年6月13日 04:26:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定