How to create object inside object in JS [closed] - javascript

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 3 years ago.
Improve this question
I want to create an object inside an object in JavaScript like this:
{
"test": {
{},
{}
}
}
In more detail, I want to create a new object if "test" doesn't exist in the object, and create a new object in the same location as the old one if "test" exists.
Please help if this is possible.

You mean, like:
theObject["test"] = {};
this?
It's just an assignment, mate!

You need to give a key in every object you create in JavaScript.
E.g.:
var obj = {
objInside: {}
}
you can read more about object literals.

Related

How to "push" multiple values on one key in JavaScript Map [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 6 years ago.
Improve this question
Hi everyone i have the following problem. I have Map() that holds keys - System names, and values - nested Map() with key/value pairs component name/sub component name. I want to store multiple sub component names on one component name in the nested map. I try something like this when i get the same systemName and componentName in my code, but it didn't work.
mapName.get(systemName).get(componentName).set(subcomponentName, value);
You can extend Map to provide automatic nesting:
class NestedMap extends Map {
get(key) {
if(!this.has(key))
this.set(key, new NestedMap);
return super.get(key);
}
}
//
let myMap = new NestedMap;
myMap.get('systemName').get('componentName').set('subcomponentName', 'xyz');
console.log(myMap.get('systemName').get('componentName').get('subcomponentName'))

How to use `this` like an object and get its variables/functions by a string? [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 6 years ago.
Improve this question
I've got an object.
function Obj()
{
}
Obj.prototype.doSomething = function(thing)
{
this["do" + thing]();
}
Obj.prototype.doAlert = function()
{
alert("Alert!");
}
var obj = new Obj();
obj.doSomething("Alert");
This is just a shortened down version of my object, and is a lot bigger.
What I would like to do is that if you pass in 'Alert' it will run this.doAlert(); and if I pass in 'Homework' it will run this.doHomework();
Obviously, in this case, it is stupid to do it like this, but my final project is going to be completely different.
It works fine with window like this:
window["do" + thing]();
but I don't want it to be a global function, but to be part of obj.
Does anyone have an idea how I'd go about doing this?
Thanks in advance!
It turns out that when you get the function through this['functionName'], this is not bound to it.
This means you cannot use this.foo inside any function called like above.
To fix this, I used the following code:
this["do" + thing].bind(this)();
instead of this["do" + thing]();
JSFiddle: https://jsfiddle.net/auk1f8ua/

Javascript Array to special Object [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 6 years ago.
Improve this question
I need for an Plugin an Object structure like this:
{
"Beer":145.53,
"Apple (red)":7.33,
"Apple (green)":0.03
}
Don't know how to write it that this shine.
I got the all values already so how to set it up like this?
Use this notation to make objects with those properties:
var obj = {};
obj["Beer"] = 145;
obj["Apple (red)"] = 7.5;
....

Remove an array-Javascript [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 8 years ago.
Improve this question
I have an array like this:
var animals=["cat","dog","snake","rhino"];
But sometimes I have to delete this array(remove it from the dom).
I have tried animals.remove; and $(animals).remove() and animals.remove() but none of them did the trick.Any ideas?
var animals=["cat","dog","snake","rhino"];
then to clear it do:
animals=[];
or
animals.length=0;
or
while(animals.length > 0) {
animals.pop();
}
Just assign the animals array to a value undefined and the array data will be dereferenced and garbage collected.
Donot try to call delete operator that is explicit removal.
animals = undefined
OR
animals = void 0
Thanks
Just Clear The Array
Using This 2 Methods
animals.length =0
animals=[];

Javascript declaration [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
Hi this is a simple question. I was wondering is there any different when you declare something like this. Thanks
selectedData[key](val)
and
selectedData[key] = val
This line selectedData[key](val) is not a declaration, it's calling the function that is stored under the key key in the object selectedData and it's passing the parameter val to that function.
The other line selectedData[key] = val is assigning the value val to the key key in the object selectedData.
In the first case, you're calling whatever is in selectedData[key] as a function with val as an argument while in the second one you're assigning it.

Categories

Resources