4

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.

6 Answers 6

3

split() is a method of the String object, not of the Array object.

From what I understand from your question, you need the Array.prototype.slice() method instead:

The slice() method returns a shallow copy of a portion of an array into a new array object.

Syntax

arr.slice([begin[, end]])

In conclusion, you may want to do something like this:

var monthly_plans = plans.slice(0, 2);
var yearly_plans = plans.slice(2);
Sign up to request clarification or add additional context in comments.

Comments

2

You can use the slice(start, end) function on arrays, e.g.

monthly_plans = plans.slice(0,2);
yearly_plans = plans.slice(2,4);

More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

1 Comment

This works but is not a general solution. Surely the OP's actual array is not only [a, b, c, d]
1

And the ES5 approach:

var plans=[a, b, c, d];

var monthly_plans = plans.filter( plan => plan==='monthly condition' );
var yearly_plans = plans.filter( plan => plan==='yearly condition' );

Comments

0

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]);
  }
}

5 Comments

thanks for replying. what is monthly condition in your loop? I mean what is it doing ?
You have to replace it by the condition you want to consider that the value is for monthly plans.
so as in my case irs ' "cpinterval": "Monthly" ' so, it would be like plans[i].cpinterval == 'Monthly'
exactly, so you will be able to create your arrays even if the data is not in the correct order
i <= plans.length should be i < plans.length
0

Based on your post, the solution will not involve split(). If you know in advance which plan designations are monthly and which are yearly:

var plans = ['a', 'b', 'c', 'd', 'm', 'y', ....... 'n'],
    count = plans.length, i = 0;

var monthly_designations = ['a', 'b', 'm'],
    yearly_designations = ['c', 'd', 'y'];

for(; i < count; i++) {

    if (monthly_designations.indexOf(plans[i]) !== -1) {
        monthly_plans.push(plans[i]);
    } else {
        if (yearly_designations.indexOf(plans[i]) !== -1) {
            yearly_plans.push(plans[i]);
        }
    }

}

Then just check the plans array against the known designations to filter the contents into the correct sub-arrays monthly_plans and yearly_plans.

Comments

0

Maybe

function split2DAryOnIdx_n() {
  const 
    array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
    [leftArray, rightArray] = split2DAryOnIdx(array, 1);

console.log(leftArray); // [[1], [4], [7]]
console.log(rightArray); // [[2, 3], [5, 6], [8, 9]]

}

const 
  split2DAryOnIdx = (array, Idx) => {
    let 
     leftArray  = [],
     rightArray = [];

    for (let row of array) {
      let 
        leftElement  = row.slice(0, Idx),
        rightElement = row.slice(Idx);

      leftArray.push(leftElement);
      rightArray.push(rightElement);    
  }

  return [leftArray, rightArray];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.