Related
I have an array like the one below. How can I find every single value in the array is small or big compare to its next value. And I want to push the result in a new array like this same array. And if the items is the same then that's will be empty.
const array = [
[7, 10, 10, 10, 9, 13, 9, 9, 11, 12, 7, 8, 8, 8, 8],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[6, 5, 6, 7, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 6],
[7, 7, 7, 8, 8, 8, 7, 7, 7, 5, 7, 7, 7, 7, 7],
[0, 63, 58, 65, 55, 68, 65, 65, 43, 58, 57, 55, 55, 70, 55],
[29, 26, 28, 27, 29, 29, 30, 33, 32, 33, 28, 28, 31, 31, 29],
[3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
];
This is my code but which is doesn't work.
const items = [];
function getOutRank() {
array.map((singleKey, index) => {
console.log("SINGLE KEY", singleKey);
for (let i = 0; i < singleKey.length; i++) {
if (singleKey[i + 1] > singleKey[i]) {
return items.push(<span key={index}>Down</span>);
}
if (singleKey[i + 1] === singleKey[i]) {
return items.push(<span key={index} />);
}
return items.push(<li key={index}>Up</li>);
}
});}
getOutRank();
For each singleKey, an array of all the compare results should be returned. Try like this:
const array = [
[7, 10, 10, 10, 9, 13, 9, 9, 11, 12, 7, 8, 8, 8, 8],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[6, 5, 6, 7, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 6],
[7, 7, 7, 8, 8, 8, 7, 7, 7, 5, 7, 7, 7, 7, 7],
[0, 63, 58, 65, 55, 68, 65, 65, 43, 58, 57, 55, 55, 70, 55],
[29, 26, 28, 27, 29, 29, 30, 33, 32, 33, 28, 28, 31, 31, 29],
[3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
];
let items = [];
function getOutRank() {
items = array.map((singleKey, index) => {
var compare = [];
for (let i = 0; i < singleKey.length - 1; i++) {
if (singleKey[i + 1] > singleKey[i]) {
compare.push(`<span key=${index}>Down</span>`);
} else if (singleKey[i + 1] === singleKey[i]) {
compare.push(`<span key=${index}>Equal</span>`);
} else {
compare.push(`<li key=${index}>Up</li>`);
}
}
return compare;
});
}
getOutRank();
console.log(items);
i have an array and i want convert this array to separate object , glad to help me, thanks
array = ["January", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, "February", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
i want
result = [
{
month:'January',
days:[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
},
{
month:'February',
days:[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
}
]
First find out the indexes for strings in array, so that you can split array.
const process = (arr) => {
const result = [];
let last = -1;
for (let i = 0; i <= arr.length; i++) {
if (typeof arr[i] === "string" || !(i in arr)) {
if (last !== -1) {
result.push([last, i]);
}
last = i;
}
}
return result.map(([start, end]) => ({
month: arr[start],
days: arr.slice(start + 1, end),
}));
};
const array = ["January", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, "February", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
console.log(process(array));
I have a js array like this.
const test_arr = [ [ 20, 7, 23, 19, 10, 15, 25 ],
[ 20, 7, 23, 19, 10, 15, 8 ],
[ 20, 7, 23, 19, 10, 15, 13 ],
[ 20, 7, 23, 19, 10, 25, 8 ],
[ 20, 7, 23, 19, 10, 25, 13 ],
[ 20, 7, 23, 19, 10, 8, 13 ],
[ 20, 7, 23, 19, 15, 25, 8 ],
[ 20, 7, 23, 19, 15, 25, 13 ],
[ 20, 7, 23, 19, 15, 8, 13 ],
[ 20, 7, 23, 19, 25, 8, 13 ],
[ 20, 7, 23, 10, 15, 25, 8 ],
[ 20, 7, 23, 10, 15, 25, 13 ],
[ 20, 7, 23, 10, 15, 8, 13 ],
[ 20, 7, 23, 10, 25, 8, 13 ],
[ 20, 7, 23, 15, 25, 8, 13 ],
[ 20, 7, 19, 10, 15, 25, 8 ],
[ 20, 7, 19, 10, 15, 25, 13 ],
[ 20, 7, 19, 10, 15, 8, 13 ],
[ 20, 7, 19, 10, 25, 8, 13 ],
[ 20, 7, 19, 15, 25, 8, 13 ],
[ 20, 7, 10, 15, 25, 8, 13 ],
[ 20, 23, 19, 10, 15, 25, 8 ],
[ 20, 23, 19, 10, 15, 25, 13 ],
[ 20, 23, 19, 10, 15, 8, 13 ],
[ 20, 23, 19, 10, 25, 8, 13 ],
[ 20, 23, 19, 15, 25, 8, 13 ],
[ 20, 23, 10, 15, 25, 8, 13 ],
[ 20, 19, 10, 15, 25, 8, 13 ],
[ 7, 23, 19, 10, 15, 25, 8 ],
[ 7, 23, 19, 10, 15, 25, 13 ],
[ 7, 23, 19, 10, 15, 8, 13 ],
[ 7, 23, 19, 10, 25, 8, 13 ],
[ 7, 23, 19, 15, 25, 8, 13 ],
[ 7, 23, 10, 15, 25, 8, 13 ],
[ 7, 19, 10, 15, 25, 8, 13 ],
[ 23, 19, 10, 15, 25, 8, 13 ] ]
var combination_before = [ 20,7,23, 19, 10, 15, 25, 8, 13 ];
These are the results of choosing seven out of nine.
I would like to return some elements only when sum of the elements is 100.
how do I make reducer if condition?
you can use filter for filtering your array and reduce for sum your nested arrays, and check in your filter which array sum is equal to 100
const test_arr = [ [ 20, 7, 23, 19, 10, 15, 25 ],
[ 20, 7, 23, 19, 10, 15, 8 ],
[ 20, 7, 23, 19, 10, 15, 13 ],
[ 20, 7, 23, 19, 10, 25, 8 ],
[ 20, 7, 23, 19, 10, 25, 13 ],
[ 20, 7, 23, 19, 10, 8, 13 ],
[ 20, 7, 23, 19, 15, 25, 8 ],
[ 20, 7, 23, 19, 15, 25, 13 ],
[ 20, 7, 23, 19, 15, 8, 13 ],
[ 20, 7, 23, 19, 25, 8, 13 ],
[ 20, 7, 23, 10, 15, 25, 8 ],
[ 20, 7, 23, 10, 15, 25, 13 ],
[ 20, 7, 23, 10, 15, 8, 13 ],
[ 20, 7, 23, 10, 25, 8, 13 ],
[ 20, 7, 23, 15, 25, 8, 13 ],
[ 20, 7, 19, 10, 15, 25, 8 ],
[ 20, 7, 19, 10, 15, 25, 13 ],
[ 20, 7, 19, 10, 15, 8, 13 ],
[ 20, 7, 19, 10, 25, 8, 13 ],
[ 20, 7, 19, 15, 25, 8, 13 ],
[ 20, 7, 10, 15, 25, 8, 13 ],
[ 20, 23, 19, 10, 15, 25, 8 ],
[ 20, 23, 19, 10, 15, 25, 13 ],
[ 20, 23, 19, 10, 15, 8, 13 ],
[ 20, 23, 19, 10, 25, 8, 13 ],
[ 20, 23, 19, 15, 25, 8, 13 ],
[ 20, 23, 10, 15, 25, 8, 13 ],
[ 20, 19, 10, 15, 25, 8, 13 ],
[ 7, 23, 19, 10, 15, 25, 8 ],
[ 7, 23, 19, 10, 15, 25, 13 ],
[ 7, 23, 19, 10, 15, 8, 13 ],
[ 7, 23, 19, 10, 25, 8, 13 ],
[ 7, 23, 19, 15, 25, 8, 13 ],
[ 7, 23, 10, 15, 25, 8, 13 ],
[ 7, 19, 10, 15, 25, 8, 13 ],
[ 23, 19, 10, 15, 25, 8, 13 ] ];
const result = test_arr.filter(arr => arr.reduce((a, b) => a + b, 0) == 100);
console.log(result);
const test_arr = [ [ 20, 7, 23, 19, 10, 15, 25 ],
[ 20, 7, 23, 19, 10, 15, 8 ],
[ 20, 7, 23, 19, 10, 15, 13 ],
[ 20, 7, 23, 19, 10, 25, 8 ],
[ 20, 7, 23, 19, 10, 25, 13 ],
[ 20, 7, 23, 19, 10, 8, 13 ],
[ 20, 7, 23, 19, 15, 25, 8 ],
[ 20, 7, 23, 19, 15, 25, 13 ],
[ 20, 7, 23, 19, 15, 8, 13 ],
[ 20, 7, 23, 19, 25, 8, 13 ],
[ 20, 7, 23, 10, 15, 25, 8 ],
[ 20, 7, 23, 10, 15, 25, 13 ],
[ 20, 7, 23, 10, 15, 8, 13 ],
[ 20, 7, 23, 10, 25, 8, 13 ],
[ 20, 7, 23, 15, 25, 8, 13 ],
[ 20, 7, 19, 10, 15, 25, 8 ],
[ 20, 7, 19, 10, 15, 25, 13 ],
[ 20, 7, 19, 10, 15, 8, 13 ],
[ 20, 7, 19, 10, 25, 8, 13 ],
[ 20, 7, 19, 15, 25, 8, 13 ],
[ 20, 7, 10, 15, 25, 8, 13 ],
[ 20, 23, 19, 10, 15, 25, 8 ],
[ 20, 23, 19, 10, 15, 25, 13 ],
[ 20, 23, 19, 10, 15, 8, 13 ],
[ 20, 23, 19, 10, 25, 8, 13 ],
[ 20, 23, 19, 15, 25, 8, 13 ],
[ 20, 23, 10, 15, 25, 8, 13 ],
[ 20, 19, 10, 15, 25, 8, 13 ],
[ 7, 23, 19, 10, 15, 25, 8 ],
[ 7, 23, 19, 10, 15, 25, 13 ],
[ 7, 23, 19, 10, 15, 8, 13 ],
[ 7, 23, 19, 10, 25, 8, 13 ],
[ 7, 23, 19, 15, 25, 8, 13 ],
[ 7, 23, 10, 15, 25, 8, 13 ],
[ 7, 19, 10, 15, 25, 8, 13 ],
[ 23, 19, 10, 15, 25, 8, 13 ] ];
var resultArr = [];
for (var i = 0; i < test_arr.length; i++){
temp = test_arr[i].reduce(getSum)
if (temp == 100) {
resultArr.push(test_arr[i]);
}
}
function getSum(total, num) {
return total + num;
}
console.log(resultArr)
You could take a different approach at the moment, where you take all combination and return only the ones who match the conditions by length and sum.
function getCombinations(array, length, sum) {
function iter(array, temp) {
if (temp.length === length) {
if (temp.reduce((a, b) => a + b) === sum) {
result.push(temp);
}
return;
}
if (!array.length || array.length + temp.length < length) {
return;
}
iter(array.slice(1), temp.concat(array[0]));
iter(array.slice(1), temp);
}
var result = [];
iter(array, []);
return result;
}
var array = [20, 7, 23, 19, 10, 15, 25, 8, 13];
console.log(getCombinations(array, 7, 100));
.as-console-wrapper { max-height: 100% !important; top: 0; }
I have the following data (loaded by ajax):
[
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [18, 15, 11, 12, 9, 2, 33, 12, 2, 4],
},
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [16, 15, 11, 12, 9, 2, 33, 12, 2, 4],
},
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [14, 15, 11, 12, 9, 2, 33, 12, 2, 4],
}
]
And I have a 5x5 table.
How can I calculate sum of each multiple element of "value" and put it in the table of right index?
My js code for now:
function risk(data) {
let sum = 0;
for (i = 0; i < data.length; i++) {
let b = data[i].Value;
console.log(b); // (10) [18, 15, 11, 12, 9, 2, 33, 12, 2, 4]
// (10) [16, 15, 11, 12, 9, 2, 33, 12, 2, 4]
// (10) [14, 15, 11, 12, 9, 2, 33, 12, 2, 4]
}
}
The end result must be like this:
You could take an object for collecting the values. For getting the result in back into the table, you could iterate the coordinates and get a value.
var data = [{ x: [1, 1, 2, 2, 2, 3, 3, 3, 4, 5], y: [3, 5, 4, 2, 5, 3, 4, 5, 1, 5], Value: [18, 15, 11, 12, 9, 2, 33, 12, 2, 4] }, { x: [1, 1, 2, 2, 2, 3, 3, 3, 4, 5], y: [3, 5, 4, 2, 5, 3, 4, 5, 1, 5], Value: [16, 15, 11, 12, 9, 2, 33, 12, 2, 4] }, { x: [1, 1, 2, 2, 2, 3, 3, 3, 4, 5], y: [3, 5, 4, 2, 5, 3, 4, 5, 1, 5], Value: [14, 15, 11, 12, 9, 2, 33, 12, 2, 4] }],
sum = data.reduce((r, {x, y, Value}) =>
(x.forEach((x, i) => {
r[x] = r[x] || {};
r[x][y[i]] = (r[x][y[i]] || 0) + Value[i];
}), r), {});
console.log(sum);
You can use reduce to build your "result" matrix, adding "Value" to its appropriate x and y coordinate in the matrix as you iterate through all the elements in your original data.
const data = [
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [18, 15, 11, 12, 9, 2, 33, 12, 2, 4],
},
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [16, 15, 11, 12, 9, 2, 33, 12, 2, 4],
},
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [14, 15, 11, 12, 9, 2, 33, 12, 2, 4],
}
];
const res = new Array(5).fill(0).map(n => new Array(5).fill(0));
data.reduce((acc, curr) => {
curr.Value.forEach((v, i) => acc[curr.x[i] - 1][curr.y[i] - 1] += v);
return res;
}, res);
console.log(res);
A] Problem summary:
I have JSON data being returned from python to javascript. I want to go thru the JSON structure and print the data elements in a html table.
B] Code excerpts:
1] JSON being returned from python --
{'data_for_users_city':
'[
{"city":
{"city_name": "Boston",
"country": {"country_name": "United States"}
},
"status": true,
"date_time": {"ctime": "Thu Apr 7 09:38:00 2011", "hour": 9, "isoweekday": 4, "month": 4, "second": 0, "microsecond": 796000, "isocalendar": [2011, 14, 4], "timetuple": [2011, 4, 7, 9, 38, 0, 3, 97, -1], "year": 2011, "epoch": 1302169080.0, "isoformat": "2011-04-07T09:38:00.796000", "day": 7, "minute": 38}
},
]'
}
Note this is a single city, like this there are many city elements in the JSON data.
2] Javascript code that I tried for parsing thru the data-structure and printing data elements in the “tbody” of a pre-ready HTML table “#datatable_for_current_users”
function LoadUsersDatatable(data) {
var tbody = $("#datatable_for_current_users > tbody").html("");
for (var i=0; i < data.length; i++)
{
var city = data.data_for_users_city[i];
var rowText = "<tr class='gradeA'><td>" + city.county.country_name + "</td><td>" + city.city_name + "</td><td>" + city.status + "</td><td>" + city.date_time.ctime + "</td></tr>";
$(rowText).appendTo(tbody);
}
}
Problems i am having the javascript code are :
1] I am unable to find the exact length of the city elements in the "data" because of which i dont know what the upper bound of the for loop is
2] I am not sure whether i am accessing the "city" variable correctly inside the for loop.
[EDIT#1]
Based on the response given by Salman and Pointy, i had to inspect the python code that was returning the json data. After some debugging it was found that the JSON data was being returned using templates because of which the template name was occuring in the JSON data. I changed the mechanism of sending JSON and now the following is how the returned JSON data looks like
[{"city": {"city_name": "Framingham", "country": {"country_name": "United States", "id": null}, "id": null}, "id": 1, "status": true, "date_time": {"ctime": "Thu Apr 7 09:38:00 2011", "hour": 9, "isoweekday": 4, "month": 4, "second": 0, "microsecond": 796000, "isocalendar": [2011, 14, 4], "timetuple": [2011, 4, 7, 9, 38, 0, 3, 97, -1], "year": 2011, "epoch": 1302169080.0, "isoformat": "2011-04-07T09:38:00.796000", "day": 7, "minute": 38}}, {"city": {"city_name": "Framingham", "country": {"country_name": "United States", "id": null}, "id": null}, "id": 2, "status": false, "date_time": {"ctime": "Thu Apr 7 09:38:03 2011", "hour": 9, "isoweekday": 4, "month": 4, "second": 3, "microsecond": 359000, "isocalendar": [2011, 14, 4], "timetuple": [2011, 4, 7, 9, 38, 3, 3, 97, -1], "year": 2011, "epoch": 1302169083.0, "isoformat": "2011-04-07T09:38:03.359000", "day": 7, "minute": 38}}, {"city": {"city_name": "Framingham", "country": {"country_name": "United States", "id": null}, "id": null}, "id": 3, "status": true, "date_time": {"ctime": "Thu Apr 7 09:38:08 2011", "hour": 9, "isoweekday": 4, "month": 4, "second": 8, "microsecond": 281000, "isocalendar": [2011, 14, 4], "timetuple": [2011, 4, 7, 9, 38, 8, 3, 97, -1], "year": 2011, "epoch": 1302169088.0, "isoformat": "2011-04-07T09:38:08.281000", "day": 7, "minute": 38}}, {"city": {"city_name": "Framingham", "country": {"country_name": "United States", "id": null}, "id": null}, "id": 4, "status": false, "date_time": {"ctime": "Thu Apr 7 09:38:14 2011", "hour": 9, "isoweekday": 4, "month": 4, "second": 14, "microsecond": 578000, "isocalendar": [2011, 14, 4], "timetuple": [2011, 4, 7, 9, 38, 14, 3, 97, -1], "year": 2011, "epoch": 1302169094.0, "isoformat": "2011-04-07T09:38:14.578000", "day": 7, "minute": 38}}, {"city": {"city_name": "Framingham", "country": {"country_name": "United States", "id": null}, "id": null}, "id": 13, "status": true, "date_time": {"ctime": "Wed Apr 13 01:37:58 2011", "hour": 1, "isoweekday": 3, "month": 4, "second": 58, "microsecond": 343000, "isocalendar": [2011, 15, 3], "timetuple": [2011, 4, 13, 1, 37, 58, 2, 103, -1], "year": 2011, "epoch": 1302658678.0, "isoformat": "2011-04-13T01:37:58.343000", "day": 13, "minute": 37}}, {"city": {"city_name": "Framingham", "country": {"country_name": "United States", "id": null}, "id": null}, "id": 14, "status": false, "date_time": {"ctime": "Wed Apr 13 01:38:01 2011", "hour": 1, "isoweekday": 3, "month": 4, "second": 1, "microsecond": 78000, "isocalendar": [2011, 15, 3], "timetuple": [2011, 4, 13, 1, 38, 1, 2, 103, -1], "year": 2011, "epoch": 1302658681.0, "isoformat": "2011-04-13T01:38:01.078000", "day": 13, "minute": 38}}, {"city": {"city_name": "Framingham", "country": {"country_name": "United States", "id": null}, "id": null}, "id": 23, "status": true, "date_time": {"ctime": "Sun Apr 17 21:24:18 2011", "hour": 21, "isoweekday": 7, "month": 4, "second": 18, "microsecond": 625000, "isocalendar": [2011, 15, 7], "timetuple": [2011, 4, 17, 21, 24, 18, 6, 107, -1], "year": 2011, "epoch": 1303075458.0, "isoformat": "2011-04-17T21:24:18.625000", "day": 17, "minute": 24}}]
I am still struggling to get a for loop around this json structure.
[EDIT#2]
After some debugging and response give by #Salman, the following function does the job
function LoadUsersDatatable(data) {
var tbody = $("#datatable_for_current_users > tbody").html("");
jsonData = jQuery.parseJSON(data);
for (var i = 0; i < jsonData.length; i++)
{
var citydata = jsonData[i];
var rowText = "<tr class='gradeA'><td>" + citydata.city.country.country_name + "</td><td>" + citydata.city.city_name + "</td><td>" + citydata.status + "</td><td>" + citydata.date_time.ctime + "</td></tr>";
$(rowText).appendTo(tbody);
}
}
One problem i found while debugging was the JSON returned was in string format and had to be converted to a JSON object, this was done using jQuery.
You seem to be using jQuery. If you want to generate straight HTML from JSON data, one easy solution is to use simple templates through plugin, like jQote2. It provides an easy syntax, that loops through your data. Using JS templates also makes it easier to maintain your HTML structure.
Strange, it seems like data_for_users_city is not and array but a string. I hope this is not a typo or copy/paste error.
Edit 1
Even when you treat it as a string, your JSON still has errors. Newlines inside quotes are not allowed in JavaScript, you must replace them with \n, use the + concatenation operator or use \ to split the string on multiple lines. In case you manage to workaround these problems, you can do a:
var data_for_users_city = eval(data.data_for_users_city);
// sometimes adding extra parenthesis help
// var data_for_users_city = eval('(' + data.data_for_users_city + ')');
alert(data_for_users_city.length);
EDIT 2
This is a quick and dirty demo that I created and tested in FireFox/Firebug console. It basically demonstrates how you can access the three levels of data inside the JSON. To visualize your JSON data properly, copy the following code and paste in jsbeautifier.
var data = [{"city": {"city_name": "Framingham", "country": {"country_name": "United States", "id": null}, "id": null}, "id": 1, "status": true, "date_time": {"ctime": "Thu Apr 7 09:38:00 2011", "hour": 9, "isoweekday": 4, "month": 4, "second": 0, "microsecond": 796000, "isocalendar": [2011, 14, 4], "timetuple": [2011, 4, 7, 9, 38, 0, 3, 97, -1], "year": 2011, "epoch": 1302169080.0, "isoformat": "2011-04-07T09:38:00.796000", "day": 7, "minute": 38}}, {"city": {"city_name": "Framingham", "country": {"country_name": "United States", "id": null}, "id": null}, "id": 2, "status": false, "date_time": {"ctime": "Thu Apr 7 09:38:03 2011", "hour": 9, "isoweekday": 4, "month": 4, "second": 3, "microsecond": 359000, "isocalendar": [2011, 14, 4], "timetuple": [2011, 4, 7, 9, 38, 3, 3, 97, -1], "year": 2011, "epoch": 1302169083.0, "isoformat": "2011-04-07T09:38:03.359000", "day": 7, "minute": 38}}, {"city": {"city_name": "Framingham", "country": {"country_name": "United States", "id": null}, "id": null}, "id": 3, "status": true, "date_time": {"ctime": "Thu Apr 7 09:38:08 2011", "hour": 9, "isoweekday": 4, "month": 4, "second": 8, "microsecond": 281000, "isocalendar": [2011, 14, 4], "timetuple": [2011, 4, 7, 9, 38, 8, 3, 97, -1], "year": 2011, "epoch": 1302169088.0, "isoformat": "2011-04-07T09:38:08.281000", "day": 7, "minute": 38}}, {"city": {"city_name": "Framingham", "country": {"country_name": "United States", "id": null}, "id": null}, "id": 4, "status": false, "date_time": {"ctime": "Thu Apr 7 09:38:14 2011", "hour": 9, "isoweekday": 4, "month": 4, "second": 14, "microsecond": 578000, "isocalendar": [2011, 14, 4], "timetuple": [2011, 4, 7, 9, 38, 14, 3, 97, -1], "year": 2011, "epoch": 1302169094.0, "isoformat": "2011-04-07T09:38:14.578000", "day": 7, "minute": 38}}, {"city": {"city_name": "Framingham", "country": {"country_name": "United States", "id": null}, "id": null}, "id": 13, "status": true, "date_time": {"ctime": "Wed Apr 13 01:37:58 2011", "hour": 1, "isoweekday": 3, "month": 4, "second": 58, "microsecond": 343000, "isocalendar": [2011, 15, 3], "timetuple": [2011, 4, 13, 1, 37, 58, 2, 103, -1], "year": 2011, "epoch": 1302658678.0, "isoformat": "2011-04-13T01:37:58.343000", "day": 13, "minute": 37}}, {"city": {"city_name": "Framingham", "country": {"country_name": "United States", "id": null}, "id": null}, "id": 14, "status": false, "date_time": {"ctime": "Wed Apr 13 01:38:01 2011", "hour": 1, "isoweekday": 3, "month": 4, "second": 1, "microsecond": 78000, "isocalendar": [2011, 15, 3], "timetuple": [2011, 4, 13, 1, 38, 1, 2, 103, -1], "year": 2011, "epoch": 1302658681.0, "isoformat": "2011-04-13T01:38:01.078000", "day": 13, "minute": 38}}, {"city": {"city_name": "Framingham", "country": {"country_name": "United States", "id": null}, "id": null}, "id": 23, "status": true, "date_time": {"ctime": "Sun Apr 17 21:24:18 2011", "hour": 21, "isoweekday": 7, "month": 4, "second": 18, "microsecond": 625000, "isocalendar": [2011, 15, 7], "timetuple": [2011, 4, 17, 21, 24, 18, 6, 107, -1], "year": 2011, "epoch": 1303075458.0, "isoformat": "2011-04-17T21:24:18.625000", "day": 17, "minute": 24}}];
var table = $("<table border='1'/>");
var thead = $("<thead/>")
.appendTo(table);
$("<tr/>")
.append("<th>Country</th>")
.append("<th>City</th>")
.append("<th>Status</th>")
.append("<th>Time</th>")
.appendTo(thead);
var tbody = $("<tbody/>")
.appendTo(table);
for (var i = 0; i < data.length; i++) {
var citydata = data[i];
$("<tr/>")
.append("<td>" + citydata.city.country.country_name + "</td>")
.append("<td>" + citydata.city.city_name + "</td>")
.append("<td>" + citydata.status + "</td>")
.append("<td>" + citydata.date_time.ctime + "</td>")
.appendTo(tbody);
}
//
// FOR TESTING
//
$("body").append(table);
Did you consider using a Javascript template engine for converting JSON to HTML?
I'm the author of pure.js which is quite original, but there are plenty of classical double-brackets-engines available.
If you are using jQuery consider using the var json = $.parseJSON(data). This will convert your JSON string into a JSON object.
Try it out. It should make getting to your objects a lot easier.