简体   繁体   English

Object.assign() 没有按原样构造我的对象数组

[英]Object.assign() doesn't structure my array of objects the way it is

我的一组名为 Favoritecoins 的对象

if (!this.favoriteCoins.some(e => e.cryptoName === coinId)) {

     Object.assign(this.favoriteCoins, {cryptoName: coinId})

}

The image is the result of my Object.assign and I wanted to know why doesn't it assign the way all the others are structured?该图像是我的 Object.assign 的结果,我想知道为什么它不按所有其他结构的方式分配?

You are calling Object.assign and passing an array as an argument.您正在调用Object.assign并将数组作为参数传递。 Array's can still accept properties the same ways objects do.数组仍然可以像对象一样接受属性。 But if you want to add the object to the array, use Array.prototype.push instead.但是,如果您想将 object 添加到数组中,请改用Array.prototype.push

if (!this.favoriteCoins.some(e => e.cryptoName === coinId)) {

     this.favoriteCoins.push({cryptoName: coinId})

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM