How do I add to a JavaScript object [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Adding members to an existing object
Lets say you have the following object:
var object = {
name: "Shawn"
};
I want to know if there is a function so I can add a new "section" to this object.
Like this for example:
object.add('age',14);
To turn the above object to:
var object = {
name: "Shawn",
age: 14
}
If you ask, "What have I tried". My answer is: "Nothing, i wouldnt be asking if I knew how to do this". I think it should be possible to do. But I just don't know how I will do it.
I looked at w3schools and don't recall seeing a built in function for this. Thanks you.

You can add function properties and methods by using the dot operator .:
obj.age = 24;
This is also equivalent:
obj['age'] = 24;

Use array syntax:
object['age'] = 14;
Or if the property name does not need to be dynamic:
object.age = 14;

var obj = {
name: "Shawn"
};
obj.age=14;
alert(obj.age)

It is as simple as:
var object = {
name: "Shawn"
};
object.age = 14;

var object = {
name: "Shawn"
};
object.age = 14;

Related

What is writing { } in Javascript? [duplicate]

This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
Syntax: const {} = variableName, can anyone explain or point me into the right direction [duplicate]
(1 answer)
Closed 4 years ago.
On this page (https://nodejs.org/api/modules.html), I found this writing: { }.
const { PI } = Math;
Does it have a particular name, so that I can get more information about it, and especially what does it produce?
Thanks in advance. :D
This is called "destructuring assignment". You can think of it as being equivalent to:
const PI = Math.PI;
…but a little more compact. It really shines when being used to pluck multiple properties off an object:
const { foo, bar, baz } = require('quux').util;
You can also destructure arrays using [ ]:
const [ first, second, third ] = array;
The curly brackets in javascript typically represent an object, but in this case, it is a "destructuring assignment". For example:
const obj = { value: 'hello world' };
const {value} = obj;
console.log(value); // outputs: hello world

javascript : get a global variable from its name as string (reflection) [duplicate]

This question already has answers here:
How to find JavaScript variable by its name
(7 answers)
Closed 7 years ago.
I saw a lot of answer to call a method from a given object using a string, but no one to get the object itself.
I would like something like that
var a
var b
var c
function getObject(objectAsString)
{
return getMyObject(objectAsString);
}
then if I write
var obj=getObject("a")
my result is obj=a
Is there a function "getMyObject"?
Thanks
See following code
<script>
//in one script
var GlobalvarName = 500;
alert(window["GlobalvarName"]); //alert is : 500
</script>
you could do:
var hello = 'Hello World';
this['hello'];
I wouldn't make those variables so global though. here's why
Instead put them inside an object like:
var obj = {
a: 'Hello',
b: 'World'
}
console.log(obj['a'], obj['b']);

Can you evaluate a property name within a JS object? [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
I know that you can evaluate the value of a property inside of a JS object, like the following:
let object = {
value: 5+5
};
I am wondering if there is any possible way to evaluate the name of an attribute with JS, i.e. achieve the following:
let object;
object[5+5].value = "ten";
As something like:
let object = {
5+5: "ten"
};
Yes in ES2015, no in ES5, but first let's clear one thing up: that's JavaScript, not JSON.
In ES2015 (formerly known as ES6):
var something = "foo";
var object = {
[something]: "bar";
};
alert(object.foo); // "bar"
Inside the [ ] can be any expression you like. The value returned is coerced to a string. That means you can have hours of fun with stuff like
var counter = function() {
var counter = 1;
return function() {
return counter++;
};
};
var object = {
["property" + counter()]: "first property",
["property" + counter()]: "second property"
};
alert(object.property2); // "second property"
JSON is a serialization format inspired by JavaScript object initializer syntax. There is definitely no way to do anything like that in JSON.
Sure. Try this:
'use strict';
let object = {
[5+5]: "ten"
};
console.log(object); // Object {10: "ten"}

Variable variables in JavaScript? [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 7 months ago.
In PHP I can mess around with variable variables and I'm wondering if I can do the same in JavaScript.
I want to create a new object with a property which's name is based on the value of a variable.
if ( obj.name === 'foo' ) {
var data = { foo: value };
}
if ( obj.name === 'bar' ) {
var data = { bar: value };
}
Is there a shorter way of doing this without using eval()? Something like:
var data = { obj.name: value };
Try this:
var data = {};
data[obj.name] = value;
You can read some more about js objects Here.
Objects in JavaScript are simply hash maps. You can access members by indexing with their names. For your problem you can use
var data = {};
data[obj.name] = value;
I've used this to implement a dynamic dispatch mechanism for arithmetic operations on different numerical types as described here.

Difference between "{}" and "new Object()" [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
creating objects - new object or object literal notation?
What exactly is the difference between the following:
var myData = new Object();
myData["name"] = "ATOzTOA";
myData["site"] = "atoztoa";
and
var myData = {};
myData["name"] = "ATOzTOA";
myData["site"] = "atoztoa";
Update
What I got is this...
var myData = {
"name" : "ATOzTOA",
"site" : "atoztoa",
};
is a shortcut to
var myData = new Object({
"name" : "ATOzTOA",
"site" : "atoztoa",
});
Am I right?
There is no difference (technically). {} is just a shortcut for new Object().
However, if you assign an object literal, you may directly form a new object with multiple properties.
var myData = {
name: 'ATOzTOA',
size: 'atoztoa'
};
..which might feel more convenient. Also, it reduces the access on the object and is ultimately faster. But that is about microoptimizations. More important is that its a lot less to type.
Nothing. {} just a short hand for new Object()
Its same logic as your full name is 'Mark Zuckerberg' and people call you ' Hi Mark'
No difference in my view , Both are initializing the object. nothing else , it is a shortcut.

Categories