简体   繁体   English

删除数组中存在的括号

[英]Remove brackets which exists in array

I do have an array which looks like:我确实有一个看起来像这样的数组:

["[0, 1]", "0", "1"]

The question is how to remove brackets inside the array, so the result which I wish to get would be:问题是如何删除数组内的括号,所以我希望得到的结果是:

["0", "1", "0", "1"]

Currently, I'm out of idea...目前,我不知道...

Depending on the specific structure of the array elements, you might be able to scan the elements for things that look like numbers and then use flat_map to unroll the arrays you get from scan :根据数组元素的特定结构,您可能能够scan元素以查找看起来像数字的东西,然后使用flat_map展开您从scan获得的 arrays :

['[0, 1]', '0', '1'].flat_map { |e| e.scan(/\d+/) }
# ["0", "1", "0", "1"] 

This is what I came up with:这就是我想出的:

["[0, 1]", "0", "1"].to_s.split(/\W/).reject(&:blank?)

If the array elements might be nested, then I would go straight to YAML or JSON parsers:如果数组元素可能是嵌套的,那么我会 go 直接到YAMLJSON解析器:

require 'json'
['[0, [1, 0], 1]', '0', '1'].flat_map { |s| JSON.parse(s) }

# => [0, [1, 0], 1, 0, 1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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