I'm having trouble building a choropleth map and the one thing I'm trying to figure out is how to access an array from a json object:
This is what I see in my console for
var datum=topojson.feature(us, us.objects.states)
and I want to specifically capture the name under properties (eg. State name)
console.log(datum.features[2].properties.name)
this should work assuming you want the properties of second object in features array. Also, JSON object is nothing but javascript object you can access the properties like you do in vanilla js
Related
I am trying to return the value under the key 'str' in an Object but I am having trouble accessing the value.
This is what is returned in the console:
Currently I am using a map function to go over the array and just return the _str value like so:
let idx = currentArray.map(function(x) {
return x._id._str;
});
However it is still returning the value as an object. How can I get just the value of the _str key?
Here is the full array without specifying the id field. This is what is returned if you jsut return 'x' in the map function.
You've clarified that the screenshot is of x._id. So to access _str, you'd use x._id[0]._str: The _str property is in the object referenced by the 0 property (the first entry in the array x._id refers to).
Note that in general, _-prefixed properties are meant not to be accessed by code outside the code responsible for the objects in question. You don't seem to be responsible for them, so accessing those properties is likely to make your code rely on undocumented properties that may change in the next "dot" release of whatever lib you're using. It's just convention, but it's a very common convention.
If you right click on the property, most browser consoles offer the ability to copy property path.
Based on this SO post and the docs, it appears that you can probably use x._id.str.
If I understand correctly, you are receiving the str value but it is an object instead of the string literal. In other words, you are getting _str: "598..." instead of "598....". A possible solution would be to use the mongo javascript function to convert the str value to a string.
In your case, I think something like return x._id.str; may work as _id is a MongoID.ObjectID.
I've also linked the documentation below for reference.
https://docs.mongodb.com/manual/reference/method/ObjectId/
Here's a relevant SO answer as well: Convert ObjectID (Mongodb) to String in JavaScript
I think you should write x[_id]._str because _id is one of the array objects.
Javascript objects use a key value system (like a hash, map, or dictionary in other programming languages). The key is referred to as a property and within an object is written like this:
var object = { property01: value01, property02: value02 }
Within objects we can access the value of the property using two access methods.
object.property01
object['property01']
In the example above property01 and property02:
What is the type of the variable holding the property name, and how is it stored in memory?
I'm not sure if I understand your question correctly, but the closest answer I could reference is this:
Objects are sometimes called associative arrays, since each property is associated with a string value that can be used to access it.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
It's a variable, but instead of being attached to the window object, it's attached to another variable.
I am trying to access an array of objects within $scope.
If I console.log($scope) (see image below) it displays the pedigree object that I would like to access. However, if I console.log($scope.pedigree) I get undefined. If I console.log(typeof $scope.pedigree) I get object.
Can someone help me access pedigree?
Pedigree is an array of objects, not a single object. As a result, you need to access the appropriate instance by index to get your object.
var pedigree = $scope.pedigree[0];
There is no "array" type in Javascript. As a result, when you use typeof on your $scope.pedigree property, it will tell you it is of type object, even though happens to be an array of objects.
You can access to scope via jquery or jqlite: $('.element').scope();
Accessing via console.log is not possible, because angular doesn't use global scope for storing scopes. More information here.
I am getting a model deferred object with a structure like this:
How can I find out how many objects are present inside this object (in this case, three). If I use Object.keys(myObject).length, it includes the object observer and other data also like _computeBindings,_bindings etc. I have even tried to use hasOwnProperty while calculating the length but its not giving me desired result.
One way I can figure out is to iterate over the index and get the last index value like this:
can.each(myObject,function(myObject,index){
// Get the last index value and put it into some variable
});
Is there an API for this?
can.Map has a keys function that will give you an Array of the keys in your Map and from that you can get how many Objects by checking that Array's length.
Using a can.List as your data structure would also work. The keys in your data are numeric and you need to check the length, all things that can.List is built for.
Try using myObject.attr('length')
The model _data attribute contains a copy of just the model without the bindings. The easiest way to do this is to use:
Object._data.length
I have made a class in javascript with an object that contains both variables and functions. I instantiate the object within one .html page, but when I change to another .html page, the object seems to be 'empty'.
How can I pass the object between one page to another?
You can have a look at Web Storage. This won't allow you to store a full JavaScript object, however you will be able to store as many string name-value pairs as you want. Either permanently (between browser restarts) or as part of a session.
Use localStorage, check this lib:
https://github.com/marcuswestin/store.js
You can do something like:
store.set('foo', originalObject);
and then...
var restoredObject = store.get('foo');
Removing stored objects:
store.remove('foo');
Fork "jQuery-like" syntax:
http://pastebin.com/x3KpKyr1