2017-12-04 3 views
0

Wahrscheinlich eine sehr schnelle Lösung, aber ich bin fest, was es ist - ich bekomme Informationen von einer API (https://api.roleplay.co.uk/v1/player/76561198062083666), wo die Anzahl der Fahrzeuge ändert sich, zusammen mit der Klassenname und Klassenart. Ich versuche, eine Liste der Namen von Fahrzeugen zu erstellen, die durch Kommas getrennt sind, und gebe dann jedem Fahrzeugnamen die Klasse seines Typs an, so dass ich sie anhand ihres Typs (z. B. Auto, Boot, Flugzeug usw.) einfärben kann. . Ich glaube, dass mein Code hier nicht funktioniert, weil man nicht mehrere Variablen hinter die .join stellen kann, aber wie komme ich daran vorbei?Skript sagt mir, dass es keine Funktion ist ERROR

var vehicleList = data.vehicles.map(v => v.classname); 
var vehicleType = data.vehicles.map(v => v.classtype); 

var content = ("<span class='" + vehicleType + "'>"+ vehicleList + "</span>).join(", "); 

document.getElementById("vehicle").textContent = content; 

Es gibt mir die folgende Fehlermeldung:

player.js:404 Uncaught TypeError: (((("<span class='" + vehicleType) + "'>") + vehicleList) + "</span>").join is not a function 
    at Object.success (player.js:404) 
    at i (jquery.min.js:2) 
    at Object.fireWith [as resolveWith] (jquery.min.js:2) 
    at A (jquery.min.js:4) 
    at XMLHttpRequest.<anonymous> (jquery.min.js:4) 
+0

Mit dieser api (https://api.roleplay.co.uk/v1/player/76561198062083666) I Ich versuche die Namen der Fahrzeuge abzurufen und setze sie in einen durch Kommas getrennten Absatz (zB "Offroad, Quadbike, etc.") und gebe sie dann jeweils Fahrzeug eine Klasse seines classtype, also kann ich sie basierend darauf färben, wenn sie ein Auto, ein Boot etc. sind. Wenn ich .join() auf einer Schnur nicht verwenden kann, was sind meine Wahlen? –

Antwort

1

Ich glaube, das erreicht, was Sie versuchen zu tun.

var data = { 
 
    vehicles: [ 
 
    { classtype: "test1", classname: "A" }, 
 
    { classtype: "test2", classname: "B" }, 
 
    { classtype: "test3", classname: "C" } 
 
    ] 
 
} 
 

 
var content = data.vehicles.map(v => "<span class='" + v.classtype + "'>" + v.classname + "</span>").join(", "); 
 

 
document.getElementById("vehicle").innerHTML = content;
<div id="vehicle"></div>

Edit:

var data = { 
 
    "uid": 169243, 
 
    "name": "PC Ant Arni", 
 
    "steamid": "76561198062083666", 
 
    "forumname": null, 
 
    "forumid": null, 
 
    "forumurl": null, 
 
    "steam": { 
 
     "name": "Ant Arni", 
 
     "profileurl": "https:\/\/steamcommunity.com\/id\/Antarni\/", 
 
     "avatarSmall": "https:\/\/steamcdn-a.akamaihd.net\/steamcommunity\/public\/images\/avatars\/25\/25f4a505cbce0a9b1deacb7468e18898bcff484f.jpg", 
 
     "avatarMedium": "https:\/\/steamcdn-a.akamaihd.net\/steamcommunity\/public\/images\/avatars\/25\/25f4a505cbce0a9b1deacb7468e18898bcff484f_medium.jpg", 
 
     "avatarLarge": "https:\/\/steamcdn-a.akamaihd.net\/steamcommunity\/public\/images\/avatars\/25\/25f4a505cbce0a9b1deacb7468e18898bcff484f_full.jpg" 
 
    }, 
 
    "guid": "33b56e488b3d41d67ff3bc1f57b3d101", 
 
    "cash": 0, 
 
    "bank": 11048411, 
 
    "adminlevel": 0, 
 
    "wanted": false, 
 
    "groupid": 204, 
 
    "grouprank": 3, 
 
    "groupname": "The Copper Kings", 
 
    "unmcrank": 0, 
 
    "unmcair": 0, 
 
    "unmcranger": 0, 
 
    "unmclastlogin": null, 
 
    "policerank": 3, 
 
    "npasrank": 0, 
 
    "mporank": 2, 
 
    "arrank": 0, 
 
    "ncarank": 0, 
 
    "rturank": 1, 
 
    "undercoverrank": "0", 
 
    "policelastlogin": "2017-12-03 19:19:15", 
 
    "nhsrank": 4, 
 
    "nhsarrank": 0, 
 
    "nhsrirrank": 1, 
 
    "nhslastlogin": "2017-11-05 21:27:15", 
 
    "civ_licenses": [{ 
 
     "license": "driver", 
 
     "owned": true 
 
    }, { 
 
     "license": "air", 
 
     "owned": true 
 
    }, { 
 
     "license": "heroin", 
 
     "owned": true 
 
    }, { 
 
     "license": "marijuana", 
 
     "owned": true 
 
    }, { 
 
     "license": "boat", 
 
     "owned": true 
 
    }, { 
 
     "license": "oil", 
 
     "owned": true 
 
    }, { 
 
     "license": "dive", 
 
     "owned": true 
 
    }, { 
 
     "license": "truck", 
 
     "owned": true 
 
    }, { 
 
     "license": "gun", 
 
     "owned": true 
 
    }, { 
 
     "license": "rebel", 
 
     "owned": true 
 
    }, { 
 
     "license": "coke", 
 
     "owned": true 
 
    }, { 
 
     "license": "diamond", 
 
     "owned": true 
 
    }, { 
 
     "license": "copper", 
 
     "owned": true 
 
    }, { 
 
     "license": "plastic", 
 
     "owned": true 
 
    }, { 
 
     "license": "aluminium", 
 
     "owned": true 
 
    }, { 
 
     "license": "silica", 
 
     "owned": true 
 
    }, { 
 
     "license": "iron", 
 
     "owned": true 
 
    }, { 
 
     "license": "sand", 
 
     "owned": true 
 
    }, { 
 
     "license": "salt", 
 
     "owned": true 
 
    }, { 
 
     "license": "cement", 
 
     "owned": true 
 
    }, { 
 
     "license": "home", 
 
     "owned": true 
 
    }, { 
 
     "license": "event", 
 
     "owned": false 
 
    }, { 
 
     "license": "meth", 
 
     "owned": true 
 
    }, { 
 
     "license": "taxi", 
 
     "owned": true 
 
    }, { 
 
     "license": "arac", 
 
     "owned": true 
 
    }, { 
 
     "license": "rebelimports", 
 
     "owned": true 
 
    }], 
 
    "vehicles": [{ 
 
     "id": 360502, 
 
     "classname": "B_Quadbike_01_F", 
 
     "type": "Car", 
 
     "color": 7 
 
    }, { 
 
     "id": 360505, 
 
     "classname": "C_Offroad_01_F", 
 
     "type": "Car", 
 
     "color": 4 
 
    }, { 
 
     "id": 364532, 
 
     "classname": "C_Van_01_box_F", 
 
     "type": "Car", 
 
     "color": 1 
 
    }, { 
 
     "id": 367013, 
 
     "classname": "C_Kart_01_Vrana_F", 
 
     "type": "Car", 
 
     "color": 0 
 
    }, { 
 
     "id": 373584, 
 
     "classname": "O_MRAP_02_F", 
 
     "type": "Car", 
 
     "color": 0 
 
    }, { 
 
     "id": 386845, 
 
     "classname": "C_Hatchback_01_sport_F", 
 
     "type": "Car", 
 
     "color": 0 
 
    }, { 
 
     "id": 392893, 
 
     "classname": "C_Scooter_Transport_01_F", 
 
     "type": "Ship", 
 
     "color": 3 
 
    }, { 
 
     "id": 393490, 
 
     "classname": "B_Truck_01_box_F", 
 
     "type": "Car", 
 
     "color": 0 
 
    }, { 
 
     "id": 398146, 
 
     "classname": "O_Plane_CAS_02_F", 
 
     "type": "Air", 
 
     "color": 0 
 
    }, { 
 
     "id": 402207, 
 
     "classname": "B_Truck_01_box_F", 
 
     "type": "Car", 
 
     "color": 0 
 
    }, { 
 
     "id": 402335, 
 
     "classname": "O_LSV_02_unarmed_F", 
 
     "type": "Car", 
 
     "color": 1 
 
    }, { 
 
     "id": 407171, 
 
     "classname": "C_Scooter_Transport_01_F", 
 
     "type": "Ship", 
 
     "color": 4 
 
    }, { 
 
     "id": 414610, 
 
     "classname": "C_Plane_Civil_01_racing_F", 
 
     "type": "Air", 
 
     "color": 0 
 
    }, { 
 
     "id": 414696, 
 
     "classname": "C_Hatchback_01_sport_F", 
 
     "type": "Car", 
 
     "color": 3 
 
    }, { 
 
     "id": 415938, 
 
     "classname": "C_Plane_Civil_01_racing_F", 
 
     "type": "Air", 
 
     "color": 0 
 
    }, { 
 
     "id": 415939, 
 
     "classname": "I_Heli_light_03_unarmed_F", 
 
     "type": "Air", 
 
     "color": 2 
 
    }, { 
 
     "id": 419203, 
 
     "classname": "I_C_Offroad_02_unarmed_F", 
 
     "type": "Car", 
 
     "color": 5 
 
    }, { 
 
     "id": 420228, 
 
     "classname": "B_T_VTOL_01_infantry_F", 
 
     "type": "Air", 
 
     "color": 0 
 
    }, { 
 
     "id": 420230, 
 
     "classname": "B_Heli_Transport_03_unarmed_F", 
 
     "type": "Air", 
 
     "color": 0 
 
    }, { 
 
     "id": 420248, 
 
     "classname": "I_Truck_02_covered_F", 
 
     "type": "Car", 
 
     "color": 0 
 
    }, { 
 
     "id": 427634, 
 
     "classname": "O_MRAP_02_F", 
 
     "type": "Car", 
 
     "color": 0 
 
    }, { 
 
     "id": 427635, 
 
     "classname": "O_Heli_Light_02_unarmed_F", 
 
     "type": "Air", 
 
     "color": 3 
 
    }, { 
 
     "id": 427640, 
 
     "classname": "C_Hatchback_01_sport_F", 
 
     "type": "Car", 
 
     "color": 0 
 
    }, { 
 
     "id": 427641, 
 
     "classname": "O_Heli_Transport_04_bench_F", 
 
     "type": "Air", 
 
     "color": 0 
 
    }, { 
 
     "id": 429865, 
 
     "classname": "I_Truck_02_fuel_F", 
 
     "type": "Car", 
 
     "color": 0 
 
    }, { 
 
     "id": 438410, 
 
     "classname": "C_Plane_Civil_01_racing_F", 
 
     "type": "Air", 
 
     "color": 0 
 
    }, { 
 
     "id": 445053, 
 
     "classname": "B_Heli_Light_01_F", 
 
     "type": "Air", 
 
     "color": 14 
 
    }, { 
 
     "id": 445054, 
 
     "classname": "B_Heli_Light_01_F", 
 
     "type": "Air", 
 
     "color": 3 
 
    }, { 
 
     "id": 445857, 
 
     "classname": "B_T_VTOL_01_infantry_F", 
 
     "type": "Air", 
 
     "color": 0 
 
    }, { 
 
     "id": 447813, 
 
     "classname": "C_Van_02_vehicle_F", 
 
     "type": "Car", 
 
     "color": 7 
 
    }, { 
 
     "id": 447814, 
 
     "classname": "C_Van_02_transport_F", 
 
     "type": "Car", 
 
     "color": 7 
 
    }, { 
 
     "id": 448115, 
 
     "classname": "I_G_Van_02_transport_F", 
 
     "type": "Car", 
 
     "color": 0 
 
    }, { 
 
     "id": 448119, 
 
     "classname": "I_G_Van_02_vehicle_F", 
 
     "type": "Car", 
 
     "color": 0 
 
    }, { 
 
     "id": 453900, 
 
     "classname": "C_Van_01_fuel_F", 
 
     "type": "Car", 
 
     "color": 0 
 
    }, { 
 
     "id": 458437, 
 
     "classname": "O_Plane_Fighter_02_Stealth_F", 
 
     "type": "Air", 
 
     "color": 0 
 
    }] 
 
}; 
 
var content = data.vehicles.map(v => "<span class='" + v.type + "'>" + v.classname + "</span>").join(", "); 
 

 
document.getElementById("vehicle").innerHTML = content;
<div id="vehicle"></div>

+0

Ich glaube nicht, dass dies funktioniert, da ich die Informationen von einem API (https://api.roleplay.co.uk/v1/player/76561198062083666) wo bekomme classtype/classname ändert sich ständig, zusammen mit der Anzahl von ihnen. Gibt es eine Möglichkeit, diese Persisent mit allen Klassennamen/classtypes zu halten? –

+0

Siehe meine Bearbeitung. Ich denke, es funktioniert ganz gut ... – silentw

+0

Vielen Dank :) –

-1

Bro ich denke, vehicleType Array ist, können Sie Array mit String das ist hinzuzufügen, versuchen Sie, diesen Fehler, überprüfen vehicleType.

0
var carsText = data.vehicles.reduce((acc,vehicle)=>{ 
    return acc.concat(`<span class=${vehicle.classtype}>${vehicle.classname}</span>,`) 
},''); 

document.getElementById('vehicle').innerHTML = carsText.slice(0,-1); //slicing to omit the last extra comma 

Dies sollte erreichen, was Sie versuchen zu tun.

+0

Hoppla hätte das erwähnen sollen, weil die Ergebnisse, die tatsächlich aus der API kommen, normalerweise ziemlich unordentlich sind (dh statt Quadbike ist es eigentlich B_Quadbike_01_F) musste ich eine massive Liste erstellen, wo ich Werte ersetze (Inhalt = Inhalt .replace (/ B_Quadbike_01_F/ig, "Quadbike") ersetzen (/ C_Offroad_01_F/ig, "Offroad" usw.) - wie würde ich das erreichen, was Sie getan haben, während ich immer noch Variablen beibehalte, damit ich die Ergebnisse dieser ersetzen kann Variablen? Wenn das Sinn macht ... –

Verwandte Themen