Javascript stringed array into an actual array - javascript

Ok. So the title sounds confusing but that's probably the best way to describe it.
I'm using the TextExt jQuery plugin to create a tag list in a form. However the plugin creates an array in a string when submitted. eg. "["this","that","other"]".
How can I convert this to an actual array? ["this","that","other"]
Cue really simple answer I completely overlooked.

Since you are using jQuery you can just pass it through jQuery.parseJSON like this:
var array = jQuery.parseJSON('["this","that","other"]')

Looks like JSON. So then use JSON.parse(), or better jQuery.parseJSON() when you already have jQuery inlcuded.

Related

How to append a string on an array?

Lets say I have an array field in a document:
entries: ["hello"]
I would like to append new elements to this array field i.e ["Yellow" ,"Blue"]
I did some research and most the techniques replace the entire record. How can I achieve this?
I'm using javascript.
Check out this question: How to append something to an array?
.
And looks like you are developing using firebase firestore, please take a look at their documentation here.
You'll have to retrieve the document you want to edit first, append the array, then update it.

Rendering json using jquery

I need a simple example that has a json data and I have to parse it and render in html. Can someone suggest me a simple way to do this?
You can create a string from an object in JavaScript like this...
var jsonString = JSON.stringify(myJsonObject);
Then you can use that string to apply to a html element. For example...
document.getElementById('myDivID').innerText = jsonString;
With JQuery you can update a DIV with the following...
$("#MyDiv").html(jsonString);
I'm not entirely sure what you are asking. You do not have to use jQuery specifically to parse the object. All you need is standard JavaScript.
Given a JSON string, you can parse it into a JavaScript object using the JSON library
var myJSONObject = JSON.parse(myJSONString);
Or into a string from an object:
var myJSONString= JSON.stringify(myJSONObject);
If you are looking for the individual items of a JSON structure, then you can use a for loop:
for (var key in myJSONObject){
alert(myJSONObject[key]);
}
I've alerted myJSONObject[key] in the above, however, you can do what you want with it.
You would use jQuery to select out the container into which you wanted the info to be displayed, as suggested in usefan's answer.

append object to innerHTML and get that object from innerHTML

here is an example in jsfiddle.
I want to know if I can append a javascript object to innerHTML, that get that object again from innerHTML as object.
something like,
alert((this.innerHTML).html);
that's just an example, don't ask me why do you need this?
I'm trying to edit an existing code, and I have to do this so.
I have to transfer an object via div.innerHTML.
Check this jsfiddle. In it, I add the object to the div as a 'data-'-attribute, using JSON to convert it to a string. After that, adding some comment to the div triggers the DOMSubtreeModified-handler, in which the 'html'-part of the object is retrieved and alerted. It that something to work with?
In this case, quite possible your only option is to convert your object to string and then put that into the element. (This is done by looping through the key, values building the string as you go.)
You would reverse the process to convert it back into an obj.
I know some javascript libary's have helper functions to make this process very simple.
You could try adding the data directly onto the dom element, rather than as its content..
tempDiv.objData = myObject;
It was suggested to use JSON, but no code. So:
function addObjAsJSON(el, obj) {
el.setAttribute('data-myJSON', encodeURIComponent(JSON.stringify(obj)));
}
function getObjAsJSON(el) {
return JSON.parse(decodeURIComponent(el.getAttribute('data-myJSON')));
}
That should allow you to add anything as a serialised object, then get it back. You should add some error checking to make it robust though (e.g. check that you get a string back from the call to getAttribute).
For user agents that don't have built-in JSON support, see json.org which has a link in the javascript section to json.js.

how do parse JSON in javascript?

This is my json string
"[{"/stab/cg/{4CD742B1-C1CA-4708-BE78-0FCA2EB01A86}/TOPS_00":[{"key":"C0.A8.01.06","value":"31"},{"key":"C0.A8.50.01","value":"25"},{"key":"C0.A8.50.81","value":"22"},{"key":"E0.00.00.FC","value":"19"},{"key":"C0.A8.01.FF","value":"18"},{"key":"C0.A8.50.FF","value":"18"},{"key":"4A.7D.EC.5F","value":"11"},{"key":"4A.7D.EC.4E","value":"11"},{"key":"SYS:GROUP_TOTALS","value":"158"}]}]"
after eval('('+ evt.data + ')'), i need to get like this
["/stab/cg/{4CD742B1-C1CA-4708-BE78-0FCA2EB01A86}/TOPS_00",[{"key":"C0.A8.01.06","value":"31"},{"key":"C0.A8.50.01","value":"25"},{"key":"C0.A8.50.81","value":"22"},{"key":"E0.00.00.FC","value":"19"},{"key":"C0.A8.01.FF","value":"18"},{"key":"C0.A8.50.FF","value":"18"},{"key":"4A.7D.EC.5F","value":"11"},{"key":"4A.7D.EC.4E","value":"11"},{"key":"SYS:GROUP_TOTALS","value":"158"}]]
How can i get this using javascript?
If you can utilize jQuery you can use the $.parseJSON() method. Documentation here
For prototype use the .evalJSON() method. Documentation here
JSON.parse has a native implementation in most modern browsers and you can shim it using the implementation on the JSON homepage that Quentin indicated.
Don't use eval for this. A collection of libraries for parsing JSON is listed near the end of the JSON homepage, there are a couple for JavaScript including json2.js which is the usual choice.
Manipulating the data structure has nothing to do with parsing JSON though. If you really want to transform it like then then you'd want something like (untested):
var newObj = [];
for (keys) in myObj) {
newObj.push([key].concat(myObj[key])
}

JSON encoded formatter function name

I'm using jqgrid for which I create column definitions on server as dynamic objects and serialize them using Json.Encode:
html.Raw(System.Web.Helpers.Json.Encode(ColumnDefinition);
I have problem with applying custom formatter, as my serialized column definition is:
{"name":"Icon","index":"Icon","hidden":false,"formatter":"iconFormatter","unformat":{}}
Problem is in quotes which are added to all keys and values to respect JSON specification, and those around iconFormatter are problem in my case as I want that to be my function.
Is there a simple solution for this?
It seems to me that you have the same or close problem as described here. You will have to replace the string values of the formatter properties to the function reference. Pragmatic way is to search for the strings like "iconFormatter" (search for all custom formatters which you use) and replace there to the corresponding function refernce.
UPDATED: If you would be use template property inside of column definition (see here) you would be solve the problem in another way. Additionally you code will be shorter, more clear and better readable.

Categories

Resources