I have some JSON data where I define presets with a unique identifier ID, (you could call it a primary key). Besides ID there are a bunch of other properties defined, including somethingelse. I want to extract the somethingelse property of a particular record, identified by its ID. I tried this:
function getSomethingElse(id) {
return presets[id].somethingelse;
}
But obviously the above code will only access the preset with array position id. I'm trying to access the preset where the actual ID is id.
This might be a dumb question, but I haven't been able to find any example of what I'm trying to do!
If ID is used as a primary key then I'd suggest converting your preset data to a hashmap instead of an an array.
var presets= {};
presets["guid1"] = "hello"
presets["guid2"] = "world"
This should work exactly as your posted code intends.
Assuming $scope.presets is an array, You need to iterate your objects like
$scope.loadPreset(id) {
angular.forEach($scope.presets, function(value, key) {
if(value.id == id){
$scope.somethingelse = value.somethingelse;
}
});
}
Related
The firebase documentation says this about updating in nested objects: https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects
My structure
let ref = db.collections('projects').doc('blabla')
ref.set({
objOfObjects: {}
})
I would like to add an object(value) with a randomly generated key to objOfObjects, much like the regular add function. I can't tie the key to any of the values of the object. Is there a way to add to objOfObjects or do I have to restructure my data?
There is no built-in function add a field with a randomly generated name to a document. But you can generate the random name yourself and add it. You can even use collection.add() to generate the random name, since (as Doug says here) that doesn't actually create a document yet.
var newId = store.collection('projects').doc().id;
let ref = db.collections('projects').doc('blabla');
var updates = {};
updates[id] = { ... }
ref.update(updates)
The tricks used here:
Get a new unique id, as explained before.
Use [] notation to use the value of that id as the property name.
Call update() to update a document, instead of replacing it.
Before marking this as a duplicate, i've spent a lot of time looking through similar question and most of the answers did not solves my situation.
i have a huge list of items as objects by IDs. like this, in a Map (userDB)
{
"15321":{name:"name1",status:"status1"},modules:{...},
"15322":{name:"name1",status:"status1"},modules:{...},
"15323":{name:"name1",status:"status1"},modules:{...}
}
now i need to make an operation in which i need all these IDs, in that case, the key names of every item on it. i need to get these "15321","15322" etc.
more specifically i wanted something that i could fetch in something like
userDB.forEach( u => {
//something here to get u's key
})
i've tried Object.keys(), but it will return a list of IDs as an object
{"15321","15322"...} in which i still cant grab the ID string
i've tried for (i in Object.keys(userDB)) too, no successs
i double-checked for silly syntax errors and everything of the sort.
Things that will be nice to get in mind to answer this:
dont try to show me a new way of storing stuff, it is already stored so you will not be of help
the result SHOULD be the ID as a string, the name of the key.
dont ask "why i want to make this". just answer and dont try to change this scenario. because this is what i've seen in most of the other similar questions and it is what makes me walk in circles every time.
TL;DR. i just want to get the parent key names of the object im currently processing
Object.keys(obj) will return an array.
But in your data there is another key modules except IDs.
So you can use this :
var keys = Object.keys(data);
keys.pop();
console.log(keys); // ["15321", "15322", "15323" ...]
You might be confused. Object.keys(obj) returns an array. In your case it looks like this: ["15321", "15322", "15323"]. You could iterate through that array like so and you'll have both the key and the object and you'll be able to do with them whatever you want. Below is a for loop that attaches the key to the object as a key named 'key'.
var keys = Object.keys(myObject);
for(var i = 0; i < keys.length; i++){
var key = keys[i]; // this is your key
var obj = myObject[key]; // this is the key's value
obj.key = key;
}
EDIT
In javascript an Array is also an object, but the 'keys' so to speak are usually numbers instead of strings. So an array that looks like this:
["Hello", "there"] is essentially represented like this: { 0 : "Hello", 1 : "there" }
When using for-in on an array, it'll loop through the keys, but those keys will be 0, 1, 2... instead of the items themselves.
I have an intention to set a field value of an object like this
$scope[nameOfField]=value;
which works if nameOfField is just field name.
However, if I define in $scope object "subObject":
$scope.subObject={};
var nameOfField='subObject.someSubField';
$scope[nameOfField]=12345;
this does not work. Apparently I can not address directly sub-object fields like this. I however do need to use nameOfField approach with sub-object fields, and appreciate hints how to make it work. I can not predict if subObject will be featured in nameOfField - it can be both name field and subObject.someSubField.
EDIT: Difference with the question Accessing nested JavaScript objects with string key is that I not only need to access value of object but modify it.
Well your current code would result into
$scope['subObject.someSubField']=12345;
which would be a syntax error. Correct would be
$scope[nameOfField1][nameOfField2]=12345;
So you need a function to archieve this. Here an example (obviously this has to be extended to support more then just the 2 levels):
var scope = {};
function setValue(scopeString, val){
var match = /(\w+)\.(\w+)/.exec(scopeString);
if(!scope[match[1]]) //create if needed
scope[match[1]] = {};
scope[match[1]][match[2]] = val;
}
function getValue(scopeString){
var match = /(\w+)\.(\w+)/.exec(scopeString);
return scope[match[1]][match[2]];
}
setValue('lvl1.lvl2', 1);
console.log(getValue('lvl1.lvl2'));
Here's a quite simple question but I have not been able to find an answer anywhere about this, so hopefully I can get the answer here.
I want to iterate through the keys in my database using:
for (var key in X) {
//X is the main object I need
}
However, in order to do this I need the actual object of the database, not the reference to the database. Because when I iterate over the reference I get a lot of nonsense properties. So how do I get the object containing all the keys that I add personally? Hopefully my question makes sense.
This can be done this way:
var value;
myDataRef.on('value', function(snapshot) {
value = snapshot.val();
})
I have a list of strings & I want to check that a specific string is not found in it.
I use javascript, so I was wondering if a Trie or binary search method would be better. Is there something pre-implemented that can be used for something like this ?
Here is the object :
var TheObject = { "TheItemId": Index, "TheItemText": NewItem };
I have a list of this objects, when I insert a new Item, I want to be sure "TheItemText" doesn't contain any similar texts.
JavaScript objects can be treated as hash-maps. So you would keep an object var strings = {}. Whenever you add object to the list, you would also add property to the strings object: strings[text] = true. Then you can easily check whether the text has been added before with if (strings[text]) {. Downside of this solution is that you must track changes in two collections (your list and strings). Maybe you do not need list at all, then you can use strings[NewItem] = { 'TheItemId': Index, 'TheItemText': NewItem }. Changes to 'TheItemText' and the property name must be still coordinated.
Wouldn't something like this work?
typeof TheList.TheItemText === 'undefined'