3
function filterResult(field, value){
    var result = [
        {
            "name": "Johnson",
            "age": "12",
            "interests": {
                "color": "red"
            }
        },
        {
            "name": "Calvin",
            "age": "24",
            "interests": {
                "color": "blue"
            }
        }
    ];
    var filter = $.map(result, function(n, i){
        //other stuff here...
        n[field] = value; //dot notation
        return n;
    });
    return filter;
}

filterResult("interests.color","black");

Is there a way to convert String Dot Notation "interests.color" to object "n[field]" to update the value?

  • Please note "interests.color" just an example. sometimes level can be deeper e.g. "interests.car.make.year". Dynamic is an idea.

Got an answer from other post.

function setterDotNotation(obj, str, value){
    if (typeof str == 'string'){
        return setterDotNotation(obj,str.split('.'), value);
    }else if (str.length==1 && value!==undefined){
        return obj[str[0]] = value;
    }else if (str.length==0){
        return obj;
    }else{
        return setterDotNotation(obj[str[0]], str.slice(1), value);
    }
}
1

1 Answer 1

2

You're not dealing with JSON here, you're dealing with an object (more specifically, an array of objects).

Anyway, you can .split() the "interests.color" string on the "." and then use the two pieces:

var fieldNames = field.split(".");       // returns ["interests", "color"]
n[fieldNames[0]][fieldNames[1]] = value;

Expand and run the following snippet to see it work in context:

function filterResult(field, value){
    var result = [
        {
            "name": "Johnson",
            "age": "12",
            "interests": {
                "color": "red"
            }
        },
        {
            "name": "Calvin",
            "age": "24",
            "interests": {
                "color": "blue"
            }
        }
    ];
    var filter = $.map(result, function(n, i){
        //other stuff here...
            var fieldNames = field.split(".");
            n[fieldNames[0]][fieldNames[1]] = value;
            return n;
    });
    return filter;
}

console.log( filterResult("interests.color","black") );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

3 Comments

I agree, but this does not help with any object sent to this function that is in the 3rd layer or below. However acceptable for the current requirement I guess
@bureaquete - Well sure, but deeper nesting is still just a matter of splitting and then adding a loop to navigate down through the object one layer at a time, i.e., just the next logical step beyond what I've shown.
@nnnnnn thanks for the answer. But this is just a sample with "interests.color". sometimes can be more deeper. e.g. "interests.car.make.year". I wish i can make it more dynamic instead of hardcoded the level.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.