简体   繁体   中英

Value of objects inside objects that give undefined result

I'm new with JavaScript and I need to retrieve information stored in this way in a globally defined object.

var customMap ={
     totalValues:{
         total:10000
     }
     carPrices:{
         'CAR1':{date1:price1}
         'CAR2':{date2:price2}
     }
}

It has been fed using a function and when I launch console.log(customMap) I can see the whole structure of it perfectly. The problem is when I try to retrieve specific information.

I always get undefined.

I have tried with:

for (var i in customMap.totalValues){
    console.log(i);
    console.log(customMap.totalValues[i]);

}//It doesn't write anything in the log.

console.log(customMap.totalValues["total"]);//undefined
console.log(customMap.totalValues.total);//undefined

what I have achieve is when I query it in this way:

console.log(customMap.totalValues);

//{}
    //total: 10000

console.log(Object.values(customMap.totalValues));
console.log(Object.keys(customMap.totalValues));
console.log(Object.entries(customMap.totalValues));

All give same returning message:

//[]
    //length: 0

Same happens with carPrices objects. I cannot retrieve information for each car. I mean CAR1, CAR2...

I've run out of ideas. I don't know if is because of the way of accessing to the object is not correct, or the object is not correctly defined or just because is globally declared.

I'd appreciate all ideas that you could have.

@ Kirill Matrosov I add below the code to give an idea of my intention. As you may notice the object structure is bigger than the previous one because I try to be more precise in the issue. Anyway I have discovered that JS is not sequential and callbacks don't help me at all :S

var customMap = 
{
    totalValues:{},
    carPrices:{}
}

function addValuesToCustomMap(date,car,value){

    if (!customMap.carPrices[car]){
        customMap.carPrices[car] = {
            dates: {},
            carTotalValue:0,
            carPercent:0
        };
    }

    if (!customMap.carPrices[car].dates[date]){
        customMap.carPrices[car].dates[date] = value;
    }
    else if (customMap.carPrices[car].dates[date]){
        var auxValue = customMap.carPrices[car].dates[date];
        customMap.carPrices[car].dates[date] = auxValue + value;
    }

    var totalValue_byCar = customMap.carPrices[car].catTotalValue;
    customMap.carPrices[car].catTotalValue = totalValue_byCar + value;

    if(!customMap.totalValues["total"]){
        customMap.totalValues["total"]=value;
    }
    else{
        var tot = customMap.totalValues["total"]; 
        customMap.totalValues["total"]=tot+value;
    } 
}

function calculatePercentagesByCar(){
    var tot = customMap.totalValues["total"];
    for (var k in Object.keys(customMap.carPrices)){
        var totalCarPrice = customMap.carPrices[k].carTotalValue;
        var percent = totalCarPrice*100/tot;

        customMap.carPrices[k].carPercent = percent;
    }
}

/*
customMap={
        totalValue:{
            total: xxxxxx
        }
        carPrices:{
                'CAR 1': {
                            dates:{
                                    {date1:value1},
                                    (...)
                                    {dateN:valueN}
                            }
                            carTotalValue: yyyyyy,
                            carPercent: zzzz
                         }
                (...)
                'CAR N': {(...)}
        }
}
*/

 var customMap ={ totalValues:{ total:10000 }, carPrices:{ 'CAR1':{date1:'price1'}, 'CAR2':{date2:'price2'} } }; console.log(customMap.totalValues.total); console.log(customMap.carPrices.CAR1); console.log(customMap.carPrices.CAR2); 

Your object is broken.

var customMap ={
 totalValues:{
     total:10000
 }, // here you are missing a comma.
 carPrices:{
     'CAR1':{date1:price1} // Is this value filled by the variable price1? If not is broken and should be 'price1'.
     'CAR2':{date2:price2} // Is this value filled by the variable price2? If not is broken and should be 'price2'.
 }
}

The problem might be that you forgot to separate the values in the customMap object and the carPrices property with commas.

Here's a working example of what you've tried

 var customMap = { totalValues:{ total:10000 }, carPrices:{ 'CAR1':{'date1':'price1'}, 'CAR2':{'date2':'price2'} } } for (var i in customMap.totalValues){ console.log('property:', i); console.log('value:', customMap.totalValues[i]); } /* property: total value: 10000 */ console.log(customMap.totalValues["total"]);//10000 console.log(customMap.totalValues.total);//10000 console.log(customMap.totalValues); console.log(Object.values(customMap.totalValues)); console.log(Object.keys(customMap.totalValues)); console.log(Object.entries(customMap.totalValues)); 

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