I have this array in the format.
var data = [{
"Month": "Jan 2017 - Check",
"balance": "0.00"
}, {
"Month": "Jan 2017 - Check",
"balance": "0.00"
}, {
"Month": "Feb 2017 - Check",
"balance": "0.00"
}, {
"Month": "Feb 2017 - Check",
"balance": "0.00"
}, {
"Month": "Mar 2017 - Check",
"balance": "0.00"
}, {
"Month": "April 2017 - Check",
"balance": "0.00"
}, {
"Month": "May 2017 - Check",
"balance": "0.00"
}, {
"Month": "May 2017 - Check",
"balance": "0.00"
}, {
"Month": "June 2017 - Check",
"balance": "0.00"
}, {
"Month": "July 2017 - Check",
"balance": "0.00"
}, {
"Month": "Aug 2017 - Check",
"balance": "0.00"
}, {
"Month": "Sept 2017 - Check",
"balance": "0.00"
}, {
"Month": "Oct 2017 - Check",
"balance": "0.00"
}, {
"Month": "Nov 2017 - Check",
"balance": "0.00"
}, {
"Month": "Dec 2017 - Check",
"balance": "0.00"
}, {
"Month": "Dec 2017 - Check",
"balance": "0.00"
}]
var label = [];
for (var i = 0; i < data.length; i++) {
var label1 = [];
var label1 = [];
label1.push(data[i].Month.split('-')[0].trim().split(" ").join(','));
label.push(label1);
}
console.log(label)
And I want to get the format
[
["June", "2015"], "July", "August", "September", "October", "November", "December", ["January", "2016"], "February", "March", "April", "May"
]
What I want is for array with space to be in another array. Reason for this is that I will use in chartJS.
What I get so far is
[
[
"Jan,2017,-,Check"
]
]
This should look like
[
[
"Jan","2017","-","Check"
]
]
Please ignore the - Check I will trim it but I have still error for now if there element is not .trim() My focus is create the same format of array
asked Jan 23, 2018 at 4:03
Giant
1,6497 gold badges34 silver badges70 bronze badges
2 Answers 2
You can use map for this.
Here is a fiddle:
var data = [{
"Month": "Jan 2017 - Check",
"balance": "0.00"
}, {
"Month": "Jan",
"balance": "0.00"
}, {
"Month": "Feb 2017 - Check",
"balance": "0.00"
}, {
"Month": "Feb",
"balance": "0.00"
}, {
"Month": "Mar",
"balance": "0.00"
}, {
"Month": "April",
"balance": "0.00"
}, {
"Month": "May 2017 - Check",
"balance": "0.00"
}, {
"Month": "May",
"balance": "0.00"
}, {
"Month": "June",
"balance": "0.00"
}, {
"Month": "July 2017 - Check",
"balance": "0.00"
}, {
"Month": "Aug",
"balance": "0.00"
}, {
"Month": "Sept",
"balance": "0.00"
}, {
"Month": "Oct 2017 - Check",
"balance": "0.00"
}, {
"Month": "Nov 2017 - Check",
"balance": "0.00"
}, {
"Month": "Dec 2017 - Check",
"balance": "0.00"
}, {
"Month": "Dec",
"balance": "0.00"
}];
var result = data.map(function(v,i){
let month = v.Month.split(" ");
if ( month.length == 1 ) return month[0];
else return [ month[0],month[1] ];
});
console.log( result );
answered Jan 23, 2018 at 4:11
Eddie
26.8k6 gold badges39 silver badges59 bronze badges
Sign up to request clarification or add additional context in comments.
7 Comments
Giant
give me a minute I check the compatibility in my source
Giant
I have last question for the trim Example I have
qwe - July - Checked and qwe - July(e-status) - Check my trim fails because there is another - my trim is like v.Month.split("-")[1].trim().split(" ") the second - is getting trim too when I need to get the July(e-status)Eddie
I dont understand what you are trying to achieve. This is not on that you desired output on your post.
Giant
it should be there but it is confusing sample string is
qwe - July(e-status) 2017 - Check now I need to get "July(e-status)", "2017" right? but when I do my split of string using the code v.Month.split("-")[1].trim().split(" ") it will fail because there is another - I want to trim only - that is between space how to do it trim?Eddie
What is
qwe? Why do you have to split("-")? |
You can use JQUERY for this.
Here is a fiddle:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var data = [{
"Month": "Jan 2017 - Check",
"balance": "0.00"
}, {
"Month": "Jan",
"balance": "0.00"
}, {
"Month": "Feb 2017 - Check",
"balance": "0.00"
}, {
"Month": "Feb",
"balance": "0.00"
}, {
"Month": "Mar",
"balance": "0.00"
}, {
"Month": "April",
"balance": "0.00"
}, {
"Month": "May 2017 - Check",
"balance": "0.00"
}, {
"Month": "May",
"balance": "0.00"
}, {
"Month": "June",
"balance": "0.00"
}, {
"Month": "July 2017 - Check",
"balance": "0.00"
}, {
"Month": "Aug",
"balance": "0.00"
}, {
"Month": "Sept",
"balance": "0.00"
}, {
"Month": "Oct 2017 - Check",
"balance": "0.00"
}, {
"Month": "Nov 2017 - Check",
"balance": "0.00"
}, {
"Month": "Dec 2017 - Check",
"balance": "0.00"
}, {
"Month": "Dec",
"balance": "0.00"
}];
var myJsonString = JSON.stringify(data);
var JsonObject = JSON.parse(myJsonString);
var label = [];
var myJsonStrings = null;
$.each(data, function (k, v) {
var label1 = [];
if (v.Month.includes("-")) {
label1.push(v.Month.split(" - Check").join(''));
label.push(label1);
}
else {
label.push(v.Month.split(" ").join(','));
}
//console.log(k + ' is ' + v.Month);
//console.log(label);
myJsonStrings = JSON.stringify(label);
});
console.log(myJsonStrings);
});
</script>
answered Jan 23, 2018 at 6:24
Akhil Singh
7307 silver badges17 bronze badges
Comments
lang-js
2015in["June", "2015"]? You need to provide more info."July 2017 - Check"i will trim it using.split('-')[0]gettingJuly 2017and then I want to get["July","2017"]but what I get is["July,2017"]I hope this is clear please comment if not["July,2017"]and strings e.g."July"? What determines if the element should be an 2-piece array vs a string?