Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a javascript object in the below format. I want to combine Name and Status for each block and store in an array.
{
"datatype": "local",
"data": [
{
"Name": "John",
"status": "To Be Processed",
"LastUpdatedDate": "2013-05-31 08:40:55.0"
},
{
"Name": "Paul",
"status": "To Be Processed",
"LastUpdatedDate": "2013-06-02 16:03:00.0"
}
}
Desired Output
John + To Be Processed
Paul + To Be Processed
Please Help ?
NOTE: Your json is not valid. It should be
"data": [{...},{...}] // ] is mising
var data = {
"datatype": "local",
"data": [
{
"Name": "John",
"status": "To Be Processed",
"LastUpdatedDate": "2013-05-31 08:40:55.0"
},
{
"Name": "Paul",
"status": "To Be Processed",
"LastUpdatedDate": "2013-06-02 16:03:00.0"
}]
};
var len = data.data.length;
var txt = "";
for(var i=0;i<len;i++){
txt += "<div>"+data.data[i].Name+" "+data.data[i].status+"</div>";
}
$("body").append(txt);
DEMO here.
You can use $.map() to create a list of processed values
var data = {
"datatype": "local",
"data": [{
"Name": "John",
"status": "To Be Processed",
"LastUpdatedDate": "2013-05-31 08:40:55.0"
}, {
"Name": "Paul",
"status": "To Be Processed",
"LastUpdatedDate": "2013-06-02 16:03:00.0"
}]
};
var result = $.map(data.data, function(obj){
return obj.Name + ' ' + obj.status
});
console.log(result)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
i have array value. need to convert this array value into json format. example is given bleow
Sample Array
[Management Portal!#!#Production Issue Handling!#!#/IONSWeb/refDataManagement/searchDynamicScripts.do, Management Portal!#!# Event Browser!#!#/IONSWeb/orderManagement/eventBrowser.do, Management Portal!#!# Order Workflow!#!#/IONSWeb/orderManagement/SearchOrdersWorkflow.do, ADMINISTRATION!#!#Admin Message!#!#/IONSWeb/userManagement/getMessageForBroadcast.do, ADMINISTRATION!#!#Audit!#!#/IONSWeb/userManagement/auditManagement.do, ADMINISTRATION!#!#Locks!#!#/IONSWeb/userManagement/lockSearch.do, ADMINISTRATION!#!#Queue!#!#/IONSWeb/GroupManagement/begin.do, ADMINISTRATION!#!#Role!#!#/IONSWeb/userManagement/goToRolePage.do, ADMINISTRATION!#!#Routing Rule!#!#/IONSWeb/ruleManagement/showRules.do, ADMINISTRATION!#!#Task Code!#!#/IONSWeb/ManageTaskCode/begin.do, ADMINISTRATION!#!#Trigger OutEvent!#!#/IONSWeb/triggerOutEvent.jsp, ADMINISTRATION!#!#User!#!#/IONSWeb/userManagement/begin.do, ADMINISTRATION!#!#Refresh Application Cache!#!#/IONSWeb/userManagement/refreshApplnCache.do]
sample Json
{
"name": "Administration",
"sub": [
{
"name": "Add Order",
"url": "/IONSWeb/userManagement/auditManagement.do"
},
{
"name": "Infrastructure sonet Add Order ",
"url": "/IONSWeb/userManagement/auditManagement.do"
},
{
"name": "fGNS Add Order",
"url": "/IONSWeb/userManagement/auditManagement.do"
}
]
}
Please anyone help on this
I think you want to do something like this. Split the string, get out the first element, that will be the name, and iterate through all the elements. Every even value will be the name, and every odd an url.
When it is odd, then add it to the sub array. Thats it.
var string = 'Management Portal!#!#Production Issue Handling!#!#/IONSWeb/refDataManagement/searchDynamicScripts.do, Management Portal!#!# Event Browser!#!#/IONSWeb/orderManagement/eventBrowser.do, Management Portal!#!# Order Workflow!#!#/IONSWeb/orderManagement/SearchOrdersWorkflow.do, ADMINISTRATION!#!#Admin Message!#!#/IONSWeb/userManagement/getMessageForBroadcast.do, ADMINISTRATION!#!#Audit!#!#/IONSWeb/userManagement/auditManagement.do, ADMINISTRATION!#!#Locks!#!#/IONSWeb/userManagement/lockSearch.do, ADMINISTRATION!#!#Queue!#!#/IONSWeb/GroupManagement/begin.do, ADMINISTRATION!#!#Role!#!#/IONSWeb/userManagement/goToRolePage.do, ADMINISTRATION!#!#Routing Rule!#!#/IONSWeb/ruleManagement/showRules.do, ADMINISTRATION!#!#Task Code!#!#/IONSWeb/ManageTaskCode/begin.do, ADMINISTRATION!#!#Trigger OutEvent!#!#/IONSWeb/triggerOutEvent.jsp, ADMINISTRATION!#!#User!#!#/IONSWeb/userManagement/begin.do, ADMINISTRATION!#!#Refresh Application Cache!#!#/IONSWeb/userManagement/refreshApplnCache.do';
var pieces = string.split('!#!#');
var first = pieces[0];
//Get out the first one, that will be the key
pieces.shift();
//Create the object
var object = {
'name': first,
'sub': []
};
//Iterate through elements
var i = 0;
var sub = [];
$.each(pieces, function (idx, piece) {
if (i % 2 == 0) {
sub['name'] = piece;
} else {
sub['url'] = piece;
object.sub.push(sub);
}
i++;
});
console.log(object);
Try something like this:
Fiddle: https://jsfiddle.net/ug85d7o7/6/
var jsonData = [],
item, name,
subItem, subUrl,
i, j,
a = [
"Management Portal!#!#Production Issue Handling!#!#/IONSWeb/refDataManagement/searchDynamicScripts.do",
"Management Portal!#!# Event Browser!#!#/IONSWeb/orderManagement/eventBrowser.do",
"Management Portal!#!# Order Workflow!#!#/IONSWeb/orderManagement/SearchOrdersWorkflow.do",
"ADMINISTRATION!#!#Admin Message!#!#/IONSWeb/userManagement/getMessageForBroadcast.do",
"ADMINISTRATION!#!#Audit!#!#/IONSWeb/userManagement/auditManagement.do",
"ADMINISTRATION!#!#Locks!#!#/IONSWeb/userManagement/lockSearch.do",
"ADMINISTRATION!#!#Queue!#!#/IONSWeb/GroupManagement/begin.do",
"ADMINISTRATION!#!#Role!#!#/IONSWeb/userManagement/goToRolePage.do",
"ADMINISTRATION!#!#Routing Rule!#!#/IONSWeb/ruleManagement/showRules.do",
"ADMINISTRATION!#!#Task Code!#!#/IONSWeb/ManageTaskCode/begin.do",
"ADMINISTRATION!#!#Trigger OutEvent!#!#/IONSWeb/triggerOutEvent.jsp",
"ADMINISTRATION!#!#User!#!#/IONSWeb/userManagement/begin.do",
"ADMINISTRATION!#!#Refresh Application Cache!#!#/IONSWeb/userManagement/refreshApplnCache.do"
];
for(i=0; i<a.length; i++)
{
item = a[i].split("!#!#");
name = item[0];
subName = item[1];
subUrl = item[2];
subItem = null;
for (j=0; j<jsonData.length; j++)
{
if (jsonData[j].Name == name)
{
subItem = jsonData[j].sub;
break;
}
}
if (!subItem)
{
jsonData.push({"Name" : name, "sub" : [] });
subItem = jsonData[jsonData.length-1].sub;
}
subItem.push({"Name" : subName, "url" : subUrl });
}
alert(JSON.stringify(jsonData));
Result:
[
{
"Name": "Management Portal",
"sub": [
{
"Name": "Production Issue Handling",
"url": "/IONSWeb/refDataManagement/searchDynamicScripts.do"
},
{
"Name": " Event Browser",
"url": "/IONSWeb/orderManagement/eventBrowser.do"
},
{
"Name": " Order Workflow",
"url": "/IONSWeb/orderManagement/SearchOrdersWorkflow.do"
}
]
},
{
"Name": "ADMINISTRATION",
"sub": [
{
"Name": "Admin Message",
"url": "/IONSWeb/userManagement/getMessageForBroadcast.do"
},
{
"Name": "Audit",
"url": "/IONSWeb/userManagement/auditManagement.do"
},
{
"Name": "Locks",
"url": "/IONSWeb/userManagement/lockSearch.do"
},
{
"Name": "Queue",
"url": "/IONSWeb/GroupManagement/begin.do"
},
{
"Name": "Role",
"url": "/IONSWeb/userManagement/goToRolePage.do"
},
{
"Name": "Routing Rule",
"url": "/IONSWeb/ruleManagement/showRules.do"
},
{
"Name": "Task Code",
"url": "/IONSWeb/ManageTaskCode/begin.do"
},
{
"Name": "Trigger OutEvent",
"url": "/IONSWeb/triggerOutEvent.jsp"
},
{
"Name": "User",
"url": "/IONSWeb/userManagement/begin.do"
},
{
"Name": "Refresh Application Cache",
"url": "/IONSWeb/userManagement/refreshApplnCache.do"
}
]
}
]
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
var resultTable = $('#templateRegArea').DataTable({
"columns": [
{ data: "ID" },
{ data: "Name" },
{ data: "Description" },
],
});
resultTable.rows.add(response.d).draw();
dataSet = response.d;
I am trying to bind a table 'templateRegArea' with the values present in "response.d" in a Jquery datatable.
response.d
I have attached the value of "response". The problem I am facing is the data are not getting loaded into the table.
HELP :(
Your server-side script produces objects, when jQuery DataTables requires array of arrays or array of objects. For example:
{
"d": [{
"ID": "1",
"Name": "John",
"Description": "Test"
}, {
"ID": "2",
"Name": "Bob",
"Description": "Test"
}]
}
When you correct the data structure as shown below, initialization code should be changed to:
var resultTable = $('#templateRegArea').DataTable({
"data": response.d,
"columns": [
{ "data": "ID" },
{ "data": "Name" },
{ "data": "Description" }
]
});
With the existing data structure you can use the code below, but that is only good for one data row.
var resultTable = $('#templateRegArea').DataTable({
"data": [response.d],
"columns": [
{ "data": "ID" },
{ "data": "Name" },
{ "data": "Description" }
]
});
I'm newbee in JS so i need your help.
I have this JSON code :
{
"data": {
"people": [
"get_my_obj": [
{
"yearcome": 2006,
"email": "email1#test.com",
"came": "1.12",
"name": "Alex "
},
{
"yearcome": 2010,
"email": "email2#test.com",
"came": "1.12",
"name": "John"
},
{
"yearcome": 2012,
"email": "email3#test.com",
"came": "1.12",
"name": "Max"
}
]
}
}
How i can get string of elements with emails?
For example var a = email1#test.com;email2#test.com;email3#test.com;
Thats really hard for me because if i have only 2 objects so string must show 2 emails if 3 objects show 3 emails.
Please help me find the way how to do it
var s = { data: ....};
var mails = s.data.people.get_my_obj.map(function (e) { return e.email; }).join(';');
console.log(mails);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
I am currently trying to send a user information about a JSON object that I've recieved from an API. An example of the format is
[
{
"lang_code": "eng",
"site_language": "1",
"name": "English"
},
{
"lang_code": "afr",
"site_language": "1",
"name": "Afrikaans"
},
{
"lang_code": "ale",
"site_language": "0",
"name": "Aleut"
},
]
I want to be able to access the lang_code property of every single language and send it. I've tried to use
var languageCodes;
var languageResult = body.lang_code; //body is the result from a request.get({ ... })
for(var codes in languageResult) {
languageCodes = languageResult[codes];
}
Object.keys does nothing, as it just sends 72 numbers to me. Any thoughts?
On a side note, I also want people to be able to type "! languages [my command] eng", for example, and it sends "English" instead of just sending "1 is [object Object]".
Assuming body is the array at the top of your question, if you just want an array of all the language codes, this should suffice
var languageCodes = body.map(function(lang) {
return lang.lang_code;
});
var body = [{
"lang_code": "eng",
"site_language": "1",
"name": "English"
}, {
"lang_code": "afr",
"site_language": "1",
"name": "Afrikaans"
}, {
"lang_code": "ale",
"site_language": "0",
"name": "Aleut"
}];
var languageCodes = body.map(function(lang) {
return lang.lang_code;
});
document.getElementById('out').innerHTML = JSON.stringify(languageCodes);
<pre id="out"></pre>
I looped through your lang_codes like this:
var codes = [{"lang_code":"eng","site_language":"1","name":"English"}, {"lang_code":"afr","site_language":"1","name":"Afrikaans"},{"lang_code":"ale","site_language":"0","name":"Aleut"}];
for(var i = 0; i < codes.length; i++) {
console.log(codes[i].lang_code);
}
I have some JSON data which is in the following format:
[
{
"id": 145,
"Name": "John",
"company_name": "A",
"email": "john#gmail.com",
"country": "USA"
},
{
"id": 500,
"Name": "Mike",
"company_name": "B",
"email": "mike#gmail.com",
"country": "London"
},
{
"id": 100,
"Name": "Sally",
"company_name": "C",
"email": "sally#gmail.com",
"country": "USA"
}
]
Now, suppose I ask the user to enter an id, say 100. Then I need to display all the details for this id.
I am supposed to do this as a part of a web application,where I have to invoke an display the fields of a particular id. This would have been easy if I had a hash like implementation and could display all parameters based on the key-id.
Can anybody tell me how this can be done using such kind of data?
Thanks!
You could use something like this:
(Assuming the you have a variable data with your Json Object).
function getid(id) {
var nobj;
data.forEach(function(obj) {
if(obj.id == id)
nobj = obj;
});
return nobj
}
var neededobj = getid(100);
console.log(neededobj.Name + "\n" + neededobj.email + "\netc...");
But to get the Object you have to loop through your complete array,
until it finds the right Object
see this Fiddle
I think you are looking for Associative Array,
the simplex one would be,
var associativeArray = [];
associativeArray["one"] = "First";
associativeArray["two"] = "Second";
associativeArray["three"] = "Third";
alert(associativeArray.one);
And obviusly you can add json object in value place