javascript在同一属性_id之后将对象添加到其他对象中

时间:2016-08-30 06:34:55

标签: javascript

    var selector = {
    "selector": {
        "_id": {
            "$exists": true,
        }
    }
};

我有这个,我想通过这样的参数在“选择器”中添加其他属性(我希望始终保持“_id:{”$ exists:true“}”属性并在_id之后添加更多,但_id必须永远留下来:

var selector = {
    "selector": {
        "_id": {
            "$exists": true,
        },
        "another":param,
        "another2":param,
    }
};

最好/最干净的方法是什么?非常感谢你!!

3 个答案:

答案 0 :(得分:0)

这只是对财产的转让:

selector.selector.another = param;
selector.selector.another2 = param;

示例:



var selector = {
  "selector": {
    "_id": {
      "$exists": true,
    }
  }
};
console.log("Before");
console.log(selector);
selector.selector.another = "first";
selector.selector.another2 = "second";
console.log("After");
console.log(selector);




答案 1 :(得分:0)

我喜欢jquery延伸很多。 https://api.jquery.com/jquery.extend/ $.extend( selector, {param1: value1, param2: value2} );

答案 2 :(得分:0)

另一种做法是:



var selector = {
    "selector": {
        "_id": {
            "$exists": true,
        }
    }
}
var dynamicProperty = "another"
selector.selector[dynamicProperty] = 123;

console.log(selector)




加上它也是动态的。