How to get a child key value in Firebase Realtime Database [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm new with Firebase and I'm currently using JavaScript SDK to integrate my web application with Firebase.
My question is: How to get value within the red box that I drew on the image?
I am using
snapshot.key
to access the key valuebut it returns the generated key (blue box) to me which is not the one I wanted.

Based on your comments, it is not 100% clear what you are looking for. If you know that you have a sub-node with the key speechtext under the recording parent node with key LRC2o.... you don't need to query for getting the key of this sub-node (since you know it).
If, on the other hand, you want to iterate on all the keys of the sub-nodes of the recording node, do as follows (based on your code):
var dbRecording = firebase.database().ref("recordings/");
dbRecording.once("value", function(snapshot2) {
if (snapshot2.exists()) {
snapshot2.forEach(function(value) {
var childObject = value.val();
Object.keys(childObject).forEach(e => console.log(`key = ${e}`));
});
}
});
If you want the keys and the values, do as follows:
var dbRecording = firebase.database().ref("recordings/");
dbRecording.once("value", function(snapshot2) {
if (snapshot2.exists()) {
snapshot2.forEach(function(value) {
var childObject = value.val();
Object.keys(childObject).forEach(e => console.log(`key = ${e} value = ${childObject[e]}`));
});
}
});

Related

Converting a javascript string into an existing javascript variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am stuck with this problem. I have a function to accept the path and also the same time I have a variable that I want to condition with.
Here is my problem: I want to make a string type that will act as an access to my variable.
In my situation, I have a roles.operation variable which I want to access it dynamically.
The roles variable has an array with the values of:
roles.operations = ['document','article','document-type'];
with this variable I want this to be access dynamically.
Here is what I've tried, which in replacePath i have the value of document-type:
export const createVariable = (roles,path) => {
const replacePath = path.replace(/-/g,"_");
const finalPath = window[`roles.operations.${replacePath}`];
console.log(finalPath);
}
this gives me undefined.
Try this way:
const finalPath = window['roles']['operations'][replacePath];
or
const finalPath = window.roles.operations[replacePath];

saving and loading variables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I made a game and its a type of game which takes hours to complete. Its a browser game. How can I store variables ( need to store 20-40 variables ) and load them using cookies ? ( so that I can access them even if browser is closed and opened again ). Please, I need help.
You would be better off using localStorage and not cookies. Pretty simple if you just do something like
//The defaults the user starts with
var _defaults = {
level : 1,
userName : null,
life : 100
};
//getting previous values from storage
var savedDetails = localStorage.settings ? JSON.parse(localStorage.settings) : {};
var settings = $.extend({},_defaults, savedDetails);
if(!settings.userName) {
//set username and save
settings.userName = window.prompt("name");
} else {
//username is there so say hi
console.log("Welcome back " + settings.userName);
}
//Save this back to the local storage since we made changes
localStorage.settings = JSON.stringify(settings);

Get a list of games and their properties from JSON [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to access a list of Casino Games and their properties from a JSON object in order to get them to display on the website. This is where I am so far.
var x = $.getJSON("http://api.bosurl.net/V1/Progressive.asmx/GetProgressiveGames?format=json&callback=?", ProcessProgressiveGames);
console.log(x);
function ProcessProgressiveGames(progressiveGames) {
console.log(progressiveGames.d.Games[0].GameName);
}
What's there best way to do this. If you check your console, you'll see that var x contains the object with the game data.
See also: http://pasteboard.co/kXb1Voq.png
Related fiddle: http://jsfiddle.net/emporio/vz24dtm8/5/
This question is unique because it requires accessing unique properties. The answers also provide a multitude of ways to retrieve the data.
DEMO
JS
var Games;
var x = $.getJSON("http://api.bosurl.net/V1/Progressive.asmx/GetProgressiveGames?format=json&callback=?", function (data) {
Games = data.d;
}).done(function () {
document.getElementById('choice').innerHTML=ProcessProgressiveGames(Games);
});
function ProcessProgressiveGames(progressiveGames) {
return progressiveGames.Games[0].GameName;
}
$.getJSON function returns promise and the proper way to handle data returned from this address is to set second argument - function to handle data
$.getJSON("http://api.bosurl.net/V1/Progressive.asmx/GetProgressiveGames?format=json&callback=?", ProcessProgressiveGames);
function ProcessProgressiveGames(progressiveGames) {
// Note that progressiveGames has a property 'd' containing the data we're interested in.
console.log(progressiveGames);
document.getElementById('choice').innerHTML = ProcessProgressiveGames(x);
return progressiveGames.d.Games[0].GameName;
}
http://jsfiddle.net/vz24dtm8/7/
You can try like this
var x = $.getJSON("http://api.bosurl.net/V1/Progressive.asmx/GetProgressiveGames?format=json&callback=?", ProcessProgressiveGames);
http://jsfiddle.net/vz24dtm8/8/
function ProcessProgressiveGames(data) {
// Note that progressiveGames has a property 'd' containing the data we're interested in.
return document.getElementById('choice').innerHTML=data.d.Games[0].GameName;
}
<pre>Use callback function to get the json value and pass the parameter in callback function</pre>
http://jsfiddle.net/rajen_ranjith/vz24dtm8/13/

Need guidance to make small utility based on user inputs [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I want to implement a small code I am not able to understand from where to start. I want to create a text field if user enter some text into text field I want to detect category of this text. For example if user enter "How to create an app for iOs".. utlity should detect this category as information technology. another example "good hotels in Singapore" this utility should detect treat this category as Travel....
My idea to create library with words.
var categories = new Object();
categories = {
'IT' : {'iOS', 'Android'},
'Travel' : {'USA', 'London', 'Singapore'}
};
var text = 'How to create an app for iOs';
var spitter = text.split(' ');
for (var i = 0; i < spitter.length; i++) {
var word = spitter[i];
for (var categoryKey in categories) {
for (var categoryKeyWord in categories[categoryKey]) {
var regExp = new RegExp(categories[categoryKey][categoryKeyWord], i);
if (regEpx.match(word) {
//Your logic
}
}
}
}

Comparing string to a list in javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a list of car models that are in the list as Chevrolet,Ford,BMW, so I enacted this code on them.
cars.getMakes() returns a list with the names mentioned above and they are formatted as such.
cars.getAllModels() returns a similar list with just the car model names.
make is a single string that is either a model name or make name. Depends on users input.
I want to test to see if what the user put in actually exists in my predetermined lists.
if it does; true. If not; False.
function makeCheck(make) {
var models = cars.getAllModels();
var makes = cars.getMakes();
if (make == makes[make]) {
return true;
} else {
return false;
}
}
If you want to check that the variable make passed to the function is in the list of valid makes:
function makeCheck(make) {
var makes = cars.getMakes();
return (makes.indexOf(make) != -1);
}

Categories

Resources