how to split an array into two arrays in javascript?
Solution 1
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]);
}
}
Solution 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
Solution 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);
Solution 4
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' );
Solution 5
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
.
Nauman Tanwir
• 6+ years of industry experience, working as a Web UI Developer handling complete front-end. • 6+ years of experience in JavaScript, and TypeScript. • 3+ years of experience in React and Redux, Restful API, • Proficient in data visualization libraries like D3.js and Charts.js • Good understanding of scripting build files with module bundlers like Gulp, and Webpack. • Worked with CI/CD tools like Jenkins, and TeamCity. • Understanding of analytics, e-commerce domain and payment gateway integration.
Updated on July 10, 2022Comments
-
Nauman Tanwir almost 2 years
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. -
The One and Only ChemistryBlob over 7 yearsThis works but is not a general solution. Surely the OP's actual array is not only [a, b, c, d]
-
Nauman Tanwir over 7 yearsthanks for replying. what is monthly condition in your loop? I mean what is it doing ?
-
Michael over 7 yearsYou have to replace it by the condition you want to consider that the value is for monthly plans.
-
Nauman Tanwir over 7 yearsso as in my case irs ' "cpinterval": "Monthly" ' so, it would be like plans[i].cpinterval == 'Monthly'
-
Michael over 7 yearsexactly, so you will be able to create your arrays even if the data is not in the correct order
-
Barmar over 7 years
i <= plans.length
should bei < plans.length