0

I have array in java script like

Array [ no:"54", no:"55,57",no:"58",no:"60,61",no:"63"]

I want to result array like [54,55,57,58,60,61,63].

When i write code to make like this :

var bstr=[],i=0;
$.each( obj, function( key, value )
{ 
bstr[i++]=value.no;

}
alert(bstr); 

Finally my array output is [54,55,58,60,63]. Comma separated values are missing . I tried value.no.split(',') . It's returning the same array.

4
  • 2
    How is the data that you have actually arranged? Your description of it isn't valid JavaScript syntax. Commented Oct 15, 2015 at 17:03
  • jsfiddle.net/sandenay/55jq3x4s Commented Oct 15, 2015 at 17:03
  • You are treating an array like an object., at least with how you have defined it above. Commented Oct 15, 2015 at 17:03
  • 1
    @SandeepNayak . Sorry. My array object is same like you described. I given in wrong format. Commented Oct 15, 2015 at 17:58

5 Answers 5

3

You need to fix the array you are talking about. I believe you need to have array of objects:

var obj = [ {no:"54"},{ no:"55,57"},{no:"58"},{no:"60,61"},{no:"63"}];
var bstr=[],i=0;
$.each( obj, function( key, value )
{    
     $.each(value.no.split(','),function(index,item){
     	bstr[i++]=item;
     });
     

});  // You missed closing this properly
console.log(bstr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

0

I think you have array of objects. Using concat and split should give you the desired output.

var obj =  [ {"no":"54"}, {"no":"55,57"},{"no":"58"},{"no":"60,61"},{"no":"63"}]
var bstr=[],i=0;
$.each(obj, function( key, value )
{ 
    bstr = bstr.concat(value.no.split(","));
});
alert(bstr);

http://jsfiddle.net/e8jn9pj5/

Comments

0

You can do a jquery merge on 2 arrays (1 from the split and 1 from bstr). https://jsfiddle.net/v4hoLes9/

var obj = [ {no:"54"}, {no:"55,57"},{no:"58"},{no:"60,61"},{no:"63"}];
var bstr=[];
$.each( obj, function( key, value ){ 
    bstr = $.merge(bstr, value.no.split(','));
});
alert("array length: " + bstr.length + "  values: " + bstr);

Comments

0

You can use reduce and a fancy little way to make the array with JSON.parse

var arr = [ {no:"54"},{ no:"55,57"},{no:"58"},{no:"60,61"},{no:"63"}];
var updated = arr.reduce( function(previousValue, currentValue){
     var val = JSON.parse("[" + currentValue.no + "]");
     return previousValue.concat(val);
}, []);

This results in an array of numbers, not strings like you will get with split()

Comments

0

Mapped no's with join as comma separated pushing to the result array shown below

let obj = [{
  "no": "54"
}, {
  "no": "55,57"
}, {
  "no": "58"
}, {
  "no": "60,61"
}, {
  "no": "63"
}]

let result = [];
result.push(obj.map(x => x.no).join(','));

console.log(result);
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

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.