2013-08-22 89 views
0

我想驗證一個對象,也許這個對象可以有額外的參數,但我只有我想要一些。什麼是最好的克隆方式。我試過下劃線,但沒有任何反應:克隆對象只有一些參數

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}} 

回答

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'); 
+0

好了,所以我需要過濾每個嵌套對象作爲獨立 – user1710825