How to combine object array values into 1 array?


I have a data object with each object containing a tags array, I would like to loop through the object and grab each of the object tags and combine eventually combine the values into 1 array called selections on completion of the loop. At the moment however I'm unable to solve this, I have tried pushing the tags into an array then use the underscore _.union method and also tries concatenating each looped array into 1 but with no success. Can anyone recommend a solution?

Fiddle: <a href="http://jsfiddle.net/24qh7zfv/3/" rel="nofollow">http://jsfiddle.net/24qh7zfv/3/</a>


var selection = []; var data = ([ { role: "Developer", tag: "developer", tags: ["Javascript", "CSS", "HTML"] }, { role: "Producer", tag: "producer", tags: ["Project Management", "Pitching", "Billing"] } ]); for( var i = 0, len = data.length; i < len; i++ ) { selection.concat(data[i].tags); } console.log(selection);


You need to reassign selection, and concat to tho that. so use:

for( var i = 0, len = data.length; i < len; i++ ) { selection = selection.concat(data[i].tags); }


This should work.

var selection = []; var data = [ { role: "Developer", tag: "developer", tags: ["Javascript", "CSS", "HTML"] }, { role: "Producer", tag: "producer", tags: ["Project Management", "Pitching", "Billing"] } ]; for( var i = 0; i < data.length; i++ ) { for(var j = 0; j < data[i].tags.length; j++){ selection.push(data[i].tags[j]); } }


You can simply do this.

var selection = data.map(function(x){ return x.tags; }).join().split(",");


i think better use concat array function . see concat function in <a href="http://skillgun.com/javascript/arrays/tutorial" rel="nofollow">http://skillgun.com/javascript/arrays/tutorial</a>


  • WPF C# Finding controls from panel inside a panel
  • How do you stack two Pandas Dataframe columns on top of each other?
  • T-SQL Pivot Add Total Row
  • get panel at specific positions in C#
  • jQuery nested $.getJSON not working
  • Use Python to get state of combustion engine based on spark plug actuation
  • StringBuilder vs ampersand equals concatentation
  • Pandas filter dataframe rows with a specific year
  • Perl-Using same variable for nested loops considered improper?
  • AngularJS class ng-scope is not being added
  • Filter log files(_success and _log) in FileSystem.liststatus
  • karma-browserify throws error when trying to load modules shimmed with browserify-shim
  • Javascript and VERY LONG string
  • miss prototype functions when “new” a instance
  • rails 5 carrierwave no route matches for image
  • Unit test express route calls controller method?
  • Using Node cluster module with SailsJs: EADDRINUSE
  • Left fixed columns with table colspan
  • How to show underscore (shortcut) without holding Alt?
  • Linq Full Outer Join on Two Objects
  • How to best manage SMTP clients
  • Swift iOS don't recognize GCM library imports?
  • File loader changed image file name but not the file name in HTML file
  • distinct values from multiple fields within one table ORACLE SQL
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • Declaring variable dynamically in VB.net
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Get data from AJAX - How to
  • Scrapy recursive link crawler
  • Exchange data b/w iOS devices using Bluetooth 4.0
  • Bug in WPF DataGrid
  • Excel - Autoshape get it's name from cell (value)
  • Check if a string to interpolate provides expected placeholders
  • Trying to switch camera back to front but getting exception
  • RestKit - RKRequestDelegate does not exist
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Traverse Array and Display in markup
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • Programmatically clearing map cache