Javascript: get the number of json objects when using #Html.Raw - javascript

To get a list of c# objects I'm using the following Javascript code:
var objectList= '#Html.Raw(Json.Encode(Model.ObjectList))';
How would I get the number of objects within objectList?
I tried using .length and .size().

What you are setting is a string type to objectList, try:
var objectList = #Html.Raw(Json.Encode(Model.ObjectList));
console.log(objectList.length);
Inspect the DOM in F12 tools to make sure the JSON is correct.

Related

getComputedStyle Font Family returns multiple families [duplicate]

Hi I have an object rowObject passed into a javascript function. When I inspect it by putting it into an alert I see something like:
435,345,345345,56456
What I want to achieve is to get the first integer ie. 435 from the list.
I know how to do this in server side code but client side code.
Can someone please help me with this?
Assuming that your rowObject is a string, you can use .split() to split the comma delimited list. At this point you can access the array of items by index and get the first element.
http://www.w3schools.com/jsref/jsref_split.asp
An Example
var rowObject = "435,345,345345,56456";
var splitRowObject = rowObject.split(',');
if(splitRowObject.length > 0)
alert(splitRowObject[0]);
alert() is calling objects toString() method, so you don't know the structure of the object. It is a good idea to use console.log instead for logging objects as in modern browsers it will allow you to explore structure of the object in the console window.
One of the solutions you can do without knowing the structure is:
var firstInteger = +rowObject.toString().split(',')[0] // 435
That works if rowObject is string, array or everything else :).
EDIT: Putting + before the string will try to convert it to a number.
Have you tried rowObject[0]?
The fact that the alert shows 435,345,345345,56456 doesn't mean that the object is string, it could be Object and Array as well as their toString method implemented to display it in such way. For example the native array is also looks like that when alerting or converting to string, so you need to call toString method at first then split it by comma:
var firstInt = rowObject.toString().split(',')[0];

javascript getElementsByClassName from javascript variable

I'm getting some html from a angular get request and I'm trying to get some specific elements from this response. Which is a list of div's with the class "post-holder". Extracting the html works fine, but to extract each div to insert separately has some troubles.
This is the code I'm trying with:
var firstTag = '<main id="posts">';
var res = data.substring(data.indexOf(firstTag) + firstTag.length, data.indexOf('</main>'));
var html2 = $.parseHTML( res );
var x = html2.getElementsByClassName("post-holder");
The last line gives me the following error in chrome: "TypeError: undefined is not a function".
I'm guessing that getElementsByClass has some troubles with the variable generated by jquery. Is there another approach to do the same or a way to fix what I already have?
See the documentation for parseHTML:
Returns: Array
Description: Parses a string into an array of DOM nodes.
getElementsByClassName is a method on DOM elements, not on arrays. You need to loop over the array, check if each value is an element (as opposed to, for instance, a text node or a comment) and then call getElementsByClassName on the values.
Or you could just construct a jQuery object and use .find() on it instead.

Undefined values in Javascript object

So I am trying to store an array of objects into localStorage, as follows:-
EDIT: The following is part of a function that is called in a loop.
c = [{"name":nameDOM.value,"add":+addDOM.value,"town":townDOM.value,"post":postalDOM.value,"mob":mobDOM.value}];
cData = cData.concat(c);
localStorage.setItem('cData', cData);
However, after a page refresh, when I try to access data from the objects, it is apparently undefined. Accessing data from the objects is fine before a refresh.
I am accessing the data in the following manner:-
//Table code omitted.
var text = document.createTextNode(""+cData[i].name+", "+cData[i].add+", "+cData[i].town+", "+cData[i].post+", "+cData[i].mob+"");
I have been trying to debug the problem using Chromes Javascript tools, as well as inserting alerts into various places to monitor the state of the variables; still undefined.
You've made an oopsies. Try:
c = [{"name":nameDOM.value,"add":+addDOM.value,"town":townDOM.value,"post":postalDOM.value,"mob":mobDOM.value}];
cData = cData.concat(c);
localStorage.setItem('cData', JSON.stringify(cData));
They difference being that you are turning your array of objects into a json string that can be parsed later by your code using:
eval(localStorage.getItem('cData'));

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.

Get first item from comma delimited object

Hi I have an object rowObject passed into a javascript function. When I inspect it by putting it into an alert I see something like:
435,345,345345,56456
What I want to achieve is to get the first integer ie. 435 from the list.
I know how to do this in server side code but client side code.
Can someone please help me with this?
Assuming that your rowObject is a string, you can use .split() to split the comma delimited list. At this point you can access the array of items by index and get the first element.
http://www.w3schools.com/jsref/jsref_split.asp
An Example
var rowObject = "435,345,345345,56456";
var splitRowObject = rowObject.split(',');
if(splitRowObject.length > 0)
alert(splitRowObject[0]);
alert() is calling objects toString() method, so you don't know the structure of the object. It is a good idea to use console.log instead for logging objects as in modern browsers it will allow you to explore structure of the object in the console window.
One of the solutions you can do without knowing the structure is:
var firstInteger = +rowObject.toString().split(',')[0] // 435
That works if rowObject is string, array or everything else :).
EDIT: Putting + before the string will try to convert it to a number.
Have you tried rowObject[0]?
The fact that the alert shows 435,345,345345,56456 doesn't mean that the object is string, it could be Object and Array as well as their toString method implemented to display it in such way. For example the native array is also looks like that when alerting or converting to string, so you need to call toString method at first then split it by comma:
var firstInt = rowObject.toString().split(',')[0];

Categories

Resources