So I am working on a Grid-based Tactical Battle System for RPG Maker MV (uses Javascript), and I am stuck on developing the turn order. My knowledge of what certain arrays are called is slim so I apologize in advance.
WHAT I AM DOING (simplified without the other classes):
var turnOrder = [];
var roll = (actor.agi - 10) + Math.randomInt(100);
var data = {
id: actor._actorId,
type: "player",
init: roll
};
turnOrder.push(data);
And that loops throughout the party members, and then adds the enemy (by eventId) at the end.
WHAT I NEED HELP WITH: How would I sort the following example? (2 players + 1 enemy)
[{"id":1,"type":"player","init":27},
{"id":2,"type":"player","init":4},
{"id":1,"type":"enemy","init":17}]
How would I sort the above by "init" only? (in descending order) I appreciate anyone and everyone in advance for this (I couldn't seem to find it via other searches).
You could use sort
to sort based on init
in descending order
pool=[{"id":1,"type":"player","init":27}, {"id":2,"type":"player","init":4}, {"id":1,"type":"enemy","init":17}] pool.sort((a,b)=>b.init-a.init) console.log(pool)
Maybe this
let arr = [{"id":1,"type":"player","init":27}, {"id":2,"type":"player","init":4}, {"id":1,"type":"enemy","init":17}]; function compare(a, b) { return a.init < b.init? -1: 1; } let sorted = arr.sort(compare); console.log(sorted);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.