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);
note: modified data have provided because closing tags(}]) not in correct order.
Comments
Post a Comment