仅使用一些参数克隆对象

时间:2013-08-22 18:25:00

标签: javascript object underscore.js

我想验证一个对象,也许这个对象可以有额外的参数,但我只想要一些。什么是克隆的最佳方式。我尝试了下划线,但没有任何反应:

var object = {
      name: "Pepe",
      age: 25,
      widget: {
         top: 1,
         left: 1,
         other: "jeje"
      }
};
var a = _.pick(object, "name", "widget.top");
console.log(a);
//Will must return: {name: "pepe", widget: {top: 1}}

1 个答案:

答案 0 :(得分:0)

var object = {
      name: "Pepe",
      age: 25,
      widget: {
         top: 1,
         left: 1,
         other: "jeje"
      }
};

var a = _.pick(object,'name','widget');
a.widget = _.pick(a.widget,'top');