在javascript中使用变量值访问JSON对象
我试图使用HTML中某些文本的值来访问我的饼图对象 但是,代码使用的是变量的名称,而不是变量的值 这对我来说似乎应该是显而易见的,但这让我感到困惑 谢谢在javascript中使用变量值访问JSON对象,javascript,json,object,variables,Javascript,Json,Object,Variables,我试图使用HTML中某些文本的值来访问我的饼图对象 但是,代码使用的是变量的名称,而不是变量的值 这对我来说似乎应该是显而易见的,但这让我感到困惑 谢谢 var pie = { welfare: { title: "Welfare", percentage : 24, point: 0, color: '#601C6B' }, health: { title: "Health",
var pie = {
welfare: {
title: "Welfare",
percentage : 24,
point: 0,
color: '#601C6B'
},
health: {
title: "Health",
percentage : 20,
point: 0,
color: '#FFAA97'
},
state_pensions: {
title: "State pensions",
percentage : 13,
point: 0,
color: "#9C9C9C"
}
}
$('.pie_item').click(function(){
var pie_piece = $(this).text();
console.log("this is " + pie_piece);
$(this).closest(".drop_down_button").find('p').text(pie_piece);
console.log(pie.pie_piece);
});
当您对对象
饼图.饼图
使用点符号进行属性访问时,它将在饼图
对象中查找具有实际名称饼图
的属性
要使用pie\u piece
的值,您需要使用括号表示法
pie[pie_piece]
有关在对象
饼图上使用点表示法进行属性访问时的详细信息。饼图
在饼图
对象中查找具有实际名称饼图
的属性
要使用pie\u piece
的值,您需要使用括号表示法
pie[pie_piece]
有关的详细信息您可以使用符号
object['element']
访问对象的元素。因此,在这种情况下,如果pie\u pie是福利,健康,或州养老金,的话,你可以做一些类似的事情
var pie = {
welfare: {
title: "Welfare",
percentage : 24,
point: 0,
color: '#601C6B'
},
health: {
title: "Health",
percentage : 20,
point: 0,
color: '#FFAA97'
},
state_pensions: {
title: "State pensions",
percentage : 13,
point: 0,
color: "#9C9C9C"
}
}
$('.pie_item').click(function(){
var pie_piece = $(this).text();
console.log("this is " + pie_piece);
$(this).closest(".drop_down_button").find('p').text(pie_piece);
console.log(pie[pie_piece]);
});
您可以使用符号object['element']
访问对象的元素。因此,在这种情况下,如果pie\u pie是福利,健康,或州养老金,的话,你可以做一些类似的事情
var pie = {
welfare: {
title: "Welfare",
percentage : 24,
point: 0,
color: '#601C6B'
},
health: {
title: "Health",
percentage : 20,
point: 0,
color: '#FFAA97'
},
state_pensions: {
title: "State pensions",
percentage : 13,
point: 0,
color: "#9C9C9C"
}
}
$('.pie_item').click(function(){
var pie_piece = $(this).text();
console.log("this is " + pie_piece);
$(this).closest(".drop_down_button").find('p').text(pie_piece);
console.log(pie[pie_piece]);
});
对象中的饼图是什么?对象中的饼图是什么?