B Hull
B Hull

Reputation: 3223

how can i push array into other array where values match?

 var cols = [
      {id: 1, title: 'First Column', tasks:[] },
      {id: 2, title: 'Second Column', tasks:[] },
      {id: 3, title: 'Third Column', tasks:[] }
 ];

 var tasks = [
     {id: 1, title: 'Bla bla project', column: 1},
     {id: 2, title: 'Hip Hop project', column: 1}
 ]

How can i cycle through the tasks variable and push them into the cols array for the matching column id? is this possible without jquery?

Upvotes: 1

Views: 66

Answers (1)

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

Try like this

cols.map(function(col){
  col.tasks=tasks.filter(function(task){ return task.column == col.id; });
})

DEMO

Upvotes: 2

Related Questions