weixin_33717298 2019-11-15 19:28 采纳率: 0%
浏览 33

如何使用jQuery做到这一点

So I have a list of items here :

347366834 AceofSpades
460724140   TheJadeRabbit
814876685   TrinityGhoul
1345867570  SweetBusiness
1508896098  TheWardcliffCoil
1852863732  Wavesplitter
2044500762  TheQueenbreaker
2146650065  PrometheusLens
2694576561  Two-TailedFox
2896466320  TheJadeRabbit
3141979346  DARCI
2978016230  TheJadeRabbit
3229272315  TheJadeRabbit
3413860062  TheChaperone
3512014804  Lumina
3580904580  LegendofAcrius
3628991658  GravitonLance
3844694310  TheJadeRabbit
4036115577  SleeperSimulant
4255268456  Skyburner'sOath
4190156464  Merciless
3973202132  Thorn
3588934839  LeMonarque
3766045777  BlackTalon
3549153979  TheProspector
3437746471  Crimson
3141979347  Borealis
3413074534  PolarisLance
3211806999  Izanagi'sBurden
3110698812  Tarrabah
2907129557  Sunshot
2130065553  Arbalest
1891561814  WhisperoftheWorm
1744115122  LegendofAcrius
1364093401  TheLastWord
1331482397  MIDAMulti-Tool
814876684   Wish-Ender
417164956   Jötunn
204878059   Malfeasance
19024058    PrometheusLens
546372301   TheJadeRabbit
400096939   OutbreakPerfected
1201830623  Truth
1345867571  Coldheart
1541131350  Cerberus+1
1864563948  WorldlineZero
2069224589  OneThousandVoices
2816212794  BadJuju
3089417789  Riskrunner
3325463374  Thunderlord
2907129556  Sturm
3413860063  LordofWolves
3549153978  FightingLion
3580904581  TractorCannon
3628991659  VigilanceWing

This list is a list of item hashes from Destiny 2 api, as well as the names that correlate them.

If I make the ajax request

https://www.bungie.net/Platform/Destiny2/2/Account/4611686018472217782/Character/0/Stats/UniqueWeapons/

It gives me the Id's but not the names. My question is, is there a way for me to use jQuery to take the response which looks like this:

"Response": {
  "weapons": [{
        "referenceId": 2208405142,
        "values": {
          "uniqueWeaponAssists": {
            "statId": "uniqueWeaponAssists",
            "basic": {
              "value": 0.0,
              "displayValue": "0"
            }
          },
          "uniqueWeaponAssistDamage": {
            "statId": "uniqueWeaponAssistDamage",
            "basic": {
              "value": 0.0,
              "displayValue": "0"
            }
          },
          "uniqueWeaponKills": {
            "statId": "uniqueWeaponKills",
            "basic": {
              "value": 2069.0,
              "displayValue": "2069"
            }
          },
          "uniqueWeaponPrecisionKills": {
            "statId": "uniqueWeaponPrecisionKills",
            "basic": {
              "value": 0.0,
              "displayValue": "0"
            }
          },
          "uniqueWeaponKillsPrecisionKills": {
            "statId": "uniqueWeaponKillsPrecisionKills",
            "basic": {
              "value": 0.0,
              "displayValue": "0%"
            }
          }
        }
      },
      {
        "referenceId": 2286143274,
        "values": {
          "uniqueWeaponAssists": {
            "statId": "uniqueWeaponAssists",
            "basic": {
              "value": 0.0,
              "displayValue": "0"
            }
          },
          "uniqueWeaponAssistDamage": {
            "statId": "uniqueWeaponAssistDamage",
            "basic": {
              "value": 0.0,
              "displayValue": "0"
            }
          },
          "uniqueWeaponKills": {
            "statId": "uniqueWeaponKills",
            "basic": {
              "value": 1529.0,
              "displayValue": "1529"
            }
          },
          "uniqueWeaponPrecisionKills": {
            "statId": "uniqueWeaponPrecisionKills",
            "basic": {
              "value": 532.0,
              "displayValue": "532"
            }
          },
          "uniqueWeaponKillsPrecisionKills": {
            "statId": "uniqueWeaponKillsPrecisionKills",
            "basic": {
              "value": 0.34793982995421846,
              "displayValue": "35%"
            }
          }
        }
      },

to have it where jQuery takes the response and pairs it with the proper name where in the end I would have 2 objects (more objects going down such as weapontwo weaponthree ect).

weaponOneName = response.weapons.referenceid,
weaponOneValue = response.weapons.uniqueWeaponKills;

My thought was:

if response.weapons.referenceId = 3973202132 then referenceid = thorn 

or something similar

  • 写回答

1条回答 默认 最新

  • weixin_33681778 2019-11-15 20:50
    关注

    Use an object like this:

    var names = {
        347366834: 'AceofSpades',
        460724140: 'TheJadeRabbit',
        814876685: 'TrinityGhoul',
        1345867570: 'SweetBusiness',
    }
    

    then you can just do this:

    // for example, the first weapon referenceId is 3973202132 (Thorn)
    var weaponId = response.weapons[0].referenceId
    var weaponName = names[weaponId]
    console.log(weaponName) // Thorn
    

    But if the names of the weapons has to be a list, you have to convert that list into an object. Something like this could work:

    var referenceText = `347366834 AceofSpades
    460724140   TheJadeRabbit
    814876685   TrinityGhoul
    1345867570  SweetBusiness`;
    
    // Just in case of an accidental whitespace character
    referenceText = referenceText.trim();
    
    // split this text into an array of lines (
     are newline characters)
    var items = reference.split(/[
    ]/g);
    var names = {};
    // for each line in the array
    items.forEach(function(item) {
        /*
            So this is RegExp. Since the amount of space characters between
            the name of the weapon and the ID varies, we use a regex which basically
            says that "Just find as many spaces as possible, and
            split the line into segments there
        */
        var data = item.split(/\s+/);
        // the data array looks something like this ['347366834', 'AceofSpades']
        var weaponId = data[0];
        var weaponName = data[1];
    
        // store the weapon name
        names[weaponId] = weaponName
    });
    
    console.log(names);
    
    // both work, you can use an int or a string, but it's better to use a string 
    console.log(names[347366834]);
    console.log(names['347366834']);
    
    评论

报告相同问题?