从 JSON 获取 KEY
Get KEY from JSON
正在关注 JSON:https://api.myjson.com/bins/3yz34
我在想我可以使用一个内部 ID 作为一个键(这简化了循环多个实体和设置数据收集)并使用一个值来显示。但是,我无法获取密钥的值。
$.each(tabledata.Companies, function (){
company = this;
alert(company);
var test = Object.keys( company );
console.log(test);
});
Object.keys() 只是给我值中的字符数。
var test = Object.keys( company );
Returns:
["0", "1", "2", "3", "4", "5", "6", "7"]
["0", "1", "2", "3", "4", "5", "6", "7", "8"]
["0", "1", "2", "3", "4", "5", "6"]
["0", "1", "2", "3", "4"]
["0", "1", "2", "3", "4", "5", "6"]
键值对中的值的字符数似乎是多少?非常坚持这一点,有什么想法吗?或者可能以不同的方式设置 JSON?
提前致谢!
更改循环函数的签名以传入参数
$.each(tabledata.Companies, function(key, value) {...}
查看下面的演示
已更新演示 根据评论
var tabledata = {
"Companies": {
"8df135f4-db42-486c-8d8c-fbbdd561c25e": "Phillips",
"47d946e3-56db-4bc7-8472-3809eb48506d": "Bauknecht",
"3ae13b97-7a09-4342-8953-c1d5a55687db": "Capitol",
"fb017214-dbc1-4b71-8080-4f9b530f4b49": "Bosch",
"50ede412-4eb0-429b-8d73-a1bfef41ebbc": "LG Corp"
},
"Pets": {
"dog": "Snoopy",
"cat": "Silvester",
"bird": "Sweetie",
"snake": "Snaker",
"fish": "Goldie Locks"
}
}
var attributeName="Companies";
$(function() {
$.each(tabledata[attributeName], function(key, value) {
console.log("KEY:" + key + " -> Value:" + value);
});
attributeName="Pets";
$.each(tabledata[attributeName], function(key, value) {
console.log("KEY:" + key + " -> Value:" + value);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
正在关注 JSON:https://api.myjson.com/bins/3yz34
我在想我可以使用一个内部 ID 作为一个键(这简化了循环多个实体和设置数据收集)并使用一个值来显示。但是,我无法获取密钥的值。
$.each(tabledata.Companies, function (){
company = this;
alert(company);
var test = Object.keys( company );
console.log(test);
});
Object.keys() 只是给我值中的字符数。
var test = Object.keys( company );
Returns:
["0", "1", "2", "3", "4", "5", "6", "7"]
["0", "1", "2", "3", "4", "5", "6", "7", "8"]
["0", "1", "2", "3", "4", "5", "6"]
["0", "1", "2", "3", "4"]
["0", "1", "2", "3", "4", "5", "6"]
键值对中的值的字符数似乎是多少?非常坚持这一点,有什么想法吗?或者可能以不同的方式设置 JSON?
提前致谢!
更改循环函数的签名以传入参数
$.each(tabledata.Companies, function(key, value) {...}
查看下面的演示
已更新演示 根据评论
var tabledata = {
"Companies": {
"8df135f4-db42-486c-8d8c-fbbdd561c25e": "Phillips",
"47d946e3-56db-4bc7-8472-3809eb48506d": "Bauknecht",
"3ae13b97-7a09-4342-8953-c1d5a55687db": "Capitol",
"fb017214-dbc1-4b71-8080-4f9b530f4b49": "Bosch",
"50ede412-4eb0-429b-8d73-a1bfef41ebbc": "LG Corp"
},
"Pets": {
"dog": "Snoopy",
"cat": "Silvester",
"bird": "Sweetie",
"snake": "Snaker",
"fish": "Goldie Locks"
}
}
var attributeName="Companies";
$(function() {
$.each(tabledata[attributeName], function(key, value) {
console.log("KEY:" + key + " -> Value:" + value);
});
attributeName="Pets";
$.each(tabledata[attributeName], function(key, value) {
console.log("KEY:" + key + " -> Value:" + value);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>