1

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);
0

5 Answers 5

2

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));
Sign up to request clarification or add additional context in comments.

2 Comments

awesome thanks, that code threw an error pointing to the split function: var s = ary.split(/\s?\:\s?/);
Sorry, left out the increment.
2

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);

2 Comments

Awesome. What would I change if I was trying to have everything in one object instead of separate objects for each stringArray[i]? Also, the outer array brackets still exist in your above implementation.
@devdropper87, updated my answer to remove the brackets. Yes, for each is fine to fill up an object instead
1

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>");

2 Comments

This version deletes some entries all together. Also poor Tre Mason is always attached to Marshawn Lynch!
awesome, but now C.J. Anderson is missing his first name :)
1

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'
...
}

1 Comment

This version deletes some entries all together. Also poor Tre Mason is always attached to Marshawn Lynch!
1

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;});
});

2 Comments

This version deletes some entries all together. Also poor Tre Mason is always attached to Marshawn Lynch!
@devdropper87 I just double checked, and I get all the players: jsfiddle.net/nbgtm3cj/1 . As for Tre Mason, I assumed it was a typo in your post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.