0

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

6
  • 1
    I don't see the pattern. How did you get 2015 in ["June", "2015"]? You need to provide more info. Commented Jan 23, 2018 at 4:07
  • @Mikey this one "July 2017 - Check" i will trim it using .split('-')[0] getting July 2017 and then I want to get ["July","2017"] but what I get is ["July,2017"] I hope this is clear please comment if not Commented Jan 23, 2018 at 4:08
  • Agreed. Please show the same data in the source and desired blocks. Also you say it should look like "Jan","2017","-","Check" but ignore the check? Commented Jan 23, 2018 at 4:10
  • @Mikey I have updated the OP so that it will be same Commented Jan 23, 2018 at 4:11
  • 1
    Kindly update the question once more with the desired array based off the source array that you provided. There is no 2015 in your source array, so there should be no 2015 in your desired array. That in itself already causes confusion. Also, why is your desired array a mix of arrays e.g. ["July,2017"] and strings e.g. "July"? What determines if the element should be an 2-piece array vs a string? Commented Jan 23, 2018 at 4:18

2 Answers 2

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

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

7 Comments

give me a minute I check the compatibility in my source
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)
I dont understand what you are trying to achieve. This is not on that you desired output on your post.
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?
What is qwe? Why do you have to split("-")?
|
0

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>

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.