如何从JSON对象获取值

时间:2016-01-12 14:17:57

标签: javascript jquery json

如何使用javascript从JSON对象获取值。我能够获得密钥(see fiddle),但我无法获得与密钥对应的值

我的代码在

下面
var getKeys = function (arr) {
        var key, keys = [];
        for (i = 0; i < arr.length; i++) {
            for (key in arr[i]) {
                if (arr[i].hasOwnProperty(key)) {
                    keys.push(key);
                }
            }
        }
        alert(keys);
    };

var a = [ { name: "Carrot", category: "Vegetable", healthiness: "Adequate" }, 
  { name: "Pork Shoulder", category: "Meat", healthiness: "Questionable" }, 
  { name: "Bubblegum", category: "Candy", healthiness: "Super High"} ]
getKeys(a);

我想要console.log是{key:value}

6 个答案:

答案 0 :(得分:2)

简单。

https://jsfiddle.net/3woafcz7/1/

for (i=0; i<arr.length; i++) {
  for (key in arr[i]) {
    keys.push(key);
    values.push(arr[i][key]); //that's the new
  }
}

答案 1 :(得分:0)

key仅显示属性名称,请使用以下arr[i][key]获取值:

keys.push(arr[i][key]);

答案 2 :(得分:0)

你到底想要什么? 你想提醒关键加上它的价值吗?

var getKeys = function (arr) {
    var key, keys = [];
    for (i = 0; i < arr.length; i++) {
        for (key in arr[i]) {
            if (arr[i].hasOwnProperty(key)) {
                keys.push(key +": "+ arr[i][key]);
            }
        }
    }
    alert(keys);
};

应该这样做。

答案 3 :(得分:0)

只需使用地图功能

var a = [ 
	{ name: "Carrot", category: "Vegetable", healthiness: "Adequate" }, 
	{ name: "Pork Shoulder", category: "Meat", healthiness: "Questionable" }, 
	{ name: "Bubblegum", category: "Candy", healthiness: "Super High"} 
]

var names = a.map(function(obj){
	return obj.name;
})
var categorys = a.map(function(obj){
	return obj.category;
})

document.write('name===>'+names)
document.write('<br>categorys==>'+categorys)

答案 4 :(得分:0)

如果您正在寻找一个包含多个对象的数组,这些对象是根据a中对象的键/值对创建的,那么这应该可以解决问题:

function getPairs(arr) {
  return arr.reduce(function (p, c) {
    for (var prop in c) {
      var obj = {};
      obj[prop] = c[prop];
      p.push(obj);
    }
    return p;
  }, []);
}

getPairs(a);

输出

[{"name":"Carrot"},{"category":"Vegetable"},{"healthiness":"Adequate"},{"name":"Pork Shoulder"},{"category":"Meat"},{"healthiness":"Questionable"},{"name":"Bubblegum"},{"category":"Candy"},{"healthiness":"Super High"}];

DEMO

答案 5 :(得分:0)

获取可以使用的对象的键:     Object.keys(一);

var a = [ { name: "Carrot", category: "Vegetable", healthiness: "Adequate" }, 
  { name: "Pork Shoulder", category: "Meat", healthiness: "Questionable" }, 
  { name: "Bubblegum", category: "Candy", healthiness: "Super High"} ]

var b = { name: "SSSS", category: "VVVVV", healthiness: "AAAAA" };
// using generator (ES6 only)
Object.prototype.iter_values = function*(){
    var keys = Object.keys(this);
    for (var k=0;k < keys.length; k++) {
        yield this[keys[k]];
    };
};
function test_iter(o){
    for (v of o.iter_values()){ // note the 'of' keyword
        console.log(v);
    }
};
// using array (ES5)
Object.prototype.values = function() {
    var keys = Object.keys(this);
    var values = [];
    for (var k=0;k < keys.length; k++) {
        values.push(this[keys[k]]);
    };
    return values;
};  

function test_array(o){
    console.log(o.values());
};

如果你想循环值,使用生成器,否则数组将更简洁。