Apakah mungkin untuk mendapatkan kunci newAchievements saat ini dalam for loop for (var key dalam achievementObject) setelah baris newAchievements.push?

Misalnya, jika kunci achievementObject adalah "Selesai level 1", apakah mungkin mengembalikan kunci newAchievements saat ini (yang akan menjadi 0 dalam kasus ini) setelah baris newAchievements.push di loop for yang sama?

Saat ini, saya mendapatkan JavascriptException ini:

"StackTrace": "TypeError: Cannot read property 'achievement' of undefined\n    at handlers.UpdateAchievementsReadOnlyData (...-main.js:24:34)\n    at Object.invokeFunction (Script:116:33)"

Saya perlu memperbarui nilai pencapaian "Level1FinishTimes" dengan server.UpdateUserReadOnlyData(MyRequest); sebelum saya memanggil ChooseanAchievement() untuk pasangan kunci/nilai berikutnya.

Apakah mungkin untuk mendapatkan kunci pencapaian baru saat ini di loop for? Jika tidak, apa cara terbaik untuk memperbarui nilai pencapaian (dengan server.UpdateUserReadOnlyData(MyRequest);) sebelum saya memanggil ChooseanAchievement() untuk pasangan kunci/nilai berikutnya?

handlers.UpdateAchievementsReadOnlyData = function (args, context)
{
    var achievementObject = args.AchievementObject;
    var achievementKeys = [];
    var newAchievements = [];

    for (var key in achievementObject)
    {
        achievementKeys.push(key);
    };

    log.info(achievementKeys);

    var getUserReadOnlyDataResult = server.GetUserReadOnlyData({ PlayFabId: currentPlayerId, Keys: achievementKeys });

    for (var key in achievementObject)
    {
        log.info("achievementObject[key]--->" + achievementObject[key]);
        newAchievements.push(ChooseanAchievement(getUserReadOnlyDataResult, key, achievementObject[key]));

    if (newAchievements[key].achievement != "")
    {
        log.info("newAchievements.achievement " + newAchievements[key].achievement, "newAchievements.addvalue " + newAchievements[key].addvalue);
        var MyRequest = {
            PlayFabId: currentPlayerId,
            Data: {},
            Permission: UserDataPermission.Public
        };

        MyRequest.Data[newAchievements[key].achievement] = newAchievements[key].addvalue;
        server.UpdateUserReadOnlyData(MyRequest);

        log.info("Updating achievement " + newAchievements[key].achievement + " was succuessful.");
    }
    else
    {
        log.info("Could not find this achievement.");
    }
};

}

function ChooseanAchievement(result, achievementkey, achievementvalue)
{
    switch (achievementkey)
    {
        case "Level1FinishTimes":
            if (result.Data.hasOwnProperty(achievementkey))
            {
                if ((result.Data[achievementkey].Value != null) && (result.Data[achievementkey].Value != ""))
                {
                    if (Number(achievementvalue) == 1)
                    {
                        log.info("Level1FinishTimes ok" + achievementkey + achievementvalue);
                        return { achievement: achievementkey, addvalue: Number(result.Data[achievementkey].Value) + Number(achievementvalue) };
                    }
                    else
                    {
                        log.info("Level1FinishTimes" + achievementkey + achievementvalue);
                        return { achievement: achievementkey, addvalue: result.Data[achievementkey].Value };
                    }
                }
                else
                    return { achievement: "", addvalue: false };
            }
            else
                return { achievement: "", addvalue: false };
            break;

        case "Finish level 1 three times":
            var resultdatalevel1finish = server.GetUserReadOnlyData({ PlayFabId: currentPlayerId, Keys: ["Level1FinishTimes"] });

            if (resultdatalevel1finish.Data.hasOwnProperty("Level1FinishTimes"))
            {
                if ((resultdatalevel1finish.Data.Level1FinishTimes.Value != null) && (resultdatalevel1finish.Data.Level1FinishTimes.Value != ""))
                {
                    if ((Number(resultdatalevel1finish.Data.Level1FinishTimes.Value) >= 3)  && (achievementvalue == true))
                    {
                        log.info("Finish level 1 three times ok" + achievementkey + achievementvalue);
                        return { achievement: achievementkey, addvalue: achievementvalue };
                    }
                    else
                    {
                        log.info("Finish level 1 three times" + achievementkey + achievementvalue);
                        return { achievement: achievementkey, addvalue: result.Data[achievementkey].Value };
                    }
                }
                else
                    return { achievement: "", addvalue: false };
            }
            else
                return { achievement: "", addvalue: false };
            break;
            //...
    }
}
1
Hobbit7 28 Oktober 2019, 16:07

1 menjawab

Jawaban Terbaik

Sepertinya Anda salah memahami istilah key dan index dari sebuah array. Anda mendorong item ke dalam array. Anda perlu menggunakan index untuk mendapatkan item array, mis. fooData[0]:

const fooData = [
    {'Finish level 0': 0, test1: 0},
    {'Finish level 1': 1, test1: 1},
    {'Finish level 2': 2, test1: 2},
];

fooData.push({'Finish level 3': 0, test1: 3});

// To get newly pushed item of array:
const maxIndex = fooData.length - 1;
console.log(`Newly pushed item of array:`, fooData[maxIndex]);

Namun, Anda dapat memiliki key dalam array. Anda perlu menggunakan key untuk mendapatkan item array, mis. fooData['someKeyOfAnArray']:

const fooData = [
  {'Finish level 0': 0, test1: 0},
  {'Finish level 1': 1, test1: 1},
  {'Finish level 2': 2, test1: 2},
];

// To get an object by key from an array
fooData['someKeyOfAnArray'] = {'message': 'This is a message'};
console.log(`// Object by key from an array: `, fooData['someKeyOfAnArray']);
0
StepUp 28 Oktober 2019, 14:04