How to convert array to json string in javascript having indexes inside the element

Mrugesh Thaker

My javascript array is :

 [
            "Action=Complete",
            "Echo=echotest",
            "PayerID=1675506249460924",
            "ObfuscatedAccountNumber=*********************",
            "ExpireDate=1225",
            "CardholderName=John Q. Test",
            "Address1=123 A. Street",
            "Address2=",
            "Address3=",
            "City=Orem",
            "State=UT",
            "PostalCode=84058",
            "Country=USA",
            "PaymentMethodId=630337dc-3537-4db1-bfaf-be98e838b3a5"
        ]

I want to convert it to

{
        "Action":"Complete",
        "Echo":"echotest",
        "PayerID":"1675506249460924",
        "ObfuscatedAccountNumber":"***************",
        "ExpireDate":"1225",
        "CardholderName":"John Q. Test",
        "Address1":"123 A. Street",
        "Address2":,
        "Address3":,
        "City":"Orem",
        "State":"UT",
        "PostalCode":"84058",
        "Country":"USA",
        "PaymentMethodId":"630337dc-3537-4db1-bfaf-be98e838b3a5"
    }

is there any way I can achieve that?

Nicholas

This is a hacky solution of mine.

var array = [
    "Action=Complete",
    "Echo=echotest",
    "PayerID=1675506249460924",
    "ObfuscatedAccountNumber=*********************",
    "ExpireDate=1225",
    "CardholderName=John Q. Test",
    "Address1=123 A. Street",
    "Address2=",
    "Address3=",
    "City=Orem",
    "State=UT",
    "PostalCode=84058",
    "Country=USA",
    "PaymentMethodId=630337dc-3537-4db1-bfaf-be98e838b3a5"
]

// In ES5
// Declare an empty object.
var obj = {};
array.forEach(function (str) {
   var data = str.split("=");
   obj[data[0]] = data[1];
});

OR

// In ES6
// Declare an empty object.
var obj = {}
array.forEach(str => {
    [key, value] = str.split("=")
    obj[key] = value
})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to convert dynamic JSON string to javascript array

From Dev

How to transform string with objects and array indexes into json

From Dev

javascript - convert string to json array

From Dev

javascript - convert string to json array

From Dev

how to convert JSON array to string ..?

From Dev

How to convert JSON into an array or string?

From Dev

In javascript how to convert string to array and array to string

From Dev

How to display Json Array data inside UICollectionViewCell having UITableview rows?

From Dev

Javascript how to deep clone a multidimensional array having object inside it?

From Dev

Convert json string with duplicate keys into javascript array

From Dev

How to convert array or string to array in JavaScript?

From Dev

How to convert a jquery array to a string array in javascript

From Dev

Encode array to JSON string without array indexes

From Dev

JavaScript - Insert an element to an array without affecting the indexes?

From Dev

how to convert json array to javascript array

From Dev

How to convert an array of JSON into an array of javascript object?

From Dev

How to change inside array element to object element in javascript?

From Dev

How to convert a string inside an array to array and combine both together?

From Dev

how to get json values inside an array in javascript

From Dev

how to get json values inside an array in javascript

From Dev

How to check for duplicated inside this JSON Array in JavaScript?

From Dev

How to Convert String Array JSON in a Java Object

From Dev

How to convert json object to string array

From Dev

How to convert string to nested json array

From Dev

How to convert JSON string to a PHP array?

From Dev

How to push an array inside another array element as a new property in JavaScript?

From Dev

How to convert String "[7, 9]" to Array in Javascript

From Dev

How to convert JSON string values to lowercase in Javascript?

From Dev

How to convert JSON string values to lowercase in Javascript?

Related Related

  1. 1

    How to convert dynamic JSON string to javascript array

  2. 2

    How to transform string with objects and array indexes into json

  3. 3

    javascript - convert string to json array

  4. 4

    javascript - convert string to json array

  5. 5

    how to convert JSON array to string ..?

  6. 6

    How to convert JSON into an array or string?

  7. 7

    In javascript how to convert string to array and array to string

  8. 8

    How to display Json Array data inside UICollectionViewCell having UITableview rows?

  9. 9

    Javascript how to deep clone a multidimensional array having object inside it?

  10. 10

    Convert json string with duplicate keys into javascript array

  11. 11

    How to convert array or string to array in JavaScript?

  12. 12

    How to convert a jquery array to a string array in javascript

  13. 13

    Encode array to JSON string without array indexes

  14. 14

    JavaScript - Insert an element to an array without affecting the indexes?

  15. 15

    how to convert json array to javascript array

  16. 16

    How to convert an array of JSON into an array of javascript object?

  17. 17

    How to change inside array element to object element in javascript?

  18. 18

    How to convert a string inside an array to array and combine both together?

  19. 19

    how to get json values inside an array in javascript

  20. 20

    how to get json values inside an array in javascript

  21. 21

    How to check for duplicated inside this JSON Array in JavaScript?

  22. 22

    How to Convert String Array JSON in a Java Object

  23. 23

    How to convert json object to string array

  24. 24

    How to convert string to nested json array

  25. 25

    How to convert JSON string to a PHP array?

  26. 26

    How to push an array inside another array element as a new property in JavaScript?

  27. 27

    How to convert String "[7, 9]" to Array in Javascript

  28. 28

    How to convert JSON string values to lowercase in Javascript?

  29. 29

    How to convert JSON string values to lowercase in Javascript?

HotTag

Archive