Augustus Brennan Augustus Brennan - 4 months ago 27
Javascript Question

Why is it pushing the parameter name and not the element? JavaScript

Info: I'm sorting an array of names into two arrays, one has names starting with A-M, N-Z in the other. I want each entry to be an object with the name as the property and an empty string as the value, like this --> {'Name' : ' '} but instead all of the entries are like this --> {val: ' '}

This is how I'm pushing entries --> arrAM.push({val: ' '});

Question: How do I need to modify ^^^^ this method to get the Name in there instead of val. Thanks for any help!

var separate = function(array){
var arrAM = [];
var arrNZ = [];
_.each(array, function(val){

if (/^[a-m]/i.test(val)){
arrAM.push({val: ''});
arrNZ.push({val: ''})
return arrAM;


ES6 has a syntactic sugar for computed property names in object initializers:

arrAM.push({[val]: ''});

Pre-ES6 requires you to use bracket syntax in a separate statement:

var obj = {};
obj[val] = '';