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.

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

本文标签: javascriptConvert Array Surrounded By Single Quotes To An ArrayStack Overflow