Find nested array-object from rethinkdb in feathers JS - javascript

I have a data set like follows-
[{
"allowedusers": ["paul#abc.com"],
"id": "1"
},{
"allowedusers": ["kmahera#abc.com","rbajaniya#abc.com"],
"id": "2"
},{
"allowedusers": ["whatever#abc.com","rbajaniya#abc.com"],
"id": "3"
}]
and I have a Query like this -
http://localhost:3030/flowz$limit=5&allowedusers[$in[]=rbajaniya#abc.com&$skip=0&$select[]=id&$select[]=alloweduser.
But I am not getting all the objects that contain rbajaniya#abc.com . How can I craft my query to get this. I want to get id=2 and id=3 in response .

$in is to check a single value in the database against a list of possible values. What you are looking for is the other way around which can be done through the feathers-rethinkdb specific $contains operator:
http://localhost:3030/flowz?$limit=5&allowedusers[$contains]=rbajaniya#abc.com&$skip=0&$select[]=id&$select[]=alloweduser

Related

Access JSON data using a value from localStorage

After searching online and not finding anything, I decided to post here.
What I have so far:
JSON
I have a JSON file, with several list of words that I need to retrieve according to user input. They look something like this:
{
"length": 10,
"targetWords": {
"SOME_NAME": [
{
"words": [
"xyz",
"xyz",
"xyz",
"xyz",
"xyz",
"xyz",
"xyz",
"xyz",
"xyz",
"xyz"
]
},
{
"text": "Some example text",
"letter": "X",
}
],
}
localStorage input
I ask the user to choose an option and "record" that answer in a variable. Let's call it "userInput".
userInput = localStorage.getItem("something")
I get the JSON and pass it to a variable (I'm using Phaser Framework)
TARGET_SOUNDS_DATA = this.game.cache.getJSON('targetSounds')
What I want now is to access the values inside it. I can do it if I go about it like this:
TARGET_SOUNDS_WORDS_ARRAY = TARGET_SOUNDS_DATA.targetWords.SOME_NAME[0].words
But unable to do so if I use the user answer/input like this:
TARGET_SOUNDS_WORDS_ARRAY =TARGET_SOUNDS_DATA.targetWords.userInput[0].words
This way I only get undefined
So I know I'm doing something wrong but what?
Any help/hints are welcomed! Thanks for your time!
Cheers,
J
You can use square brackets to select a property with a variable
TARGET_SOUNDS_WORDS_ARRAY = TARGET_SOUNDS_DATA.targetWords[userInput][0].words
You have to parse it and then to get result based on userInput access it like this
TARGET_SOUNDS_WORDS_ARRAY =TARGET_SOUNDS_DATA.targetWords[userInput][0].words
As I understand, and maybe I'm wrong, localStorage is key/value storage only. You'll have to convert the json to a string, store it, then retrieve it and convert it back to an object when you want to use it.

AngularJS JSON Check Object

I am currently using Angular to read in a json file and output it onto a table.
Because some of the objects are a little different, I want to make a check to see if job.text exists.
[
{
"job": {
"href": "www.google.com",
"text": "Google"
},
"api": "Some Text Here"
},
{
"job": "Yahoo",
"api": "More text here"
}
]
If job.text exists, then display job.text
else display job
Here is my html from angular but only displaying objects with job.text, otherwise it returns nothing.Is there a simple way to write a check statement to make sure I can display both types of objects?
<td><a ng-href="{{item.job.href}}" target="_blank">{{item.job.text}}</a></td>
Use a ternary:
{{item.job.text ? item.job.text : item.job}}
You should try and normalize your data struct a bit - seems odd that job may contain an object or a simple text field.

Need an algorithm to convert string to JSON with javascript

I'm calling an api for the history of an ID which returns a string object that looks like this:
09304790130000--09304790090000
09304790130000--09304790120000
09304790090000--09304790010000
09304790120000--09304790020000
09304790120000--09304790030000
09304790120000--09304790110000
09304790110000--09304790050000
09304790010000--042322003
09304790020000--042322002
09304790030000--042322001
09304790050000--042322004
I could do so much more with it if I could figure out how to use JavaScript to convert it to JSON so it would look like this:
{
"name": "09304790130000",
"children": [{
"name": "09304790090000",
"children": [{
"name": "09304790010000",
"children": [{
"name": "04 2322-003"
}]
}]
}, {
"name": "09304790120000",
"children": [{
"name": "09304790020000",
"children": [{
"name": "04 2322-002"
}]
}, {
"name": "09304790030000",
"children": [{
"name": "04 2322-001"
}]
}, {
"name": "09304790110000",
"children": [{
"name": "09304790050000",
"children": [{
"name": "04 2322-004"
}]
}]
}]
}]
}
Is there an algorithm I can use that can construct the object I need regardless of how complicated the "tree" becomes?
EDIT for clarity:
The "--" in the string represents the relationship of the ID's. The left ID is the parent of the ID right of the dashes. So the ID that I feed the api, "09304790130000" has two children, each could have more children until they reach the current 9-digit ID.
What you have here is an input that is in a custom format. What you need to handle it is a regular expression. (Although your format might be simple enough that a full on regex function is not required so much as splitting on separators?) You need to do is break the input string up and loop over the components and put those into your desired data structure (which sounds like it would be some kind of tree). The high level pseudo-code would be something like:
Take line of input.
Break on "--".
Create root node from the left side if the tree is empty, otherwise just find the existing node.
Add child from right side to the parent.
Getting it into the JSON format you want may require also writing a function that iterates over the tree and writes a string in that format... although if you are using existing libraries and data types this probably already exists.
EDIT: To expand on the last bit, to get the format you want would mean a Pre-order traversal of the tree. At each step you just add the formatting and name to the JSON String. One of these libraries should have the capabilities you need, although obviously you can write a tree data structure and traversal function yourself if you need to.

Binding and accessing nested elements using Rivets.js and Backbone deep model

We are using an open source FormBuilder client side component and extending it to fit our requirements. Formbuilder is written using Backbone Deep model with nested data and for binding, it use Rivets.js.
Here Formbuilder is on GitHub: https://github.com/dobtco/formbuilder and here backbone deep model at GitHub: https://github.com/powmedia/backbone-deep-model
Now we are using nested elements in view, which are nested in structure as in following JSON:
{
"fields": [{
"label": "Untitled",
"field_type": "checkboxes",
"required": true,
"field_options": {
"options": [{
"label": "test",
"checked": false
}, {
"label": "",
"checked": false
}]
},
"rules_data": {
"rules": [{
"ruleId": "rule6",
"criterias": [{
"condition": "if",
"responseTo": "",
"userOption": ""
}],
"branchTo": [{
"branch": "test"
}, {
"branch": ""
}, {
"branch": ""
}]
}]
},
"cid": "c2"
}]
}
Here there is array of rules, then rules have at every index have more data with in which one is branchTo, now branchTo is also an indexed array. In Rivets.js we can bind something using Rivets.js . or : operator. In case of properties, we can use : but we are unable to access elements inside nested indexed array.
So is it possible to access and bind elements in Rivets while using nexted indexed elements? If yes, then how can we do so? Or is there better and simpler way to accomplish same goal? I am beginner in Backbone as well as Rivets, and I am not sure if this is the right way.
If I understand rivetsjs correctly the : is just an example of an adapter you could have ^ as an adapter separator if you wish. This means you can also have both and nest adapters. having the : search the first level and then the ^ to search 1 level deeper.
You can also build a more adaptive adapter that can get objects deeper. Example in the following stackoverflow answer. You can also see some other methods of getting deeper nested objects here:
How to bind deeper than one level with rivets.js
Hope this solves your problems

parsing JSON object with Jquery problem

Hi I am trying to parse the following bit of json with Jquery so far I can get everything out of the results that I want apart from one crucial piece of information the performance tags.
Each json result is wrapped in an event tag and then within this there is info like time and date etc formatted in the following way
"location": {
"lng": -0.1187418,
"city": "London, UK",
"lat": 51.4681089
},
"start": {
"time": "19:30:00",
"datetime":"2010-02-16T19:30:00+0000",
"date": "2010-02-16"
},
I have managed to loop through this and parse it to html. However there is one set of tags for 'performance' that are formatted differently.
"performance": [{
{
"artist": {
"uri": "http://www.songkick.com/artists/288696-vampire-weekend",
"displayName": "Vampire Weekend",
"id": 288696,
"identifier": [{"mbid": "af37c51c-0790-4a29-b995-456f98a6b8c9"}]
}
"displayName": "Vampire Weekend",
"billingIndex": 1,
"id": 5380281,
"billing": "headline"
}
}],
now in my for loop i am running the following code which displays the performance information in the console.
var events = data.resultsPage.results.event;
for (var i = 0, l = events.length; i < l; i++) {
console.log(events[i].performance); }
However when i try to go into the structure like i have been with the other elements I get returned undefined i.e
console.log(events[i].performance.displayName);
Do I have to do this in a different way because of the use of the [ ] brackets in the performance tag in the Json?
Thanks in advance
Assuming that what you posted is not exactly what your JSON looks like (because what's posted has a syntax error), the "performance" attribute is an array of objects. To get at the "displayName", therefore, you'd need to know which element of the "performance" array you wanted. You'd then access it by index.
console.log(events[i].performance[j].displayName);
(assuming you looped through the "performance" array with the variable "j".)
Try to validate your returned JSON object here, I guess there is some issue with the JSON output..

Categories

Resources