RMH
RMH

Reputation: 831

Create a variable of another variable (json, getJson method)

I have the following viable as an example: "ratioSize": "LP555/12317 Z ABCD"

in my each function inside my getJson call:

 $.each(data.infoData, function (i, item) {
                // console.log(Rating); works just fine
                //console.log(stockNumber ); works just fine

            var Rating = this.tireRating;
            var stockNumber = this.SN;
            var ratioSize = this.TS
            var NEW_VARIALBE_I_NEED = the last 2 number of the FIRST PART of ratioSize, so in this case it is 17


        });

any ideas of how I can make that into one of my variables? I'm totally lost here

Upvotes: 1

Views: 41

Answers (2)

Luke
Luke

Reputation: 1724

Assuming the position and length of the "17" string doesn't change, we can simply grab a substring:

var ratioSize = "LP555/12317 Z ABCD";
var result = ratioSize.substring(9, 11);

Upvotes: 1

MichaelG
MichaelG

Reputation: 737

I'm not exactly sure of what you're asking, in order to get the characters you want, do this:

var Rating = this.tireRating;
var stockNumber = this.SN;
var ratioSize = this.TS
var ratioSplit=ratioSize.split(' ');
var variable=ratioSize.slice(ratioSplit[0].length-2,ratioSplit[0].length);

variable is now 17 from your example.

Upvotes: 1

Related Questions