我是JavaScript的新手,我需要检索以这种方式存储在全局定义的对象中的信息。
var customMap ={
totalValues:{
total:10000
}
carPrices:{
'CAR1':{date1:price1}
'CAR2':{date2:price2}
}
}
已使用函数将其填充,当我启动console.log(customMap)
时,我可以完美地看到它的整个结构。问题是当我尝试检索特定信息时。
我总是不确定。
我尝试过:
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
当我以这种方式查询它时,我所能达到的目的:
console.log(customMap.totalValues);
//{}
//total: 10000
console.log(Object.values(customMap.totalValues));
console.log(Object.keys(customMap.totalValues));
console.log(Object.entries(customMap.totalValues));
所有人都给出相同的返回消息:
//[]
//length: 0
carPrices
对象也会发生同样的情况。我无法获取每辆车的信息。我的意思是CAR1,CAR2 ...
我的想法已经用完了。我不知道是否是由于访问对象的方式不正确,或者对象定义不正确或者仅仅是因为全局声明。
我很感谢您可能提出的所有想法。
@ Kirill Matrosov我在代码下面添加了一下我的意图。您可能会注意到对象结构比上一个更大,因为我试图在问题上更加精确。无论如何,我发现JS不是顺序的,并且回调根本无法帮助我: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': {(...)}
}
}
*/
答案 0 :(得分:0)
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);
答案 1 :(得分:0)
您的对象已损坏。
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'.
}
}
答案 2 :(得分:0)
问题可能是您忘记了用逗号分隔customMap
对象和carPrices
属性中的值。
这是您尝试过的可行示例
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));