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"
}
]
}
]
Related
I have a nested object displayed in my view and i want to search in it from the query like this :
http://localhost:3000/test?$folder=folder
My object is like this :
const test = {
"item": [
{
"name": "folder",
"item": [{
"name": "subFolder",
"item": [
{
"name": "subFolderTest1",
"item": [{
"path": "/",
"items": [{
"method": "GET",
}]
}]
},
{
"name": "subFolderTest2",
"item": [{
"path": "/",
"items": [{
"method": "GET",
}]
}]
}
]
}]
}
]
}
If i have http://localhost:3000/test?$folder=folder then i would have :
{
{
"name": "subFolder",
"item": [{},{}]
}
}
And if i have http://localhost:3000/test?$folder=folder/subFolder then i will have :
{
{
"name": "subFolderTest1",
"item": [{}]
},
{
"name": "subFolderTest2",
"item": [{}]
}
}
I know how to get what's inside my query and what the user's input but i don't how to search and display from something which is already displayed.
If you already have your query string you could split it by the "/" character. Use the first name to find your first item, the second name to find the second, and so on.
var folder = "folder/subFolder";
var path = folder.split("/");
var currentItem = test["item"];
// Get each name you are looking for
path.forEach(function(name) {
// get each entry of the current item
currentItem.forEach(function(entry) {
// if the name of the entry is the same as the
// name you are looking for then this is the
// next item you are looking for
if (entry.name == name) {
currentItem = entry["item"];
}
});
});
// now currentItem should be the entry specified by your path
You will still have to add code to handle situations like not having the name you are looking for in your current item, or having multiple entries with the correct name, and so on. I just kept it simple for the sake of clarity.
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'm working with a jsonp object, it lists items like this:
{
"played_tracks": [
{
"artist": {
"id": 5524,
"name": "BAAUER",
}
},
"title": "Harlem Shake",
},
{
"artist": {
"id": 114,
"name": "BIRDY",
}
},
"title": "Wings ",
},
{
"artist": {
"id": 1257,
"name": "MILEY CYRUS",
}
},
"title": "Wrecking Ball",
},
{
etc........
Now currently I'm using this code to loop through all of them:
for(i in json.played_tracks)
{
oTrack = json.played_tracks[i];
etc.........
}
How do I adjust my code to only display the last played song? The last song is the first item in the jSONP object.
Instead of looping over json.playedTracks, just fetch the first item of the array like so:
oTrack = json.playedTracks[0];
As you say, the first item is the "last played track," so that should do what you want. If you need the other end of the list though, you can grab that element just as easily:
oTrack = json.playedTracks[json.playedTracks.length - 1];
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)
I know that I can create this JSON:
[
{
"Title": "Something",
"Price": "234",
"Product_Type": "dsf sf"
},
{
"Title": "hskiuea",
"Price": "4234",
"Product_Type": "sdawer"
}
]
*It uses the values obtained from text inputs contained within an element with the class "newpappend" - As shown below
Using the following code which obtains values from my HTML:
var jsonObj = []; //declare array
$(".newpappened").each(function () {
var p_title = $(this).find('#p_title').val();
var p_price = $(this).find('#p_price').val();
var p_ptype = $(this).find('#p_ptype').val();
jsonObj.push({Title: p_title, Price: p_price, Product_Type: p_ptype});
$(this).remove();
});
But, my goal is to end up with the JSON structured like this:
{
"Product_List": {
"Product": [
{
"Title": "asdf",
"Price": "53",
"Product_Type": "Adfsdf"
},
{
"Title": "asgsd",
"Price": "123",
"Product_Type": "Ntohig"
}
]
}
}
Basically, I am struggling with the correct Javascript to use to reach my goal
You are really close. Start with what you have, and then later:
var output = {
"Product_List": {
"Product": jsonObj
}
}
// output is now what you are looking for.