I am getting JSON via ajax like this
{
"questionTypes": [
{
"id": 1,
"name": "text",
"deleted": false,
"version": 0
},
{
"id": 2,
"name": "rating",
"deleted": false,
"version": 0
},
{
"id": 3,
"name": "boolean",
"deleted": false,
"version": 0
},
{
"id": 4,
"name": "option",
"deleted": false,
"version": 0
}
],
"data": [
{
"id": 1,
"category": "default",
"question": "Staff Courtesy",
"deleted": false,
"version": 0
},
{
"id": 2,
"category": "default",
"question": "Staff Response",
"deleted": false,
"version": 0
},
{
"id": 3,
"category": "default",
"question": "Check In",
"deleted": false,
"version": 0
},
{
"id": 4,
"category": "default",
"question": "Check Out",
"deleted": false,
"version": 0
},
{
"id": 5,
"category": "default",
"question": "Travel Desk",
"deleted": false,
"version": 0
},
{
"id": 6,
"category": "default",
"question": "Door Man",
"deleted": false,
"version": 0
},
{
"id": 14,
"category": "client",
"question": "test question",
"deleted": false,
"version": 0
},
{
"id": 15,
"category": "client",
"question": "test1",
"deleted": false,
"version": 0
},
{
"id": 16,
"category": "client",
"question": "test2",
"deleted": false,
"version": 0
},
{
"id": 17,
"category": "client",
"question": "test2",
"deleted": false,
"version": 0
},
{
"id": 21,
"category": "client",
"question": "ggggg",
"deleted": false,
"version": 0
}
]
}
I want to get the name fields from
"questionTypes": [
{
"id": 1,
"name": "text",
"deleted": false,
"version": 0
},
{
"id": 2,
"name": "rating",
"deleted": false,
"version": 0
},
{
"id": 3,
"name": "boolean",
"deleted": false,
"version": 0
},
{
"id": 4,
"name": "option",
"deleted": false,
"version": 0
}
],
$.each(data1.data.questionTypes, function(index, currPat) {
console.log(currPat.name); }
but it did not work ,I get undefined in console.Can any body tell me how to loop exactly
Demo Fiddle
jQuery
$.each(json.data, function (index, currPat) {
$(tbody).append('<tr><td><b>Question from data: ' + currPat.question + '</b></td><td><b>Names from questionTypes</b><select class="qType"></select></td></tr>');
});
var select = $('.qType');
$(json.questionTypes).each(function (index, currPat) {
$(select).append('<option>' + currPat.name + '</option>');
});
You must loop twice: Once for getting the question and then for getting name.
OK, you can do this ,
$(jsonObject.questionTypes).each(function(i,value){
//get value here using value.name
alert(value.name);
});
And the problem was you were actually extracting questionTypes from datawhich is non existent there.
Fiddle Demo
data has no questionTypes
$.each(data1.questionTypes, function(index, currPat) {
alert(currPat.name);
});
in your code I don't see any username properties,
try to use this code.
$.each(data1.data.questionTypes, function(index, currPat) {
console.log(currPat.name); }
Try this
$.each(json.questionTypes, function (index, currPat) {
div=$('<div/>').text(currPat.name);
$('#jsoncontainter').append(div);
});
http://jsfiddle.net/ardeezstyle/497Tx/1/
For your second part of the problem, here is a solution.
$.each(json.data, function (index, currQuest) {
question=$('<div/>',{'class':'question'}).text(currQuest.question);
answer=$('<div/>',{'class':'answer'});
dropdown=$('<select/>');
$.each(json.questionTypes, function (index, currPat) {
option=$('<option/>').text(currPat.name).appendTo(dropdown);
});
answer.append(dropdown);
$('#jsoncontainter').append(question).append(answer);
});
Fiddle
Related
I have two arrays of objects. I want to look at the first one, find typeId then look in the second array for the match (states.typeId == stateTypes.id) then merge those properties into the first array's found match object. If the properties have same key append "stateType" to the property name if not just bring it over. I think an example would best explain it.
Array of objects
"states": [
{
"id": 1,
"typeId": 1,
"name": "CREATED",
"description": "Created",
"label": "Created",
"perviousNotMatchKey": "Text"
},
{
"id": 2,
"typeId": 3,
"name": "ASSIGNED",
"description": "Assigned",
"label": "Assigned",
"perviousNotMatchKey": "Text"
},
{
"id": 3,
"typeId": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"perviousNotMatchKey": "Text"
}
],
"stateTypes": [
{
"id": 1,
"name": "PENDING",
"description": "Pending",
"label": "Pending",
"newIncomingKey": "Text"
},
{
"id": 2,
"name": "IN_PROGRESS",
"description": "In Progress",
"label": "In Progress",
"newIncomingKey": "Text"
},
{
"id": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"newIncomingKey": "Text"
}
],
Wanted array
"newArray": [
{
"id": 1,
"typeId": 1,
"name": "CREATED",
"description": "Created",
"label": "Created",
"perviousNotMatchKey": "Text",
"newIncomingKey": "Text",
"stageType-id": 1,
"stageType-name": "PENDING",
"stageType-description": "Pending",
"stageType-label": "Pending"
},
{
"id": 2,
"typeId": 3,
"name": "ASSIGNED",
"description": "Assigned",
"label": "Assigned",
"perviousNotMatchKey": "Text",
"newIncomingKey": "Text",
"stageType-id": 3,
"stageType-name": "COMPLETED",
"stageType-description": "Completed",
"stageType-label": "Completed"
},
{
"id": 3,
"typeId": 2,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"perviousNotMatchKey": "Text",
"newIncomingKey": "Text",
"stageType-id": 2,
"stageType-name": "IN_PROGRESS",
"stageType-description": "In Progress",
"stageType-label": "In Progress"
}
],
Something like this:
const state = [
{
"id": 1,
"typeId": 1,
"name": "CREATED",
"description": "Created",
"label": "Created",
"perviousNotMatchKey": "Text"
},
{
"id": 2,
"typeId": 3,
"name": "ASSIGNED",
"description": "Assigned",
"label": "Assigned",
"perviousNotMatchKey": "Text"
},
{
"id": 3,
"typeId": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"perviousNotMatchKey": "Text"
}
];
const stateTypes = [
{
"id": 1,
"name": "PENDING",
"description": "Pending",
"label": "Pending",
"newIncomingKey": "Text"
},
{
"id": 2,
"name": "IN_PROGRESS",
"description": "In Progress",
"label": "In Progress",
"newIncomingKey": "Text"
},
{
"id": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"newIncomingKey": "Text"
}
];
const temp = {};
for (const obj1 of state) {
temp[obj1.id] = { ...obj1 };
}
for (const obj2 of stateTypes) {
const destination = temp[obj2.id];
for (const [key, value] of Object.entries(obj2)) {
if (destination[key]) destination[`stateTypes-${key}`] = value;
else destination[key] = value;
}
}
const newArray = Object.values(temp);
console.log(newArray);
I've got a array of object in which the children got arrays as well:
[{
"id": 0,
"name": "Primary",
"xPoints": [{
"id": 0,
"name": "Untitledtest12",
"type": "custom",
"description": ""
}, {
"id": 1,
"name": "asd",
"description": "",
"type": "custom"
}, {
"id": 2,
"name": "asd",
"description": "",
"type": "custom"
}, {
"id": 3,
"name": "asd123",
"description": "",
"type": "custom"
}, {
"id": 4,
"name": "a",
"description": "a",
"type": "custom"
}]
}, {
"id": 1,
"name": "Untitled X Line",
"xPoints": [
{},
{},
{},
{},
{},
{
"id": 0,
"name": "this is the farthest",
"type": "custom",
"description": ""
},
{}
]
}]
what I'm looking for is a way to get the "farthest" of object index on these childrens of the main array
Expected result:
index: 6 from second's "Untitled X Line"
You can use recursion to get the depth of the object.
const obj = [{
"id": 0,
"name": "Primary",
"xPoints": [{
"id": 0,
"name": "Untitledtest12",
"type": "custom",
"description": ""
}, {
"id": 1,
"name": "asd",
"description": "",
"type": "custom"
}, {
"id": 2,
"name": "asd",
"description": "",
"type": "custom"
}, {
"id": 3,
"name": "asd123",
"description": "",
"type": "custom"
}, {
"id": 4,
"name": "a",
"description": "a",
"type": "custom"
}]
}, {
"id": 1,
"name": "Untitled X Line",
"xPoints": [
{},
{},
{},
{},
{},
{
"id": 0,
"name": "this is the farthest",
"type": "custom",
"description": ""
},
{}
]
}]
function depthOf(object) {
var level = 1;
for(var key in object) {
if (!object.hasOwnProperty(key)) continue;
if(typeof object[key] == 'object'){
var depth = depthOf(object[key]) + 1;
level = Math.max(depth, level);
}
}
return level;
}
var maxLevels = 0;
var maxObj;
obj.forEach(e => maxLevels < depthOf(e) ? maxObj = e : '');
console.log(maxObj);
can you please tell me how to validate form which is prepared dynamically using dfom plugin?
I used jquery validation but it don't work on chrome.
can you please tell me why dform with jquery validation not working in chrome ?.It is working fine in firefox but not with in chrome why ?? Here is http://jsfiddle.net/8bweG/13/?
$("#sform").dform(
{
"elements": [
{
"html": [
{
"html": [
{
"type": "number",
"id": "totalRetryCount",
"name": "totalRetryCount",
"required": false,
"value": 0,
"tabindex": 1,
"validate": {
"messages": {
"number": "Naveen Kanjush hain"
}
}
}
],
"type": "fieldset",
"caption": "Total Retry Count"
},
{
"html": [
{
"type": "number",
"id": "totalRepeatCount",
"name": "totalRepeatCount",
"required": false,
"value": 0,
"tabindex": 2,
"validate": {
"messages": {
"number": "Naveen Kanjush hain"
}
}
}
],
"type": "fieldset",
"caption": "Total Repeat Count"
},
{
"html": [
{
"type": "select",
"options": {
"true": "true",
"false": "false"
},
"id": "summaryReportRequired",
"name": "summaryReportRequired",
"required": false,
"value": "true",
"tabindex": 3,
}
],
"type": "fieldset",
"caption": "Summary Report Required"
},
{
"html": [
{
"type": "select",
"options": {
"ALWAYS": "ALWAYS",
"ON_SUCCESS": "ON_SUCCESS"
},
"id": "postConditionExecution",
"name": "postConditionExecution",
"required": false,
"value": "ON_SUCCESS",
"tabindex": 4,
}
],
"type": "fieldset",
"caption": "Post Condition Execution"
}
],
"type": "div",
"class": "inputDiv",
"caption": "<h3>Configuration Parameters</h3>"
}
],
"id": "testSuiteConfigurationform",
"name": "testSuiteConfigurationform",
"method": "post"
}
);
Problem is that
First field of type ="number".User only type number in that .If user type string or character and move to another field it gives error.if you check on firefox this fiddle it is giving validation error .But on chrome and safari same functionality not work.
please follow these steps
on first field write "abc" then click below or next field.it gives error front of first field.it is display on firefox but not on chrome
It is working but you are missing the Submit button.
This is the correct version: http://jsfiddle.net/8bweG/22/
$("#sform").dform({
"action" : "http://dgg.gg",
"method" : "get",
"elements": [
{
"html": [
{
"html": [
{
"type": "number",
"id": "totalRetryCount",
"name": "totalRetryCount",
"required": false,
"value": 0,
"tabindex": 1,
"validate": {
"messages": {
"number": "Naveen Kanjush hain"
}
}
}
],
"type": "fieldset",
"caption": "Total Retry Count"
},
{
"html": [
{
"type": "number",
"id": "totalRepeatCount",
"name": "totalRepeatCount",
"required": false,
"value": 0,
"tabindex": 2,
"validate": {
"messages": {
"number": "Naveen Kanjush hain"
}
}
}
],
"type": "fieldset",
"caption": "Total Repeat Count"
},
{
"html": [
{
"type": "select",
"options": {
"true": "true",
"false": "false"
},
"id": "summaryReportRequired",
"name": "summaryReportRequired",
"required": false,
"value": "true",
"tabindex": 3,
}
],
"type": "fieldset",
"caption": "Summary Report Required"
},
{
"html": [
{
"type": "select",
"options": {
"ALWAYS": "ALWAYS",
"ON_SUCCESS": "ON_SUCCESS"
},
"id": "postConditionExecution",
"name": "postConditionExecution",
"required": false,
"value": "ON_SUCCESS",
"tabindex": 4,
}
],
"type": "fieldset",
"caption": "Post Condition Execution"
},
{
"html": [
{
"type" : "submit",
"value" : "Login"
}
],
"type": "fieldset",
"caption": "Go away"
}
],
"type": "div",
"class": "inputDiv",
"caption": "<h3>Configuration Parameters</h3>"
}
],
"id": "testSuiteConfigurationform",
"name": "testSuiteConfigurationform",
"method": "post"
}
);
I have this collection: https://graph.facebook.com/2playcz/albums
This collection contains 8 id. How can i get the total count id of this collection using javascript? (Total = 8)
Source:
{
"data": [
{
"id": "201936779932071",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Tr\u00e9ninky",
"link": "https://www.facebook.com/album.php?fbid=201936779932071&id=190320081093741&aid=41883",
"cover_photo": "201937046598711",
"count": 8,
"type": "normal",
"created_time": "2012-07-02T09:33:43+0000",
"updated_time": "2012-09-15T12:05:44+0000",
"can_upload": false,
"likes": {
"data": [
{
"id": "1788805921",
"name": "Edita Nov\u00e1"
},
{
"id": "100001449904219",
"name": "Mirka Brani\u0161ov\u00e1"
}
],
"paging": {
"next": "https://graph.facebook.com/201936779932071/likes?limit=25&offset=25&__after_id=100001449904219"
}
}
},
{
"id": "205206429605106",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Turnaje a akce",
"link": "https://www.facebook.com/album.php?fbid=205206429605106&id=190320081093741&aid=42900",
"cover_photo": "205208716271544",
"count": 14,
"type": "normal",
"created_time": "2012-07-10T19:36:53+0000",
"updated_time": "2012-09-15T12:04:05+0000",
"can_upload": false
},
{
"id": "221784994613916",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Tenisova \u0161kolka 2play",
"description": "Tenisov\u00e1 \u0161kolka 2play",
"link": "https://www.facebook.com/album.php?fbid=221784994613916&id=190320081093741&aid=49379",
"cover_photo": "221785024613913",
"count": 9,
"type": "normal",
"created_time": "2012-08-31T11:19:59+0000",
"updated_time": "2012-09-14T15:17:53+0000",
"can_upload": false
},
{
"id": "203405996451816",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Webov\u00e9 fotografie - Logo",
"link": "https://www.facebook.com/album.php?fbid=203405996451816&id=190320081093741&aid=42285",
"cover_photo": "203406586451757",
"count": 11,
"type": "normal",
"created_time": "2012-07-05T10:12:40+0000",
"updated_time": "2012-09-14T15:16:40+0000",
"can_upload": false
},
{
"id": "190332361092513",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Cover Photos",
"link": "https://www.facebook.com/album.php?fbid=190332361092513&id=190320081093741&aid=39232",
"cover_photo": "225939404198475",
"count": 2,
"type": "normal",
"created_time": "2012-06-09T13:52:38+0000",
"updated_time": "2012-09-12T18:15:51+0000",
"can_upload": false
},
{
"id": "190802884378794",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Wall Photos",
"link": "https://www.facebook.com/album.php?fbid=190802884378794&id=190320081093741&aid=39324",
"cover_photo": "190802891045460",
"count": 2,
"type": "wall",
"created_time": "2012-06-10T13:19:48+0000",
"updated_time": "2012-07-17T17:16:19+0000",
"can_upload": false
},
{
"id": "205207126271703",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Ostatn\u00ed",
"link": "https://www.facebook.com/album.php?fbid=205207126271703&id=190320081093741&aid=42902",
"cover_photo": "205209679604781",
"count": 4,
"type": "normal",
"created_time": "2012-07-10T19:40:05+0000",
"updated_time": "2012-07-16T14:47:16+0000",
"can_upload": false,
"likes": {
"data": [
{
"id": "100001449904219",
"name": "Mirka Brani\u0161ov\u00e1"
}
],
"paging": {
"next": "https://graph.facebook.com/205207126271703/likes?limit=25&offset=25&__after_id=100001449904219"
}
}
},
{
"id": "190320914426991",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Profile Pictures",
"link": "https://www.facebook.com/album.php?fbid=190320914426991&id=190320081093741&aid=39224",
"cover_photo": "190327474426335",
"count": 1,
"type": "profile",
"created_time": "2012-06-09T13:29:16+0000",
"updated_time": "2012-06-09T13:43:08+0000",
"can_upload": false
},
{
"id": "190322704426812",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Logo",
"description": "Loga spole\u010dnosti",
"link": "https://www.facebook.com/album.php?fbid=190322704426812&id=190320081093741&aid=39225",
"type": "normal",
"created_time": "2012-06-09T13:34:09+0000",
"updated_time": "2012-07-05T10:16:58+0000",
"can_upload": false
}
]
}
If you mean the count of items in the data array, it would just be data.length.
First of all, there 9 ids. If you want to count the number of elements in array there's a built-in property length. So that if your object is called x you retrieve the length of data by accessing x.data.length.
On the other hand, if you wanted to count the number of unique ids (IDs should by unique anyway, but if you really really wanted to) you have to manually iterate through the array and count them:
var countIds = function (arr) {
var uniqueIds = {}, num = 0;
if (!arr.data) return false;
arr.data.forEach(function (val, i) {
if (typeof uniqueIds[val.id] === "undefined") {
++num;
uniqueIds[val.id] = true;
}
});
return num;
};
I have JSON similar to this . I wish to extract values like name, his id, and product title from this list . But I am not able to figure it out . I was trying "eval" for the same.
{
"data": [{
"id": "3092773937557",
"from": {
"id": "1810306393",
"name": "Prashant Singh"
},
"start_time": "2012-07-21T09:12:53+0000",
"end_time": "2012-07-21T09:12:53+0000",
"publish_time": "2012-07-21T09:12:53+0000",
"application": {
"id": "132692593533721",
"name": "Compare Hatke"
},
"data": {
"productname": "Apple iPod Nano",
"price": 399,
"product": {
"id": "10151004296768984",
"url": "http:\/\/compare.buyhatke.com\/products\/Apple-iPod-Nano",
"type": "comparehatke:product",
"title": "Apple iPod Nano"
}
},
"likes": {
"count": 0
},
"comments": {
"count": 0
},
"no_feed_story": false
}, {
"id": "3092770217464",
"from": {
"id": "1810306393",
"name": "Prashant Singh"
},
"start_time": "2012-07-21T09:08:53+0000",
"end_time": "2012-07-21T09:08:53+0000",
"publish_time": "2012-07-21T09:08:53+0000",
"application": {
"id": "132692593533721",
"name": "Compare Hatke"
},
"data": {
"productname": "Apple iPod Nano",
"price": 399,
"product": {
"id": "10151004296768984",
"url": "http:\/\/compare.buyhatke.com\/products\/Apple-iPod-Nano",
"type": "comparehatke:product",
"title": "Apple iPod Nano"
}
},
"likes": {
"count": 0
},
"comments": {
"count": 0
},
"no_feed_story": false
}],
"paging": {
"next": "https:\/\/graph.facebook.com\/me\/comparehatke:compare\/?access_token=AAAB4rubm4xkBAHRhdjVgx7JxIIvUxImIm31AMxgnqEAOQsAsgZAJjBYUfvzKc8XgxDBg3AzKN1S6QU2dnmtgj7TPcoCiih1RzrL3pLpuZAgGt8eKpq&limit=2&method=get&pretty=0&offset=2"
}
}
<html>
<script style="text/javscript">
var myObject = { "data": [{
"id": "3092741696751",
"from": {
"id": "1810306393",
"name": "Prashant Singh"
},
"start_time": "2012-07-21T08:40:38+0000",
"end_time": "2012-07-21T08:40:38+0000",
"publish_time": "2012-07-21T08:40:38+0000",
"application": {
"id": "132692593533721",
"name": "Compare Hatke"
},
"data1": {
"productname": "Apple iPod Nano",
"price": 399,
"product": {
"id": "10151004296768984",
"url": "http:\/\/compare.buyhatke.com\/products\/Apple-iPod-Nano",
"type": "comparehatke:product",
"title": "Apple iPod Nano"
}
},
"likes": {
"count": 0
},
"comments": {
"count": 0
},
"no_feed_story": false
} ]};
alert(myObject.data[0].id);
</script>
</html>
data = JSON.parse(yourJSONString);
If this fails, you likely have an error in your JSON. You can use http://jsonlint.com/ to find and resolve the problem. In the paste above, you're missing your closing ]}.