devdropper87
devdropper87

Reputation: 4207

Converting an array of strings, where each string is a key value pair, to an object

I'm new to JS and was wondering What is the best way to convert the following, in JavaScript, to an object? I'd like to preserve the key value pairs that are stringified.

[
    "\"Matt Forte : 17",
    "C.J. Anderson : 16",
    "Jamaal Charles : 16",
    "Eddie Lacy : 15",
    "Andre Ellington : 14",
    "LeSean McCoy : 14",
    "Marshawn Lynch : 14Tre Mason : 13",
    "Latavius Murray : 13",
    "Rashad Jennings : 12",
    "Alfred Morris : 12",
    "Jonathan Stewart : 12",
    "Doug Martin : 12",
    "Chris Ivory : 12",
    "\""
]

I tried this but it did not work! Are there any builtins that would do something like this for me? Perhaps I should have stored the data as an object to begin with...

var players = [
    "\"Matt Forte : 17",
    "C.J. Anderson : 16",
    "Jamaal Charles : 16",
    "Eddie Lacy : 15",
    "Andre Ellington : 14",
    "LeSean McCoy : 14",
    "Marshawn Lynch : 14Tre Mason : 13",
    "Latavius Murray : 13",
    "Rashad Jennings : 12",
    "Alfred Morris : 12",
    "Jonathan Stewart : 12",
    "Doug Martin : 12",
    "Chris Ivory : 12",
    "\""
];

var obj = {};
for (var i = 0; i< players.length; i++) {
    var tuple = players[i].split(":");
    console.log(tuple);
    for(var key in obj){
        key = tuple[0];
        obj[key] = tuple[1];

    }
};

console.log(obj);

Upvotes: 1

Views: 72

Answers (5)

user2575725
user2575725

Reputation:

Try using RegExp to split the key and value:

var arr = [
  "\"Matt Forte : 17",
  "C.J. Anderson : 16",
  "Jamaal Charles : 16",
  "Eddie Lacy : 15",
  "Andre Ellington : 14",
  "LeSean McCoy : 14",
  "Marshawn Lynch : 14Tre Mason : 13",
  "Latavius Murray : 13",
  "Rashad Jennings : 12",
  "Alfred Morris : 12",
  "Jonathan Stewart : 12",
  "Doug Martin : 12",
  "Chris Ivory : 12",
  "\""
];
//re-build proper key-value pairs
arr = arr.join('').match(/([\w|\.\s]*\:\s\d*)/g);
var obj = {};
var l = arr.length;
var tmp;
var rgx = /\s?\:\s?/;
while (l--) {
  tmp = arr[l].split(rgx);
  obj[tmp[0]] = tmp[1];
}
document.write("<pre>");
document.write(JSON.stringify(arr));
document.write("<br/>");
document.write(JSON.stringify(obj, 0, 4));
document.write("</pre>");

Upvotes: 1

Christophe
Christophe

Reputation: 28154

Unfortunately, all the posted answers using split break on the last item of your array. Here is a slightly more complicated way that should work fine:

var object = {};
players.map(function(string) {
  string.replace(/(.+) : (\d+)/,function($0,$1,$2){object[$1]=$2;});
});

Upvotes: 1

Richard Hamilton
Richard Hamilton

Reputation: 26444

Use the map function to create two new arrays, containing the keys and values respectively.

Create an empty object, then iterate over either the keys or values array.

var names = players.map(function(player) { return player.split(':')[0] }
var score = players.map(function(player) { return player.split(':')[1] }

var object = {}

for (var i = 0; i < names.length; i++) {
   object[names[i]] = score[i];
}

Here's what my output looks like

{ 'Matt Forte': '17',
  'C.J. Anderson': '16',
  'Jamaal Charles': '16',
  'Eddie Lacy': '15'
...
}

Upvotes: 1

AmmarCSE
AmmarCSE

Reputation: 30597

Use Array.prototype.map

var stringArray = [
  "\"Matt Forte : 17",
  "C.J. Anderson : 16",
  "Jamaal Charles : 16",
  "Eddie Lacy : 15",
  "Andre Ellington : 14",
  "LeSean McCoy : 14",
  "Marshawn Lynch : 14Tre Mason : 13",
  "Latavius Murray : 13",
  "Rashad Jennings : 12",
  "Alfred Morris : 12",
  "Jonathan Stewart : 12",
  "Doug Martin : 12",
  "Chris Ivory : 12",
  "\""
];

var objectArray = stringArray.map(function(string) {
  var splat = string.replace("\"", "").split(' : ');
  var obj = {};
  obj[splat[0]] = splat[1];
  return obj;
});

console.log(objectArray);

Upvotes: 2

StackSlave
StackSlave

Reputation: 10627

Like this:

function prestoChango(ary){
  var o = {};
  for(var i=0,l=ary.length; i<l; i++){
    var s = ary[i].split(/\s*\:\s*/);
    o[s[0]] = +s[1];
  }
  return o;
}
console.log(prestoChango(arrayOfStrings));

Upvotes: 2

Related Questions