当前位置:网站首页>JS中为对象数组添加新对象结果导致数组中已存在的对象也修改了
JS中为对象数组添加新对象结果导致数组中已存在的对象也修改了
2022-07-28 16:26:00 【Mr_Bobcp】
问题描述
为对象数组添加新对象结果导致数组中已存在的对象也改变了
我需要将一个空数组中存放已有的某个对象,这个对象是复用的,结果每次执行添加后所有对象都成为了最后添加的那个。
this.ObjList.push(this.Obj);
问题分析
因为数组是引用传递而不是值传递,指向的是同一个内存地址 ,所以赋值给数组里的对象其实一直都是同一个地址里存的同一个数组 ,所以最后一次输入的值会将每个引用的值都修改。
解决方法
先先将需要操作的Obj对象转换成字符串,然后再转换成对象这样就会新分配一块内存新创建一个对象。
this.ObjList.push((JSON.parse(JSON.stringify(this.Obj))));
边栏推荐
- Opencv based real-time stitching of simple dual camera images
- Use of influxdb2
- WPF command button transparent style
- Self study examination in April 2021
- Several methods of importing excel file data by C #
- 异步FIFO基本原理(基于Verilog的简单实现)
- 关于标准IO缓冲区的问题
- Verilog 每日一题(VL2 异步复位的串联T触发器--牛客网)
- Kubernetes service and ingress you need to master
- Difference between reconnaissance aircraft and early warning aircraft
猜你喜欢
![[impala] [error reporting solution] impala cannot read or execute the parent directory of dfs.domain.socket.path](/img/8f/eb83ff3d5364a704957bc3217ccc2e.png)
[impala] [error reporting solution] impala cannot read or execute the parent directory of dfs.domain.socket.path

Janus series article 3 API usage guide videoroom creating a new video room

Shell脚本之免交互操作

部署LAMP平台---Linux,Apache,MySQL,PHP的编译安装

LNMP源码编译安装

异步FIFO基本原理(基于Verilog的简单实现)

influxdb2的使用

零基础利用Unity3D开发AR应用并远程下载3D模型

Several methods of importing excel file data by C #

The practice of the beego framework of goweb development: Section II project initialization configuration
随机推荐
Why do I choose to use go language?
Some attention code explanations
Verilog 每日一题(VL14 自动贩售机1--FSM常见题型)
Opencv based real-time stitching of simple dual camera images
MySQL的触发器
Iris framework practice of goweb development: project summary and review
Goweb开发之Beego框架实战:第五节 项目搭建及注册用户
Flat data to tree
Uparse rich text style of uni app
Verilog daily question (simple implementation of VL30 RAM)
【presto 】presto 新版本升级详情
Export word according to the template, generate compound format tables and variable column tables
The practice of beego framework developed by goweb: Section 4 database configuration and connection
Jupyter notebook win installation record
@RequestParam使用
线性代数及矩阵论(九)
Difference between reconnaissance aircraft and early warning aircraft
Goweb开发之Beego框架实战:第四节 数据库配置及连接
Goweb开发之Beego框架实战:第四节 数据库配置及连接
Self study examination in April 2021