简体   繁体   中英

Setting and retrieveing value from javascript objects

I have just started using javascript and would like to ask a question (obviously :P) I have written two methods, one that gets value from an object, based on its data path (data path being structure within an object, like this: "object.subObject.anotherSubObject.property"), and another that sets a value to a object based on the data path.

Here is that code written in typescript:

public getValueFromObject(object:any, path:string|string[], thisArg:any = null):any {
    thisArg = thisArg == null ? this : thisArg;
    var value:any = null;
    if (object == null || object == undefined || object == "undefined")
        return value;
    if (path == null || path == undefined || path == "undefined" || path == "")
        return value;
    if (typeof path == "string" && !Array.isArray(path)) {
        path = (<string>path).split(".");
    }
    var currPath:string = path[0];
    if (path.length > 1 && object.hasOwnProperty(currPath)) {
        value = thisArg.getValueFromObject(object[currPath], path.slice(1), thisArg);
    }
    else if (object.hasOwnProperty(currPath)) {
        value = object[currPath];
    }
    return value;
}

private setValueToObject(dataObject:any, value:any, dataPath:string|string[], thisArg:any = null):any {
    thisArg = thisArg == null ? this : thisArg;
    if (dataObject == null || dataObject == undefined || dataObject == "undefined")
        return null;
    if (dataPath == null || dataPath == undefined || dataPath == "undefined" || dataPath == "")
        return null;
    if (typeof dataPath == "string" && !Array.isArray(dataPath)) {
        dataPath = (<string>dataPath).split(".");
    }
    var currPath:string = dataPath[0];
    if (dataPath.length > 1) {
        if (!dataObject.hasOwnProperty(currPath)) {
            dataObject[currPath] = {};
        }
        dataObject[currPath] = thisArg.setValueToObject(dataObject[currPath], value, dataPath.slice(1), thisArg);
    }
    else {
        dataObject[currPath] = value;
    }
    return dataObject;
}

Now, I would like to know, is this a well written javascript code, and are there any libraries that could do the same thing I'm trying to achieve? Maybe lodash? And would really appreciate if someone would provide an example code.

Thank you in advance.

Objects are pretty malleable with javascript as it is.

Start an object

var myobj = {
    var1: 'hello'
}

Get var1

console.log(myobj.var1) // 'hello'

Set var1

myobj.var1 = 'world'

Get var1 again

console.log(myobj.var1) // 'world'

Bring it all together to output 'hello world' to your console

var myobj = { var1: 'hello' };
console.log(myobj.var1);
myobj.var1 = 'world';
console.log(myobj.var1);

Other than that your code looks pretty ok. There is "more than way to skin a cat" and by no means is one way better than another when programming for fun. It is good to write things like you did for good practice. You should also be aware of faster ways to improve your code in production though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM