Search code examples
jqueryajaxeachgetjson

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


Solution

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