如何使用jQuery实现这一点
我这里有一个项目列表:如何使用jQuery实现这一点,jquery,json,ajax,Jquery,Json,Ajax,我这里有一个项目列表: 347366834 AceofSpades 460724140 TheJadeRabbit 814876685 TrinityGhoul 1345867570 SweetBusiness 1508896098 TheWardcliffCoil 1852863732 Wavesplitter 2044500762 TheQueenbreaker 2146650065 PrometheusLens 2694576561 Two-TailedFox 2896
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
此列表是Destiny 2 api中的项哈希列表,以及与它们相关的名称
如果我提出ajax请求
https://www.bungie.net/Platform/Destiny2/2/Account/4611686018472217782/Character/0/Stats/UniqueWeapons/
它给了我身份证,但没有名字。我的问题是,有没有一种方法可以让我使用jQuery获取如下所示的响应:
"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%"
}
}
}
},
为了让jQuery接收响应并将其与适当的名称配对,最终我将有2个对象(更多的对象,如weapontwo-weaponthree等)
我的想法是:
if response.weapons.referenceId = 3973202132 then referenceid = thorn
或者类似的东西使用如下对象:
var名称={
347366834:“AceofSpades”,
460724140:“贾德拉比特”,
814876685:“三一食尸鬼”,
1345867570:“糖果生意”,
}
然后你可以这样做:
// for example, the first weapon referenceId is 3973202132 (Thorn)
var weaponId = response.weapons[0].referenceId
var weaponName = names[weaponId]
console.log(weaponName) // Thorn
但是如果武器的名称必须是一个列表,你必须将该列表转换为一个对象。类似这样的方法可能会奏效:
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 (\r\n are newline characters)
var items = reference.split(/[\r\n]/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']);
使用如下对象:
var名称={
347366834:“AceofSpades”,
460724140:“贾德拉比特”,
814876685:“三一食尸鬼”,
1345867570:“糖果生意”,
}
然后你可以这样做:
// for example, the first weapon referenceId is 3973202132 (Thorn)
var weaponId = response.weapons[0].referenceId
var weaponName = names[weaponId]
console.log(weaponName) // Thorn
但是如果武器的名称必须是一个列表,你必须将该列表转换为一个对象。类似这样的方法可能会奏效:
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 (\r\n are newline characters)
var items = reference.split(/[\r\n]/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']);
那么,即使列表每次都是以随机顺序生成的,这仍然有效吗?@apnp_apnu抱歉回答得太晚了。是的,只要'referenceText'变量的格式保持不变(即,左边的武器ID+几个空格+右边的武器名称),它就可以用于任何排序,所以即使列表每次都是以随机顺序生成的,它也可以工作吗?@apnp_apnu抱歉回答得太晚。是的,只要'referenceText'变量的格式保持不变(即,左边的武器ID+几个空格+右边的武器名称),它就可以用于任何排序