How to Set Object Property (Of Object Property Of..) Given Its String Name in JavaScript

How to set object property (of object property of..) given its string name in JavaScript?

function assign(obj, prop, value) {
if (typeof prop === "string")
prop = prop.split(".");

if (prop.length > 1) {
var e = prop.shift();
assign(obj[e] =
Object.prototype.toString.call(obj[e]) === "[object Object]"
? obj[e]
: {},
prop,
value);
} else
obj[prop[0]] = value;
}

var obj = {},
propName = "foo.bar.foobar";

assign(obj, propName, "Value");

How to create an object property from a variable value in JavaScript?

There's the dot notation and the bracket notation

myObj[a] = b;

Get object property name as a string

Yes you can, with a little change.

function propName(prop, value){
for(var i in prop) {
if (prop[i] == value){
return i;
}
}
return false;
}

Now you can get the value like so:

 var pn = propName(person,person.first_name);
// pn = "first_name";

Note I am not sure what it can be used for.

Other Note wont work very well with nested objects. but then again, see the first note.

Convert string value to object property name

Use ['propname']:

objPosition[txtCol] = "whatever";

Demo: http://jsfiddle.net/hr7XW/

JavaScript object: access variable property by name as string

You don't need a function for it - simply use the bracket notation:

var side = columns['right'];

This is equal to dot notation, var side = columns.right;, except the fact that right could also come from a variable, function return value, etc., when using bracket notation.

If you NEED a function for it, here it is:

function read_prop(obj, prop) {
return obj[prop];
}

To answer some of the comments below that aren't directly related to the original question, nested objects can be referenced through multiple brackets. If you have a nested object like so:

var foo = { a: 1, b: 2, c: {x: 999, y:998, z: 997}};

you can access property x of c as follows:

var cx = foo['c']['x']

If a property is undefined, an attempt to reference it will return undefined (not null or false):

foo['c']['q'] === null
// returns false

foo['c']['q'] === false
// returns false

foo['c']['q'] === undefined
// returns true

Use string as property name

You'd do something like

var string = 'name';
var username = user[string];

to access the property in the string.

Right now your code is trying to access an undefined 'string' property on the user.

Getting a Custom Objects properties by string var

Simply use myObject['thing'].

Create an object from two objects and set their property names as a value from their type definitions

You can use [key: number]: A | B. Any key of the object should be a number or you can specify any type and the value of this key is A or B.

I could not find any way to make dynamic keys like you try to do.

for example :

type A = {
key: number;
name: string;
age: number;
};

type B = {
key: number;
adress: string;
phone: number;
};

const obj1: A = { key: 1, name: "John", age: 30 };
const obj2: B = { key: 2, adress: "London", phone: 12345 };

const merge = (obj1: A, obj2: B): { [key: string]: A | B } => {
return { [obj1.key]: obj1, [obj2.key]: obj2 };
};

merge(obj1,obj2);
// {1: {key: 1, name: "John", age: 30}, 2: {key: 2, adress: "London", phone: 12345}}



Related Topics



Leave a reply



Submit