简体   繁体   中英

Return object from function to which JSON is passed

Dears,

I have a function, to which i input JSON data

function getTotals(users){

users = data;


allUsers = users.length;
active = 0;
women = 0;
men = 0;
lastActive = 0;

for (elm in users){
    if (users[elm].active){
        active++;

        if (users[elm].gender == "Female"){
            women++;
        } else if (users[elm].gender == "Male") {
            men++;
        } else if (users[elm].last_login){
            var lastLogin = new Date (users[elm].last_login);
            var lastMonths = lastLogin.getMonth()-6;
            var lastYears = lastLogin.getFullYear();
            //console.log(lastYears);

            if (lastMonths <6 && lastMonths > -6 && lastYears >= lastLogin.getFullYear()-1){
                lastActive++;
            }
        }

    }
}
return {allUsers : allUsers, active : active, women : women, men : men, lastActive : lastActive};}

but i cannot display the values, when i do following:

var listOfUsers = document.createElement('p');
listOfUsers.textContent = "Liczba wszystkich użytkowników: "+allUsers;
document.querySelector("#row1 > div").appendChild(listOfUsers);

var listOfActive = document.createElement('p');
listOfActive.textContent =  "Liczba aktywnych użytkowników: "+active;
document.querySelector("#row2 > div").appendChild(listOfActive);


var listOfWomen = document.createElement('p');
listOfWomen.textContent = "Liczba aktywnych kobiet: "+women;
document.querySelector("#row3 > div").appendChild(listOfWomen);

var listOfMen = document.createElement('p');
listOfMen.textContent = "Liczba aktywnych mężczyzn: "+men;
document.querySelector("#row4 > div").appendChild(listOfMen);

var listOfLastActv = document.createElement('p');
listOfLastActv.textContent = "Liczba aktywnych (ost 6 mcy): "+lastActive;
document.querySelector("#row5 > div").appendChild(listOfLastActv);

When i do following (found here in other questions):

var getTotalUsers = new getTotal();
var allUsers = getTotalUsers.allUsers;
var active = getTotalUsers.active;
var women = getTotalUsers.women;
var men = getTotalUsers.men;
var lastActive = getTotalUsers.lastActive;

I get undefined as a result. I do not know how to fix this issue, as in next part of js, i will need to display list of active users.

new Promise() returns results asynchronously. You can either chain .then() or use async/await to get expected result

 function httpGet() { return new Promise(function(resolve) { setTimeout(function() { resolve([{a:1}, {b:2}, {c:3}]) }, Math.floor(Math.random() * 1200)) }) } async function getTotals() { const users = await httpGet(); console.log(users); } getTotals(); 

 function httpGet() { return new Promise(function(resolve) { setTimeout(function() { resolve([{a:1}, {b:2}, {c:3}]) }, Math.floor(Math.random() * 1200)) }) } function getTotals() { const data = httpGet(); data.then(function(users) { console.log(users) }) .catch(function(err) { console.log(err) }) } getTotals(); 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM