I am trying to access the key and values of my nested array like this:
var obj = $.getJSON("mydata.json", function() {
console.log( "load success" );
});
Object.keys(obj[2].type).forEach(function(key) {
console.log(key, obj[key]);
});
But I get an error.
Here's the structure of the JSON file:
{
"nodes": [
{
"nd": "nd1",
"cat": "cat1"
}
],
"links": [
{
"id": 100
}
],
"types": [
{
"type": "one",
"image": "image001"
},
{
"type": "two",
"image": "image002"
},
{
"type": "three",
"image": "image003"
}
]
}
My goal is to get a list of values of:
one two three
I am trying to access the key and values of my nested array like this:
var obj = $.getJSON("mydata.json", function() {
console.log( "load success" );
});
Object.keys(obj[2].type).forEach(function(key) {
console.log(key, obj[key]);
});
But I get an error.
Here's the structure of the JSON file:
{
"nodes": [
{
"nd": "nd1",
"cat": "cat1"
}
],
"links": [
{
"id": 100
}
],
"types": [
{
"type": "one",
"image": "image001"
},
{
"type": "two",
"image": "image002"
},
{
"type": "three",
"image": "image003"
}
]
}
My goal is to get a list of values of:
Share Improve this question edited Nov 10, 2016 at 15:13 Sam Hanley 4,7557 gold badges37 silver badges64 bronze badges asked Nov 10, 2016 at 15:02 passionpassion 1,0207 gold badges21 silver badges51 bronze badges 3one two three
- What error do you get? – Vitalii Strimbanu Commented Nov 10, 2016 at 15:04
-
var vals = obj.types.map((x) => x.image; });
– Stan Commented Nov 10, 2016 at 15:07 - There is no key 2 for the object – Dimitri Commented Nov 10, 2016 at 15:09
6 Answers
Reset to default 3You can do it with this code:
var obj = {
"nodes": [
{
"nd": "nd1",
"cat": "cat1"
}
],
"links": [
{
"id": 100
}
],
"types": [
{
"type": "one",
"image": "image001"
},
{
"type": "two",
"image": "image002"
},
{
"type": "three",
"image": "image003"
}
]
};
Object.keys(obj["types"]).forEach(function(key) {
console.log(obj["types"][key].image);
});
Something like that?
You have to firstly acces to object.types attribute, then iterate on it to retrieve every type.
var object = {
"nodes": [{
"nd": "nd1",
"cat": "cat1"
}],
"links": [{
"id": 100
}],
"types": [{
"type": "one",
"image": "image001"
}, {
"type": "two",
"image": "image002"
}, {
"type": "three",
"image": "image003"
}]
};
function parseJson(object){
object.types.forEach(function(key) {
console.log(key.type);
});
}
parseJson(object);
--- update to anwer question in ment ---
you could enclose the code in a function, and call it when you load your json:
$.getJSON("mydata.json", function(data) {
parseJson(data);
});
Object.keys(obj[2].type)
doesn't really make sense here. You're not iterating over the properties of any object, you're accessing the same property in a set of objects. This is what map
does.
var types = obj.types.map(x => x.type);
$.getJSON returns a promise because it is asynchronous, but you can set your variable in your callback:
var obj;
$.getJSON("mydata.json", function(data) {
obj = data;
Object.keys(obj[2].type).forEach(function(key) {
console.log(key, obj[key]);
});
});
link to documentation
You should post the error that you get, but I can tell you that the problem is that your output code executes before the JSON object has been loaded. Try this:
$.getJSON("mydata.json", function(obj) {
console.log( "load success" );
Object.keys(obj[2].type).forEach(function(key) {
console.log(key, obj[key]);
});
});
Here is a functional way to do it in js :
var types = $.getJSON("mydata.json").done(function(response) {
return Object.keys(data["types"]).map(function(element){
return element.type;
}).join(' ');
});
console.log(types);