Вы можете объявить gamesBack
глобально, чтобы вы могли использовать его в любом месте вашего кода. Кроме того, чтобы получить значение see
, мы можем использовать each(team..
l oop, который у вас уже есть в вашем коде, сохраните значение gb
в gamesBack
, где see = 8
.
Demo Code (я удалил ajax и 1-й массив под co, потому что он не нужен в этой демонстрации):
var data = {
"sta": {
"gdte": "2020-06-17",
"co": [
{
"val": "West",
"di": [{
"val": "Northwest",
"t": [{
"tid": 1610612743,
"see": 3,
"cli": 1,
"clid": 0,
"clic": 0,
"elim": 0,
"str": "L 1",
"l10": "5-5",
"dr": "10-1",
"cr": "26-13",
"l": 22,
"w": 43,
"hr": "25-8",
"ar": "18-14",
"gb": 7.000,
"gbd": 0.000,
"gbl": "10.00000",
"ta": "DEN",
"tn": "Nuggets",
"tc": "Denver"
},
{
"tid": 1610612762,
"see": 4,
"cli": 1,
"clid": 0,
"clic": 0,
"elim": 0,
"str": "L 1",
"l10": "5-5",
"dr": "5-5",
"cr": "21-16",
"l": 23,
"w": 41,
"hr": "21-10",
"ar": "20-13",
"gb": 8.500,
"gbd": 1.500,
"gbl": "11.50000",
"ta": "UTA",
"tn": "Jazz",
"tc": "Utah"
},
{
"tid": 1610612760,
"see": 5,
"cli": 1,
"clid": 0,
"clic": 0,
"elim": 0,
"str": "W 3",
"l10": "8-2",
"dr": "7-4",
"cr": "25-15",
"l": 24,
"w": 40,
"hr": "20-13",
"ar": "20-11",
"gb": 9.500,
"gbd": 2.500,
"gbl": "12.50000",
"ta": "OKC",
"tn": "Thunder",
"tc": "Oklahoma City"
},
{
"tid": 1610612757,
"see": 9,
"cli": 0,
"clid": 0,
"clic": 0,
"elim": 0,
"str": "W 1",
"l10": "4-6",
"dr": "4-8",
"cr": "16-26",
"l": 37,
"w": 29,
"hr": "18-14",
"ar": "11-23",
"gb": 21.500,
"gbd": 14.500,
"gbl": "24.50000",
"ta": "POR",
"tn": "Trail Blazers",
"tc": "Portland"
},
{
"tid": 1610612750,
"see": 14,
"cli": 0,
"clid": 0,
"clic": 0,
"elim": 1,
"str": "L 3",
"l10": "3-7",
"dr": "2-10",
"cr": "9-30",
"l": 45,
"w": 19,
"hr": "8-24",
"ar": "11-21",
"gb": 30.500,
"gbd": 23.500,
"gbl": "33.50000",
"ta": "MIN",
"tn": "Timberwolves",
"tc": "Minnesota"
}
]
},
{
"val": "Pacific",
"t": [{
"tid": 1610612747,
"see": 1,
"cli": 1,
"clid": 0,
"clic": 0,
"elim": 0,
"str": "L 1",
"l10": "8-2",
"dr": "9-2",
"cr": "33-7",
"l": 14,
"w": 49,
"hr": "23-8",
"ar": "26-6",
"gb": 0.000,
"gbd": 0.000,
"gbl": "3.00000",
"ta": "LAL",
"tn": "Lakers",
"tc": "Los Angeles"
},
{
"tid": 1610612746,
"see": 2,
"cli": 1,
"clid": 0,
"clic": 0,
"elim": 0,
"str": "W 1",
"l10": "7-3",
"dr": "8-4",
"cr": "27-14",
"l": 20,
"w": 44,
"hr": "25-7",
"ar": "19-13",
"gb": 5.500,
"gbd": 5.500,
"gbl": "8.50000",
"ta": "LAC",
"tn": "Clippers",
"tc": "LA"
},
{
"tid": 1610612758,
"see": 11,
"cli": 0,
"clid": 0,
"clic": 0,
"elim": 0,
"str": "L 1",
"l10": "7-3",
"dr": "7-5",
"cr": "20-20",
"l": 36,
"w": 28,
"hr": "14-17",
"ar": "14-19",
"gb": 21.500,
"gbd": 21.500,
"gbl": "24.50000",
"ta": "SAC",
"tn": "Kings",
"tc": "Sacramento"
},
{
"tid": 1610612756,
"see": 13,
"cli": 0,
"clid": 0,
"clic": 0,
"elim": 0,
"str": "L 1",
"l10": "4-6",
"dr": "5-9",
"cr": "15-27",
"l": 39,
"w": 26,
"hr": "13-22",
"ar": "13-17",
"gb": 24.000,
"gbd": 24.000,
"gbl": "27.00000",
"ta": "PHX",
"tn": "Suns",
"tc": "Phoenix"
},
{
"tid": 1610612744,
"see": 15,
"cli": 0,
"clid": 0,
"clic": 0,
"elim": 1,
"str": "L 1",
"l10": "3-7",
"dr": "2-11",
"cr": "9-34",
"l": 50,
"w": 15,
"hr": "8-26",
"ar": "7-24",
"gb": 35.000,
"gbd": 35.000,
"gbl": "38.00000",
"ta": "GSW",
"tn": "Warriors",
"tc": "Golden State"
}
]
},
{
"val": "Southwest",
"t": [{
"tid": 1610612745,
"see": 6,
"cli": 1,
"clid": 0,
"clic": 0,
"elim": 0,
"str": "W 1",
"l10": "6-4",
"dr": "7-4",
"cr": "25-17",
"l": 24,
"w": 40,
"hr": "22-10",
"ar": "18-14",
"gb": 9.500,
"gbd": 0.000,
"gbl": "12.50000",
"ta": "HOU",
"tn": "Rockets",
"tc": "Houston"
},
{
"tid": 1610612742,
"see": 7,
"cli": 0,
"clid": 0,
"clic": 0,
"elim": 0,
"str": "W 1",
"l10": "6-4",
"dr": "10-3",
"cr": "25-15",
"l": 27,
"w": 40,
"hr": "19-15",
"ar": "21-12",
"gb": 11.000,
"gbd": 1.500,
"gbl": "14.00000",
"ta": "DAL",
"tn": "Mavericks",
"tc": "Dallas"
},
{
"tid": 1610612763,
"see": 8,
"cli": 0,
"clid": 0,
"clic": 0,
"elim": 0,
"str": "L 1",
"l10": "4-6",
"dr": "4-7",
"cr": "19-22",
"l": 33,
"w": 32,
"hr": "18-15",
"ar": "14-18",
"gb": 18.000,
"gbd": 8.500,
"gbl": "21.00000",
"ta": "MEM",
"tn": "Grizzlies",
"tc": "Memphis"
},
{
"tid": 1610612740,
"see": 10,
"cli": 0,
"clid": 0,
"clic": 0,
"elim": 0,
"str": "W 2",
"l10": "5-5",
"dr": "3-8",
"cr": "17-25",
"l": 36,
"w": 28,
"hr": "13-19",
"ar": "15-17",
"gb": 21.500,
"gbd": 12.000,
"gbl": "24.50000",
"ta": "NOP",
"tn": "Pelicans",
"tc": "New Orleans"
},
{
"tid": 1610612759,
"see": 12,
"cli": 0,
"clid": 0,
"clic": 0,
"elim": 0,
"str": "W 1",
"l10": "5-5",
"dr": "4-6",
"cr": "15-21",
"l": 36,
"w": 27,
"hr": "16-14",
"ar": "11-22",
"gb": 22.000,
"gbd": 12.500,
"gbl": "25.00000",
"ta": "SAS",
"tn": "Spurs",
"tc": "San Antonio"
}
]
}
]
}
]
}
};
var gbEight = ""; //decalre globally
jQuery(function() {
var conference = data.sta.co[0];
// console.log(conference);
var confStandings = [];
var games = [];
var sorted;
confStandings += (
'<div class="standings col-md-12">\
<div class="table">\
<div class="thead">\
<div class="tr">\
<span data-toggle="tooltip" data-placement="top" title="Wins">W</span>\
<span data-toggle="tooltip" data-placement="top" title="Losses">L</span>\
<span data-toggle="tooltip" data-placement="top" title="Percentage">PCT</span>\
<span data-toggle="tooltip" data-placement="top" title="Games Back">GB</span>\
<span data-toggle="tooltip" data-placement="top" title="Home Record">HR</span>\
<span data-toggle="tooltip" data-placement="top" title="Away Record">AR</span>\
<span data-toggle="tooltip" data-placement="top" title="Division Record">DR</span>\
<span data-toggle="tooltip" data-placement="top" title="Conference Record">CR</span>\
</div>\
</div>\
<div class="tbody">'
);
var region = data.sta.co[0].di;
jQuery.each(region, function(i, reg) {
var team = reg.t;
jQuery.each(team, function(i, name) {
var eight = (name.see == 8);
if (eight == true) {
gbEight = (name.gb); //getting value of gb
console.log(gbEight)
}
var wins = parseFloat(name.w);
var losses = parseFloat(name.l);
var total = wins + losses;
var percent = (wins / total).toFixed(3);
var width = (percent * 100).toFixed(1);
var abbr = name.ta.toLowerCase();
games.push({
tid: name.tid,
tn: name.tn,
cli: name.cli,
ta: abbr,
wdth: width,
w: name.w,
l: name.l,
pct: percent,
gb: name.gb,
hr: name.hr,
ar: name.ar,
dr: name.dr,
cr: name.cr,
see: name.see,
elim: name.elim
});
sorted = games.sort(function(a, b) {
if (a.see > b.see) {
return 1;
}
if (a.see < b.see) {
return -1;
}
return 0;
});
});
});
jQuery.each(sorted, function(i, val) {
confStandings += (
'<div class="team">');
if (val.see < 9) {
confStandings += ('<div class="trow in ' + val.ta + '-row">');
}
if (val.gb - gbEight <= 4) {
confStandings += ('<div class="trow in-race ' + val.ta + '-row">');
}
if (val.gb - gbEight > 4) {
confStandings += ('<div class="trow out ' + val.ta + '-row">');
}
if (val.elim == 1) {
confStandings += ('<div class="trow out ' + val.ta + '-row">');
}
confStandings += (
'<div class="tdata">\
<div class="bar" style="width: ' + val.wdth + '%;"></div><div class="key">');
if (val.see < 9) {
confStandings += ('<small class="indicator see">' + val.see);
if (val.cli > 0) {
confStandings += ('x');
}
confStandings += ('</small>');
}
if (val.elim == 1) {
confStandings += ('<small class="indicator elim">o</small>');
}
confStandings += ('</div><div class="logo ' + val.ta + '" data-toggle="tooltip" data-placement="top" title="' + val.tn + '">' + val.tn + '</div>\
<div class="tstats">\
<span data-title="W"><em>' + val.w + '</em></span>\
<span data-title="L"><em>' + val.l + '</em></span>\
<span data-title="PCT"><em>' + val.pct + '</em></span>\
<span data-title="GB"><em>' + val.gb + '</em></span>\
<span data-title="HR" class="hideMobile"><em>' + val.hr + '</em></span>\
<span data-title="AR" class="hideMobile"><em>' + val.ar + '</em></span>\
<span data-title="DR" class="hideMobile"><em>' + val.dr + '</em></span>\
<span data-title="CR" class="hideMobile"><em>' + val.cr + '</em></span>\
</div>\
</div>\
</div></div>');
});
confStandings += ('</div></div></div>');
games = [];
jQuery('#dataConfStandings').append(confStandings);
function initToolTip() {
jQuery('[data-toggle="tooltip"]').tooltip();
}
// Ordinals
function ordinal_suffix(i) {
var j = i % 10,
k = i % 100;
if (j == 1 && k != 11) {
return i + "st";
}
if (j == 2 && k != 12) {
return i + "nd";
}
if (j == 3 && k != 13) {
return i + "rd";
}
return i + "th";
}
});
.in-race{
color : green;
}
.out {
color : red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="dataConfStandings"></div>