-1

I have this normal array

["Company="Google",Country="USA",Id="123"", "Company="Volvo",Country="SWEDEN",Id="999""]

And I would like to build an object out of its values.

Expected result is

  {
    "root": [{
            "Company": "Google",
            "Country": "USA",
            "Id": "123"
        }, {
            "Company": "Volvo",
            "Country": "SWEDEN",
            "Id": "999"
        }
    ]
}

How do I work this structure in JavaScript?

snorlax
  • 73
  • 8
  • 2
    There is no such thing as a "JSON object". JSON is a string format. – melpomene Feb 25 '17 at 07:46
  • 4
    Your "normal array" is a syntax error (as is your expected result). Please show what you actually have. – melpomene Feb 25 '17 at 07:47
  • What console? Can you post a screenshot? And what's the code that prints to the console? – melpomene Feb 25 '17 at 07:53
  • @melpomene var request = 'CREATE Car(Company="Google",Country="USA",Id="123"), CREATE Car(Company="Volvo",Country="SWEDEN",Id="999")'; var result = request.match(/\(([^)]+)\)/g); console.log(result); – snorlax Feb 25 '17 at 08:26
  • That returns `['CREATE Car(Company="Google",Country="USA",Id="123"', ', CREATE Car(Company="Volvo",Country="SWEDEN",Id="999"']`. – melpomene Feb 25 '17 at 08:47

1 Answers1

1

the array you posted is not valid we'll need to mix single and double quotes like this:

['Company="Google",Country="USA",Id="123"', 'Company="Volvo",Country="SWEDEN",Id="999"']

if you're getting the array as a response from a request and you copied it from console than it quotes must've been escaped using \" and you don't have to fix it.

converting the array into an object:

var myArray = ['Company="Google",Country="USA",Id="123"', 'Company="Volvo",Country="SWEDEN",Id="999"']
var myObject = array2obj(myArray);

function array2obj(myArr) {
    var myObj = {root:[]}
    myArr.forEach(function(v) {
        var currentObj = {}
        v.split(",").forEach(function(vi) {
            var tmp = vi.replace(/\"/g, "").split("=");
            currentObj[tmp[0]] = tmp[1];
        });
        myObj.root.push(currentObj);
    });
    return myObj
}

as you can see you call the function like this var myObject = array2obj(myArray) assuming your array is stored in myArray.

now you have your object in the variable myObject:

{
    "root": [
        {
            "Company": "Google",
            "Country": "USA",
            "Id": "123"
        },
        {
            "Company": "Volvo",
            "Country": "SWEDEN",
            "Id": "999"
        }
    ]
}

"reducing" the array into ids only:

as asked in comments, the following will produce a newArray = ["123", "999"]

var myArray = ['Company="Google",Country="USA",Id="123"', 'Company="Volvo",Country="SWEDEN",Id="999"'];

var newArray = myArray.map(function(x) {
    var id = /,\s*id\s*=\s*"(.*?)"/gi.exec(x);
    if (id) return id[1]
});

I'm using regex to match the id and .map() to create a new array with the matched results.

if you want the array to contain numbers and not strings replace return id[1] with return Number(id[1]) but you have to make sure ids are always numbers or you will get NaN errors

Community
  • 1
  • 1
Maher Fattouh
  • 1,742
  • 16
  • 24