Click here to Skip to main content
14,452,520 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a company, department drop-down list.
If I select a company, then department dropdown appears, after select multiple departments, the result show me who are belong to those company + departments.
The problem is I need to store those people in a JSON object to send back to a server.

What I have tried:

departmentSelector.onchange = function() {
    persons.innerHTML = "";
    var selectedDepartments = document.querySelectorAll("[name='departmentSelector'] option:checked");

    var myObj = {};

    for (var i = 0; i < selectedDepartments.length; i++) {
        var temp3 = selectedDepartments[i].dataset.company;
        var prsonsArray = database[temp3][selectedDepartments[i].value];
        console.log(prsonsArray);
        for (var x = 0; x < prsonsArray.length; x++) {
            persons.innerHTML += "<li>" + prsonsArray[x] + "</li>";
            console.log(myObj);
        }
    }
}
Posted
Updated 1-Apr-19 9:23am
Comments
Richard Deeming 4-Apr-19 13:26pm
   
What is the JSON supposed to look like, and where are you stuck?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Is there a reason you're not just emitting to say, a StringBuilder?

I usually just spit out JSON that way. It's so simple it's hard to get it wrong.

The biggest thing is spitting out json string escapes but that's pretty trivial

This will give you a properly escaped JSON string literal from a C# string.
Every other JSON datatype except "null" can be written out with ToString()

public static string ToStringLiteral(string str)
		{
			if (null == str) return "null";
			StringBuilder result = new StringBuilder();
			result.Append("\"");
			foreach (char ch in str)
			{
				switch (ch)
				{
					case '\b':
						result.Append("\\b");
						break;
					/*case '/':
						result.Append("\\/");
						break;*/
					case '\\':
						result.Append("\\\\");
						break;
					case '\t':
						result.Append("\\t");
						break;
					case '\r':
						result.Append("\\r");
						break;
					case '\n':
						result.Append("\\n");
						break;
					case '\"':
						result.Append("\\\"");
						break;
					default:
						if (char.IsControl(ch))
						{
							result.Append("\\u");
							ushort u = ch;
							result.Append(((byte)(u / 256)).ToString("x2"));
							result.Append(((byte)(u % 256)).ToString("x2"));
						}
						else
							result.Append(ch);
						break;
				}
			}
			result.Append('\"');
			return result.ToString();
		}
   
v2
Comments
NULL Value 1-Apr-19 14:43pm
   
Thank for your solution.
However, I'm not using C# in this case. I believed that I put a hashtag #javascript.
myObj = Object.assign({},prsonsArray); would be used in this case.
honey the codewitch 1-Apr-19 14:47pm
   
whoops, i need more coffee. Still that should be easy enough to port to javascript.

just using string concatenation instead of stringbuilder

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100