arrays - How to create a javascript function that output a list of object with a list of the object's property? -


i have piece of javascript looks following

var set = [{   name: 'a',   property1: '',   property2: '', }, {   name: 'b',   property1: '',   property2: '', }, {   name: 'c',   property1: '',   property2: '', }]; 

since property1 , property2 both empty objects, want automate such need keep track of name. like:

namelist = ['a', 'b', 'c']; magicfunction(namelist); 

that magicfunction can return set mentioned above. new javascript, if think rudimentary stackoverflow, please give me few keywords know should searching for. thanks!

you can use map

to set namelist

var namelist = ['a', 'b', 'c']; var set = namelist.map(function(e) { return { name: e, property1: 0, property2: 0 }}); 

function magicfunction(arr) {    return arr.map(function(e) {      return {        name: e,        property1: 0,        property2: 0      }    });  }    var namelist = ['a', 'b', 'c'];  set = magicfunction(namelist);    console.log(set);  document.getelementbyid('output').innerhtml = json.stringify(set, 0, 2);
<pre id="output"></pre>


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -