0

I have below string.

18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147

I want to fetch every 5 number and remove from the string means if start from 18 to 30 then 18 to 30 will remove from this string now string like

33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147

-

var allval = jQuery('.implodearrayhide').val();
       var str_split = allval.split(",");

I am trying this code but didn't userstand how i remove 5 number when loop complete after 5

1
  • Sorry I'm not sure what you mean by loop? Where are you looping? If you want to split this into an array of arrays, all of 5 or fewer elements then you can do this `var groups = []; while (nums.length > 0) { groups.push(nums.splice(0,5)); } Commented Dec 30, 2013 at 14:00

4 Answers 4

2

You can use Array.filter like so:

var split = allval.split(",").filter(function(i) {
    return i % 5 === 0;
});
Sign up to request clarification or add additional context in comments.

Comments

1

I guess you're looking for slice:

> str = "18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147"
> newstr = str.split(",").slice(5).join()
"33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147"

Comments

0

Use array.slice(i, i+4) like in http://jsfiddle.net/k2kST/

I am guessing that you want to remove every 5th element of the array...

Comments

0

JS code

var str = "18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147";
while (str.length > 0) {
    str = str.replace(str.split(",", 5).join(), "").replace(/^,/, "");
    console.log(str);
}

O/P

33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147
48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147
63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147
78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147
93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147
108,111,114,117,120,123,126,129,132,135,138,141,144,147
123,126,129,132,135,138,141,144,147
138,141,144,147

JSfiddle

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.