Use JSON2.js
var obj = JSON.parse(data);
for(var key in obj){
if (obj.hasOwnProperty(key)){
var value=obj[key];
// work with key and value
}
}
Answer from Shiplu Mokaddim on Stack OverflowUse JSON2.js
var obj = JSON.parse(data);
for(var key in obj){
if (obj.hasOwnProperty(key)){
var value=obj[key];
// work with key and value
}
}
var s = '{"AboutToExpire":{"Display":true,"Message":"Several of your subscriptions are about to expire. \u003ca id=\"lnkShowExpiringSubs\" href=\"#\"\u003eView subscriptions\u003c/a\u003e"},"Expired":{"Display":true,"Message":"Your McAfee WaveSecure - Tablet Edition subscription has expired and you’ve been without protection for 384 days. \u003ca id=\"lnkNotificationRenewNow\" href=\"http://home.mcafee.com/root/campaign.aspx?cid=96035&pk=FAB37CF4-3680-4A87-A253-77E7D48BF6D7&affid=0\"\u003eRenew now\u003c/a\u003e"}}';
var data = eval(s); // this will convert your json string to a javascript object
for (var key in data) {
if (data.hasOwnProperty(key)) { // this will check if key is owned by data object and not by any of it's ancestors
alert(key+': '+data[key]); // this will show each key with it's value
}
}
Use for-in...something like:
for (var i in dictionary) {
dictionary[i].forEach(function(elem, index) {
console.log(elem, index);
});
}
where the i would iterate through your dictionary object, and then you can use forEach for every json array in the dictionary(using dictionary[i])
With this code you'll get
Object {id: "0", name: "ABC"} 0
Object {id: "1", name: "DEF"} 1
Object {id: "0", name: "PQR"} 0
Object {id: "1", name: "xyz"} 1
You can tailor the forEach function definition(replacing the console.log bit) to do whatever you want with it.
DEMO
Edit: Doing the same thing using Object.keys
Object.keys(dictionary).forEach(function(key) {
dictionary[key].forEach(function(elem, index) {
console.log(elem, index);
});
});
Edit2: Given the somewhat complicated structure of your jsonData object, you could try using a (sort of) all-purpose function that would act on each type of component separately. I've probably missed a few cases, but maybe something like:
function strung(arg) {
var ret = '';
if (arg instanceof Array) {
arg.forEach(function(elem, index) {
ret += strung(elem) + ',';
});
} else if (arg instanceof Object) {
Object.keys(arg).forEach(function(key) {
ret += key + ': /' + strung(arg[key]) + '/';
});
} else if (typeof arg === "string" || typeof arg === "number") {
ret = arg;
}
return ret;
}
document.body.innerHTML = strung(jsonData);
DEMO
Please note that yours is just a JavaScript array object. To make it simple to understand, you can iterate over it like this:
for (var i in dictionary) {
// do something with i
// here i will contain the dates
for (n = 0; n < dictionary[i].length; n++) {
// do something with the inner array of your objects
// dictionary[i][n].id contains the "id" of nth object in the object i
// dictionary[i][n].name contains the "name" of nth object in the object i
}
}
See this fiddle: http://jsfiddle.net/Ke8F5/
The iteration looks like this:
12Jan2013 : (id = 0, name = ABC) (id = 1, name = DEF)
13Jan2013 : (id = 0, name = PQR) (id = 1, name = XYZ)
javascript - how to loop through a nested dictionary or json data in Reactjs - Stack Overflow
json - Loop through data to create a dictionary with key/value pairs using JavaScript - Stack Overflow
Iterate through dictionaries in javascript and access values? - Stack Overflow
javascript - Trying to loop through JSON file - Stack Overflow
Of course that I cant make an entire project solution for you but the function that you wanted must have this kind of logic.
const jsonData = [{
"squadName": "Super hero squad",
"homeTown": "Metro City",
"formed": 2016,
"secretBase": "Super tower",
"active": true,
"members": [
{
"name": "Molecule Man",
"age": 29,
"secretIdentity": "Dan Jukes",
"powers": [
"Radiation resistance",
"Turning tiny",
"Radiation blast"
]
},
{
"authorization": "Black card",
"location": [
"Next",
"Previous",
"Here"
]
}
]
}]
jsonData.forEach(item=>{
item.members.map((member)=>{
if(member.location&&member.location[0]){
//Do whatever, maybe you want to use return statement in there
console.log(member.location)
}
else{
//do something else, or add more conditions
console.log("There's no location in it")
}
})
})
you can put it in a variable and add your variable inside your jsx return statement or use it directly in middle of your function's return statement.
Good luck.
You can map through data in a functional component the same way you would map through a class component. For this example, you could list only nested data:
const List = () => {
return (
<div>
{data.map(item =>
(item.members || []).map(member =>
(member.location || []).map(item => (<div>{item}</div>))
)
)}
</div>
);
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
which would list only the nested "location" data for each member if that property exists. Or you could map through data and display top-level properties and then also map through its nested properties:
const List = () => {
return (
<div>
{data.map(item => (
((item.members || []).map(member => (
<div>
{member.name || ''}
{member.location && member.location.map(loc => (<div>{loc}</div>))}
</div>
)))
))}
</div>
);
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
var test = [{"name":"aName","lastName":"aLastname"},{"name":"bName","lastName":"bLastname"}];
for (var i = 0; i < test.length; ++i) {
alert(test[i].name + ", " + test[i].lastName);
}
http://jsfiddle.net/1odgpfg4/1/
You may want to try this.
var arr = [{"name":"aName","lastName":"aLastname"},{"name":"bName","lastName":"bLastname"}];
arr.forEach(function(d){
console.log(d.name, d.lastName);
});
You have added extra td JSFIDDLE
$.each(result.teams, function (index, value) {
var row = ['<tr><td>', index, '</td>']; // extra td
// Add if place is found or not.
$(this.done).each(function () {
if (this === true) {
row = row.concat(['<td>yes</td>']);
} else {
row = row.concat(['<td>no</td>']);
}
});
$('.scorestable tbody').append(row.join('') + '</tr>');
});
Here you go. The each() method needs an index and value. The value is your boolean.
// Add teams and their result.
$.each(result.teams, function (index, value) {
var row = ['<tr><td>', index, '</td>']; // extra td
// Add if place is found or not.
$(this.done).each(function (index, value) {
if (value === true) {
row = row.concat(['<td>yes</td>']);
} else {
row = row.concat(['<td>no</td>']);
}
});
$('.scorestable tbody').append(row.join('') + '</tr>');
});
Tested and working