Strings starts from end to start

61 Views Asked by At

I have a function that joins all the key values with | from a json string but when I join everything it joins the itemsObj from end to start.

{
    "100_npc": {
      "uniqueId": 9000234,
      "username": "[NPC]Pelican",
      "bitmask": 1,
      "itemsObj": {
        "colour_id": 0,
        "head": 0,
        "face": 0,
        "neck": 0,
        "body": 0,
        "hand": 0,
        "feet": 0,
        "flag_id": 0,
        "photo_id": 0,
        "x_corr": 100,
        "y_corr": 100,
        "frame": 0,
        "is_member": 1,
        "total_membership_days": 0
      },
      "isEnabled": true
    }
}
function onloadCompleteNPC(event) {
    var _loc4 = event.target.data;
    for (var key in _loc4) {
        if (_loc4.hasOwnProperty(key)) {
            var item = _loc4[key];
            var roomId = key.split('_')[0];
            if(roomId === "130") {
                var joinArr = [item.uniqueId, item.username];
                for (var objKey in item.itemsObj) {
                    joinArr.push(item.itemsObj[objKey]);
                }
                var npcStr = joinArr.join('|');
                Test(npcStr, roomId);
            }
        }
    }
}

function Test(str, roomId) {
    trace("NPC String: " + str + " Room ID: " + roomId);
}

Results

 9000251|[NPC]Pelican|0|1|0|100|100|0|0|0|0|0|0|0|0|0 

Expected Results should be as followed but doesn't.

 9000251|[NPC]Pelican|0|0|0|0|0|0|0|0|0|100|100|0|1|0

The code seems to work in Javascript but it just doesn't in actionscript. I've tried reversing the strings with reverse() but that doesn't work.

1

There are 1 best solutions below

2
user2182349 On

Does this work for you?

const obj = {
    "100_npc": {
      "uniqueId": 9000234,
      "username": "[NPC]Pelican",
      "bitmask": 1,
      "itemsObj": {
        "colour_id": 0,
        "head": 0,
        "face": 0,
        "neck": 0,
        "body": 0,
        "hand": 0,
        "feet": 0,
        "flag_id": 0,
        "photo_id": 0,
        "x_corr": 100,
        "y_corr": 100,
        "frame": 0,
        "is_member": 1,
        "total_membership_days": 0
      },
      "isEnabled": true
    }
}

function flatten(a,o) {
    if (typeof o !== 'object') {
        a.push(o);
    } else {
        const v = Object.values(o);
        for (const vv of v) {
            flatten(a,vv);
        }
    }
}

const a = [];

flatten(a,obj['100_npc']);

const valueString = a.join('|');

console.log(valueString);