javascript - Access a deeply nested field inside an array of objects -


i have code below:

var ops = [{     label: 'primary colors',     options: [{         label: 'yellow',         value: 'yellow'     }, {         label: 'red',         value: 'red'     }, {         label: 'blue',         value: 'blue'     }],     label: 'secondary colors',     options: [{         label: 'pink',         value: 'yellow'     }, {         label: 'pink',         value: 'pink'     }, {         label: 'violet',         value: 'violet'     }] } 

i want create variable contains values value field inside options array this: value = [yellow, red, blue, yellow, pink, violet]. ideas?

you can javascript foreach method

var ops=[{label: 'primary colors',  options: [{label: 'yellow',value: 'yellow'},  {label: 'red',value: 'red'},  {label: 'blue',value: 'blue'}]},  {label: 'secondary colors',  options: [{label: 'pink',value: 'yellow'},{label: 'pink',value: 'pink'},{label: 'violet',value: 'violet'}]}];          var arr =[];var obj = [];  arr = ops.foreach(function(o){    var = o.options;    a.foreach(function(option) {            obj.push(option.value);  	});  });  console.log(obj);

fiddle working demo

note: modified data have provided because closing tags(}]) not in correct order.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -