javascript中json数组的取值问题

2024-11-15 10:27:27
推荐回答(2个)
回答(1):

//构建一个json对象
var pinpai = {
"0":{"美的":49,"三星":35,"海信":25,"格力":16,"方太":14},
"1":{"美的":49,"三星":35,"海信":25,"格力":16,"方太":14}
};
jiage = pinpai[0]['美的'];
源:
//构建一个json对象
var jb={"美的":49,"三星":35,"海信":25,"格力":16,"方太":14};
for(var key in jb){
alert(key); //json对象的key
alert(jb[key]); //json对象的值
}

回答(2):

var id = 2;
var name = "";
var index = test.findIndex((v,i)=>v.id == id);
if(index >= 0)
    name = test[index].name;