是否有可能使用解构来追加?(Is it possible to use destructuring to append?)

我想知道我是否可以使用解构来附加到列表中?

例如

p = []; q = []; var obj_1 = {a: 42, b: 52}; var obj_2 = {a: 43, b: 53}; var {p, q} = obj_1; {p, q} = obj_2;

第一次应该给我:p = [42] q = [52]

然后下一次

应该给我p = [42,43] q = [52,53]

以上示例不起作用。 所以我的问题是是否可以通过破坏来实现。 或者,如果有一些替代方式?

I wonder if I somehow can use destructuring to to append into a list?

E.g.

p = []; q = []; var obj_1 = {a: 42, b: 52}; var obj_2 = {a: 43, b: 53}; var {p, q} = obj_1; {p, q} = obj_2;

First time it should give me: p = [ 42 ] q = [ 52 ]

and then next time

should give me p = [ 42, 43 ] q = [ 52, 53 ]

Above example doesn't work. So my question is whether it is possible to do so with destructing. Or if there are some alternative way?

最满意答案

不是那种有用或省力的方式。 你可以这样做:

({a:p[p.length], b:q[q.length]} = obj_1);

......但...... :-)

const p = [];
const q = [];
const obj_1 = {a: 42, b: 52};
const obj_2 = {a: 43, b: 53};
({a:p[p.length], b:q[q.length]} = obj_1);
console.log("p", JSON.stringify(p), "q", JSON.stringify(q));
({a:p[p.length], b:q[q.length]} = obj_2);
console.log("p", JSON.stringify(p), "q", JSON.stringify(q)); 
  
 

Not in a way that's all that useful or effort-saving. You can do this:

({a:p[p.length], b:q[q.length]} = obj_1);

...but... :-)

const p = [];
const q = [];
const obj_1 = {a: 42, b: 52};
const obj_2 = {a: 43, b: 53};
({a:p[p.length], b:q[q.length]} = obj_1);
console.log("p", JSON.stringify(p), "q", JSON.stringify(q));
({a:p[p.length], b:q[q.length]} = obj_2);
console.log("p", JSON.stringify(p), "q", JSON.stringify(q)); 
  
 

是否有可能使用解构来追加?(Is it possible to use destructuring to append?)

我想知道我是否可以使用解构来附加到列表中?

例如

p = []; q = []; var obj_1 = {a: 42, b: 52}; var obj_2 = {a: 43, b: 53}; var {p, q} = obj_1; {p, q} = obj_2;

第一次应该给我:p = [42] q = [52]

然后下一次

应该给我p = [42,43] q = [52,53]

以上示例不起作用。 所以我的问题是是否可以通过破坏来实现。 或者,如果有一些替代方式?

I wonder if I somehow can use destructuring to to append into a list?

E.g.

p = []; q = []; var obj_1 = {a: 42, b: 52}; var obj_2 = {a: 43, b: 53}; var {p, q} = obj_1; {p, q} = obj_2;

First time it should give me: p = [ 42 ] q = [ 52 ]

and then next time

should give me p = [ 42, 43 ] q = [ 52, 53 ]

Above example doesn't work. So my question is whether it is possible to do so with destructing. Or if there are some alternative way?

最满意答案

不是那种有用或省力的方式。 你可以这样做:

({a:p[p.length], b:q[q.length]} = obj_1);

......但...... :-)

const p = [];
const q = [];
const obj_1 = {a: 42, b: 52};
const obj_2 = {a: 43, b: 53};
({a:p[p.length], b:q[q.length]} = obj_1);
console.log("p", JSON.stringify(p), "q", JSON.stringify(q));
({a:p[p.length], b:q[q.length]} = obj_2);
console.log("p", JSON.stringify(p), "q", JSON.stringify(q)); 
  
 

Not in a way that's all that useful or effort-saving. You can do this:

({a:p[p.length], b:q[q.length]} = obj_1);

...but... :-)

const p = [];
const q = [];
const obj_1 = {a: 42, b: 52};
const obj_2 = {a: 43, b: 53};
({a:p[p.length], b:q[q.length]} = obj_1);
console.log("p", JSON.stringify(p), "q", JSON.stringify(q));
({a:p[p.length], b:q[q.length]} = obj_2);
console.log("p", JSON.stringify(p), "q", JSON.stringify(q));