admin管理员组文章数量:1335895
Deeply nested in a JSON object I have the property value
.
actions:
[ { name: 'InviteUser',
type: 'button',
value: '[Brian, Timoney, [email protected],United States, DEM, edit me, 1580333]' } ],
callback_id: 'market_invite',
pulled out of the nested JSON it looks like:
value: '[Brian, Timoney, [email protected],United States, DEM, edit me, 1580333]'
Is there a simple way to convert this to an array? The array output I am looking for would be:
["Brian", "Timoney", "[email protected]", "United States", "DEM", "edit me", "1580333"]
Hard to believe I am asking this since it seems very straight-forward but after some time of trying variations of things like Array.from()
and JSON.parse()
and trying to find a duplicate question on stack overflow, I am left puzzled.
Deeply nested in a JSON object I have the property value
.
actions:
[ { name: 'InviteUser',
type: 'button',
value: '[Brian, Timoney, [email protected],United States, DEM, edit me, 1580333]' } ],
callback_id: 'market_invite',
pulled out of the nested JSON it looks like:
value: '[Brian, Timoney, [email protected],United States, DEM, edit me, 1580333]'
Is there a simple way to convert this to an array? The array output I am looking for would be:
["Brian", "Timoney", "[email protected]", "United States", "DEM", "edit me", "1580333"]
Hard to believe I am asking this since it seems very straight-forward but after some time of trying variations of things like Array.from()
and JSON.parse()
and trying to find a duplicate question on stack overflow, I am left puzzled.
- Can you be more clear about what you want converted to an array? Adding the desired output to the post would help a lot. – Ed Bayiates Commented Dec 11, 2017 at 17:07
- @EdBayiates - edited to be more explicit – maxwell Commented Dec 11, 2017 at 17:08
- 1 The better idea would be to fix whatever program serialised this value into a string. – Bergi Commented Dec 11, 2017 at 17:10
- So your output would be an array consisting of 6 elements, namely: "Brian", "Timoney", "[email protected],United States", "DEM", "edit me", "1580333" ? – Ed Bayiates Commented Dec 11, 2017 at 17:10
- The problem is that the string is not in the form of valid JSON. The "values" are not quoted, so how the strings should be separated is ambiguous. Where does something like that e from in the first place? It might be easier to work on avoiding getting the list in that form in the first place. – Pointy Commented Dec 11, 2017 at 17:11
3 Answers
Reset to default 5You can simply use the string API. Depending on how certain you are about the characters that might occur in your input (especially mas), the following may be enough:
function valueStringToArray(valueString) {
return valueString.substr(0, valueString.length - 1).substr(1).split(',');
}
var obj = {
actions: [{
name: 'InviteUser',
type: 'button',
value: '[Brian, Timoney, [email protected],United States, DEM, edit me, 1580333]'
}],
callback_id: 'market_invite'
};
var arr = obj.actions[0].value.replace(/\[|\]/g, "").split(",").map(str => str.trim());
console.log(arr);
Try using the built in string methods, for example:
function parseArrayValues(s) {
return s.substr(1, s.length-2).split(/\s*,\s*/);
}
parseArrayValues(actions[0].value);
// => ["Brian", "Timoney", "[email protected]", "United States", "DEM", "edit me", "1580333"]
本文标签: javascriptConvert Array Surrounded By Single Quotes To An ArrayStack Overflow
版权声明:本文标题:javascript - Convert Array Surrounded By Single Quotes To An Array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742396957a2467119.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论