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);
    }
}
user862602
  • 33
  • 5
  • There's also this other post ~ http://stackoverflow.com/questions/6393943/convert-javascript-string-in-dot-notation-into-an-object-reference – Phil Feb 21 '17 at 05:38

1 Answers1

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>
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • 3
    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 – buræquete Feb 21 '17 at 05:36
  • @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 Feb 21 '17 at 05:39
  • 2
    @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. – user862602 Feb 21 '17 at 07:20