How to add a new property in object in javascript [duplicate] - javascript

This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 6 years ago.
I have one obj like
var obj = {
name : "Jenny",
roll : "1000"
}
Now I want to add a new property in obj called grade.
after that my object looks like
var obj = {
name : "Jenny",
roll : "1000",
grade :"7"
}
I am not getting any method from obj obj[op]=value. How to achive that.

It is an object so you can not do obj[op] = value
Instead you can do obj.op = value
In this case, obj.grade = 7 will serve the purpose.

You can do this.
Object.defineProperty(obj, 'grade', {
value: '7'
});

Related

How do i access an object's key but i want to pass the key as a variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
for example:
object = {
name: "Mike",
age: 15
}
object.age // returns 15 right?
But I want to pass the 'age' key as a variable name:
object = {
name: "Mike",
age: 15
}
const age = 10.toString()
How do I get the result of something like this:
object.`${age}`
Try
object["age"]
or
var propName='age'
object[propName]
You can simply do this by
object[age]
Properties can be accessed by either . (dot notation) or via brackets []
that is:
object[key_name]
// where key_name is a variable
object["some_key"] // {"some_key":"value"} or {some_value:"value"}
object[19] // {19:"some_value"}
You can use object destructuring like;
var object = { name: "Mike"
, age : 15
},
{age} = object;
console.log(age);

JSON Object key value pair function [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 3 years ago.
Why i am unable to pass "abc" to someKey
function convertToKeyValuePair(someKey,someValue){
var map = {someKey : someValue};
return JSON.stringify(map);
}
print(convertToKeyValuePair("abc","xyz"));
O/P = {"someKey":"sdfdf"}
Expected O/P = {"abc":"xyz"}
As you’re passing key dynamically so you’ve to use bracket around it like:
{ [someKey]: someValue }
You want dynamic keys.
let key = 'yo';
let obj = {key: 0}; // creates {"key": 0}
let objDynamic = {[key]: 0}; // creates {"yo": 0};
console.log(obj);
console.log(objDynamic);
As answered by the comment of #junvar, passing keys to objects without quotes is syntactic sugar and both of the following examples will give the same result:
{ "someVar": someValue }
{ someVar: someValue }
To use the value of a variable as a key you have to use square brackets as in:
{ [someVar]: someValue }

How to use an object key in another object's path? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 5 years ago.
I have the following objects:
var obj1 {
key1: "path"
};
var obj2 {
path: "done"
};
I want to get "done" (obj2.path) in the scope of obj1 key1.
So the key "path" shall not be reached by obj2.path, but by obj1.key1.
Something like: obj2.(obj1.key1)?
i hope you can understand, sorry for my english :)
You can try using the [] notation to access property, which derived from obj1 key.
var obj1= {
key1: "path"
};
var obj2= {
path: "done"
};
console.log(obj2[obj1.key1]);

Create a JSON field according to the value of a variable [duplicate]

This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 6 years ago.
Supposing we have JSON data who came as :
msg = {
fieldName: fieldA
};
msg = {
fieldName: fieldB
};
I can receive multiple of this message, but everytime i have a different field, i want it to add in the 'data' json object, as i can access by using this :
data.fieldA
data.fieldB
By which way can i perform this ?
To access a property of an object when the name of the property is something your code figures out at runtime:
var propertyName = someCode();
var propertyValue = someObject[propertyName];
To create an object with an object initializer and a computed property name, you can do this in modern (ES2015) JavaScript:
var propertyName = someCode();
var someObject = {
[propertyName]: someValue
};
Prior to ES2015, you would do this:
var propertyName = someCode();
var someObject = {};
someObject[propertyName] = someValue;

in javascript is it possible to construct an object literal with expressions evaluating to strings for property names? [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 8 years ago.
i.e. is it possible to do this:
var fruit = "banana";
var x = {
"app" + "le" : 5, // "apple" : 5
function(){return "orange"} : 8, // "orange" : 8
"" + fruit : 3 // "banana" : 3
};
No, you can't, you need to feed it after the first initialization :
var myKeyName = "bar";
x[myKeyName] = "foo";
You need to declare the empty object and build the strings after.
An object literal expects valid strings for its names
If you don't run the function for 'orange' as well as define it,
the name you want to be 'orange' will be the string of the entire function instead.
var fruit = "banana";
var x = {};
x["app" + "le"]=5;
x[(function(){return "orange"})()]=8;
x[fruit]=3;
/* returns x={
apple: 5,
banana: 3,
orange: 8
}

Categories

Resources