I have my json object ing from ajax resaponse.It looks like this.
{
"customerID": "87545",
"parentCustomerID": "Parent:87545",
"relationshipID": "87545-- Rel 1234",
"customerName": "87545-- John Snow",
"constitution": "87545-- consti"
}
Now I want to iterate through this either using DOJO 1.10 library or normal for loop javascript.But I am not able to get the loop through. I have tried the approach for
require(["dojo/_base/array"],
function(array){
array.forEach(JSON.stringify(ajaxJsonData), function(entry, i){ });
});
Can anyont please help me ?
NB: this json object is ing dynamically each time and the keys are the same as the id of the input types in my jsp page. So i need to get the key and the value.
I have my json object ing from ajax resaponse.It looks like this.
{
"customerID": "87545",
"parentCustomerID": "Parent:87545",
"relationshipID": "87545-- Rel 1234",
"customerName": "87545-- John Snow",
"constitution": "87545-- consti"
}
Now I want to iterate through this either using DOJO 1.10 library or normal for loop javascript.But I am not able to get the loop through. I have tried the approach for
require(["dojo/_base/array"],
function(array){
array.forEach(JSON.stringify(ajaxJsonData), function(entry, i){ });
});
Can anyont please help me ?
NB: this json object is ing dynamically each time and the keys are the same as the id of the input types in my jsp page. So i need to get the key and the value.
Share Improve this question edited Sep 28, 2017 at 22:23 denov 12.7k2 gold badges30 silver badges45 bronze badges asked Jul 14, 2015 at 6:50 SambuddhaSambuddha 2455 silver badges18 bronze badges5 Answers
Reset to default 3Came across this and wanted to point out that you could end up iterating over something you don't want. Consider adding a check inside your loop:
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(key, obj[key]);
}
}
This keeps you from accidentally working with something up the prototype chain on accident. I prefer using Object.keys
which does the same check and more:
var keys = Object.keys(obj);
keys.forEach(function(key) {
console.log(key, obj[key]);
});
TLDR; Run this and ask yourself if it's what you want:
function Person() {
this.name = 'Joe';
this.age = 21;
}
Person.prototype.sayHello = function() {
console.log('Hi my name is ' + this.name + ', I am ' + this.age);
};
var obj = new Person();
obj.sayHello();
for (var key in obj) {
console.log(key, obj[key]);
}
You can use the following in Javascript:
var obj = { "customerID": "87545", "parentCustomerID": "Parent:87545",
"relationshipID": "87545-- Rel 1234", "customerName": "87545-- John Snow", "constitution": "87545-- consti" };
for (var key in obj) {
console.log(key +":" +obj[key]);
}
Explanation: It will iterate over all the keys present in obj
and print it along with the corresponding value in the obj
using obj[key]
.
You can check the results by copying the above code in your browser's console.
try this using javascript
for(var key in yourObj) {
var value = yourObj[key];
console.log(value);
}
Here is a js fiddle link:
http://jsfiddle/qo0669dx/1/
var Obj = {
"customerID": "87545",
"parentCustomerID": "Parent:87545",
"relationshipID": "87545-- Rel 1234",
"customerName": "87545-- John Snow",
"constitution": "87545-- consti"
}
for (var key in Obj) {
console.log(key + ":" + Obj[key])
}
Following is Object not Array. To iterate through object you can use for in
var obj = { "customerID": "87545", "parentCustomerID": "Parent:87545",
"relationshipID": "87545-- Rel 1234", "customerName": "87545-- John Snow", "constitution": "87545-- consti" }
for(var prop in obj) {
console.log(obj[prop]);
}