I am not really sure how to get data out of this nested object with Mustache.js. I am using the YQL multi query that brings back my data objects nested https://gist.github.com/866247. My problem comes when I am trying to access the nested objects
The data comes back like this where 0 and 1 are part of an array with objects in them. Here is a picture of the tree http://cl.ly/1e1b3O3U233e2I0d3g2f.
query
results
results
0
1
I have tried the template below and I don't get anything back. I believe the problem is that that second "results key" comes back with the array and mustache doesn't know what to do.
"{{#query}}
{{#results}}
{{#results}}
{{#photo}}
{{farm}}
{{#photo}}
{{/results}}
{{/results}}
{{/query}}"
If this makes sense to anyone, how do I access the nested objects in the array if there is no key to them?
I think you should flatten your data into a simpler format to write a template that makes sense, but you can get pretty close to what you want with it as-is.
The one insurmountable issue I saw was that 'results' is inside 'results' resulting in a tag closure mismatch. Doing something like this solves that problem:
data.query.flattened = data.query.results.results;
A functional sample is here.
You can find some more mustache tricks in this article.
This is one of the types of issues with Moustache that Handlebars attempts to address - traversing through objects in the template:
http://handlebars.strobeapp.com/#paths
Where you access it directly as if it's a directory structure. I know this isn't an answer to your question, per-se, but aside from flattening the data it's an alternative solution.
Related
I am using the node.js framework from this tutorial: https://www.w3schools.com/nodejs/nodejs_mongodb.asp
I have a strange problem which i dont understand, i have make a picture for better understanding:
picture from my database and nodejs code
If i just try to update another object in this array it does not work, but if i update the first object it does work, so please does somebody know why, is the mongo client which i use not good or does my query have a error?
First off, code examples shouldn't be shared via an external picture. This should be provided as a code snippet in your question.
Now as to the reason why it isn't working. You don't have an array of objects, you have an array of array of objects.
If you look at the first one in the array of questions, it is an object, but if you look at the second one, you can see that it is an array that includes 1 object in it. You will need to make sure your questions array is an array of objects and not an array of arrays with objects. Then your query will work. See below from your given example.
I'm searching for a way to deal with a list of objects that should have unique IDs in Javascript. I want to be able to add, remove and edit (by their ID) objects that are in this list, loop over the list and be able to store it in a JSON file.
My naive implementation looks like this: [6,{"ID":4,"name":"test"},{"ID":5,"name":"test2"}]
Each time I add an object to the array I give it the number that is in position [0] as an ID and increase the number in position [0].
As I thought about how to edit an element in this kind of data structure (my ideas were using indexOf() or an ordinary for) I started to wonder whether my data structure was really the best way to solve my problem.
My question: Is there a better data structure/way of dealing with objects in JavaScript that fulfills my requirements?
Thanks in advance for the help
This might seem like an odd questions; I know its possible to slice an array, but I was thinking, if I'm calling an array externally via a $http GET (using Angular) like so:
$http.get('/url')
is it possible for me to declare how much of the array I want to retrieve first before making the request, instead of retrieving the whole thing (and therefore saving on performance). I know its possible to do something similar using PHP but wasn't sure to the extent I had in Javascript? Or can I only slice it once the array is declared fully?
The only way to retrieve part of the array is for the server that you are requesting the data from to support that capability. You cannot do that entirely from the client unless the server has that capability.
The usual way that this would be done would be to add query parameters to the URL that specify how much of the data you want and for the server to look at those query parameters and to send only those pieces of the data.
For example:
$http.get('/url?start=0&end=20');
Arrays can only be sliced once they exist and have some data in them so I'm not sure what that part of the question was trying to ask about. Either the client or the server could slice the result array once they had built the initial array.
On our site, we have pages that can be assigned tags, and I'm having issues with figuring out how to handle this with Adobe DTM/Analytics. Each page can have between 1 and n tags being captured in an array. I can see them in the developer console just fine, ex:
> data.page.termIds
< [513, 1787, 115, 4330]
Each element corresponds to the tag ID.
I've been searching online but can't find any answers that really help me. This blog post is close, but it looks like they need to create a separate data element for each element in the array. I also found this question in the adobe forums, but it's similar to the blog post.
Does anyone know if this is even possible? Are we expected to create a block of data elements, with each one assigned to an array index?
Do not bother with the JS Object option unless you want to target a top level js object/variable. So for example foobar is okay but foo['bar'] or foo.bar is not. If you want to target anything "nested", skip JS Object and use Custom Script to check for it and return it, instead. This is because DTM doesn't do a good job of parsing or checking if the parent nodes exist before returning it. This is basically what was already said in the blog link you mentioned.
As for what the Data Element is returning and how to use it...the Data Element itself will have a return value of exactly what you return. So if you return an object, it will be an object. If you return an array, it will be an array. The trick (or caveat, or bug, depending on how you want to look at it) is how you reference or use the Data Element within the tools and rules.
The long story short of it is this: If you intend to use the %data_element_name% syntax (e.g. in any of the built in form fields in the rules, config settings, etc.) then you should only have your Data Element return a string type value (this is basically the issue the forum post link had, and I assume the issue you are running into).
If you want to be able to access it as some other type (e.g. the array, or js object in general), you need to use _satellite.getVar('data_element_name') syntax. The obvious drawback to this is it basically makes using DTM in a "non-coder" way useless in that you cannot use this in any of the built-in fields in rules. You can only use it in contexts where you can write javascript, e.g. Data > Custom type Conditions or in Javascript / 3rd Party Tag containers.
But one question for you is, what are you ultimately using the Data Element for? For example, if the end game here is you are looking to populate a prop or eVar or some other variable with a comma delimited string.. well just return your array as such in your Data Element and then you can use the %data_element_name% syntax.
Capturing the data is one thing and formatting is another.
My question is how do you need to see this data organized in reporting? Capturing the array itself doesn't do much unless you know what format you need it in. Are you hoping to capture these IDs and classify them? Do you want to see the array list by page?
Can you provide more detail on how you'll be reporting on these values?
Long story short : I'd like to treat several javascript associative arrays as a database (where the arrays are tables). The relations could be represented by special fields inside the arrays. I'm not interested in the persistence aspect of a database, I only want to be able to query the arrays with a SQL-like language and retrieve sets of data in the form of associative arrays.
My question : Is there any javascript library that has such features ? Otherwise, is there any library that can at least take care of the SQL-like language part ?
Thanks
I believe the closest thing to what you need is the jLinq library. It can operate with js objects and arrays much in the same way you would do with a database, but in a slightly different way. You don't really write queries, but use methods to construct them. Overall it's way better I think.
Some googling found this: http://ajaxian.com/archives/two-js-solutions-to-run-sql-like-statements-on-arrays-and-objects which seemed interesting.
Can I ask why you want to do this?
I came across this question while searching something sort of related. Wanted to share with you (9 years later, I do realize) that I have the same want/need, often, where the script I'm working on has a lot of cross-referencing to do between various sources of information. I use PowerShell. Enumerating arrays of objects, from within a loop, which is enumerating other objects, is just bad/slow/horrible.
To date, my solution has been to take all my arrays and then make hashtables from them, where the key/name is a property value that is common across all the arrays (e.g. ObjectId (GUID)), and the value is the entire object from the array. With this, while in my loop which is enumerating Array#1, I can check for the presence of this current item in any of the other arrays simply by checking the existence of the key in the corresponding hashtables, and that way there's no enumerating the other array, there's just direct , equal effort access to the correct item in the array (but really coming from the newly built hashtable).
So my arrays are just temporary collection buckets, then everything I do from there uses the hashtables, which are just index/lookup tables.
What I was searching for when I stumbled here, was for solutions to keep track of all the different hashtables in the building/planning phase of my scripts.