javascript - Convert Array Surrounded By Single Quotes To An Array - Stack Overflow

Deeply nested in a JSON object I have the property value.actions:[ { name: 'InviteUser',type:

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.

Share Improve this question edited Dec 11, 2017 at 17:15 maxwell asked Dec 11, 2017 at 17:06 maxwellmaxwell 2,38127 silver badges36 bronze badges 7
  • 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
 |  Show 2 more ments

3 Answers 3

Reset to default 5

You 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"]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742396958a4436142.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信