nt2 nt2 - 2 months ago 14
Javascript Question

how to split an array into two arrays in javascript?

I have an array

var plans=[a, b, c, d ];
with prices based monthly any yearly.

Consider- a and b are monthly and c and d are yearly.


So, I want to split the array based on the monthly and yearly values and store the values in to separate arrays

var monthly_plans=[]; and var yearly_plans=[]


So, how do I do this?

I have used the js
split()
function before but on a very basic level.

Answer

I think it will be a better avenue to use a for.

Example:

for (var i=0;i<=plans.length;i++)
{
  if(plans[i] == 'monthly condition')
  {
     monthly_plans.push(plans[i]);
  }
  else
  {
     yearly_plans.push(plans[i]);
  }
}
Comments