当前位置: 首页 > news >正文

达州达县网站建设济南防疫最新动态

达州达县网站建设,济南防疫最新动态,许昌网站建设找汉狮,小学网站建设情况汇报前言 js拷贝数组对象:浅拷贝&深拷贝,包括:Object.assign、concat、slice、JSON.parse(JSON.stringify()) 场景:弹窗选择组织结构(树形结构),选择后显示相关数据至输入框中(每次选…

前言

js拷贝数组对象:浅拷贝&深拷贝,包括:Object.assign、concat、slice、JSON.parse(JSON.stringify())
场景:弹窗选择组织结构(树形结构),选择后显示相关数据至输入框中(每次选择都将重新拷贝初始组织结构数据)
博客地址:芒果橙的个人博客 【http://mangocheng.com】


关于浅拷贝、深拷贝的使用场景

在开发过程中,需要基于某一个对象上进行新增修改的场景是非常多的,因此经常会进行对象的拷贝。在不需要多次复用源数据的情况下,那么对象的拷贝只需要进行赋值就能满足要求,即浅拷贝,但有时候需要多次使用到初始的原数组,那么则需要深拷贝,以达到每次拷贝原数组都是初始数据的目的

常用的拷贝方法

场景

  • 弹窗选择组织结构(树形结构),选择后显示相关数据至输入框中

  • 数据格式:[ {},{}]

  • 场景数据,案例使用

const copy = {// 源数组1:简单数组sourceArr: ['a', 'b', 'c'],			targetArr: [],// 源数组2:复杂数组sourceArrAndObj: [					{'A': 1,'B': 2},{'S': 10,'T': 20}],targetArrAndObj: []
}

1. 普通赋值语法-简单数据、复杂数据均为浅拷贝

目标数组=源数组

  • 测试数据
    • 源数组1-sourceArr:[‘a’, ‘b’, ‘c’]
    • 目标数组1-targetArr:[]
    • 源数组2-sourceArrAndObj:[ { ‘A’ : 1, ‘B’:2 }, { ‘S’ : 10, ‘T’ : 20 } ]
    • 目标数组2-targetArrAndObj: []
  • 操作
    • 赋值目标数组
    • 增加目标数组1数据[d、e]
    • 增加目标数组2数据属性[C、D]、修改属性[S、T]
  • 输出
    • 源数组1-sourceArr = a,b,c,d,e
    • 源数组2-sourceArrAndObj = [ { ‘A’ : 1, ‘B’:2, ‘C’ : 3, ‘D’ : 4 }, { ‘S’ : 30, ‘T’ : 40 } ]
  • 结果&结论
    • 源数组改变
    • 浅拷贝作用
console.log('sourceArr = ', this.sourceArr.join());console.log('sourceArrAndObj=', this.sourceArrAndObj);this.targetArr = this.sourceArr;this.targetArrAndObj = this.sourceArrAndObj;console.log('赋值后:targetArr = ', this.targetArr.join());console.log('赋值后:targetArrAndObj=', this.targetArrAndObj);console.log('=====增加目标数组数据[d、e]');this.targetArr.push('d');this.targetArr.push('e');console.log('=====增加目标数组数据属性[C、D];修改属性[S、T]');this.targetArrAndObj[0].C = 3;this.targetArrAndObj[0].D = 4;this.targetArrAndObj[1].S = 30;this.targetArrAndObj[1].T = 40;console.log('更新后:sourceArr = ', this.sourceArr.join());console.log('更新后:sourceArrAndObj=', this.sourceArrAndObj);

2. Object.assign(target,source)-简单数据深拷贝、复杂数据浅拷贝

Object.assign(目标数组,源数组)

  • 测试简单数据
    • 源数组sourceArr:[‘a’, ‘b’, ‘c’]
    • 目标数组targetArr:[]
  • 操作
    • 增加目标数组数据[d、e]
  • 输出
    • targetArr = a,b,c,d,e
    • sourceArr= a,b,c
  • 结果&结论
    • 源数组未改变
    • 深拷贝作用
console.log('=========简单数组对象=========');console.log('sourceArr = ', this.sourceArr.join());Object.assign(this.targetArr, this.sourceArr);console.log('拷贝后:targetArr = ', this.targetArr.join());console.log('=====增加目标数组数据[d、e]');this.targetArr.push('d');this.targetArr.push('e');console.log('更新后:sourceArr = ', this.sourceArr.join());console.log('更新后:targetArr = ', this.targetArr.join());
  • 测试复杂数据
    • 源数组sourceArrAndObj:[ { ‘A’ : 1, ‘B’:2 }, { ‘S’ : 10, ‘T’ : 20 } ]
    • 目标数组targetArrAndObj: []
  • 操作
    • 增加目标数组数据属性[C、D]
    • 修改属性[S、T]
  • 输出
    • sourceArrAndObj:[ { ‘A’ : 1, ‘B’:2, ‘C’ : 3, ‘D’ : 4 }, { ‘S’ : 30, ‘T’ : 40 } ]
    • targetArrAndObj:[ { ‘A’ : 1, ‘B’:2, ‘C’ : 3, ‘D’ : 4 }, { ‘S’ : 30, ‘T’ : 40 } ]
  • 结果&结论
    • 源数组改变,与目标数组一样
    • 浅拷贝作用
console.log('=========复杂数组对象=========')console.log('sourceArrAndObj=', this.sourceArrAndObj);Object.assign(this.targetArrAndObj, this.sourceArrAndObj);console.log('拷贝后:targetArrAndObj=', this.targetArrAndObj);console.log('=====增加目标数组数据属性[C、D];修改属性[S、T]');this.targetArrAndObj[0].C = 3;this.targetArrAndObj[0].D = 4;this.targetArrAndObj[1].S = 30;this.targetArrAndObj[1].T = 40;console.log('更新后:sourceArrAndObj=', this.sourceArrAndObj);console.log('更新后:targetArrAndObj=', this.targetArrAndObj);

3. concat()/slice()-简单数据深拷贝、复杂数据浅拷贝

目标数组=源数组.concat();

目标数组=源数组.slice();

  • 测试简单数据
    • 源数组sourceArr:[‘a’, ‘b’, ‘c’]
    • 目标数组targetArr:[]
  • 操作
    • 增加目标数组数据[d、e]
  • 输出
    • sourceArr= a,b,c
    • targetArr = a,b,c,d,e
  • 结果&结论
    • 源数组未改变
    • 深拷贝作用
console.log('=========简单数组对象=========');console.log('sourceArr = ', this.sourceArr.join());this.targetArr = this.sourceArr.concat();// this.targetArr = this.sourceArr.slice();console.log('拷贝后:targetArr = ', this.targetArr.join());console.log('=====增加目标数组数据[d、e]');this.targetArr.push('d');this.targetArr.push('e');console.log('更新后:sourceArr = ', this.sourceArr.join());console.log('更新后:targetArr = ', this.targetArr.join());
  • 测试复杂数据
    • 源数组sourceArrAndObj:[ { ‘A’ : 1, ‘B’:2 }, { ‘S’ : 10, ‘T’ : 20 } ]
    • 目标数组targetArrAndObj: []
  • 操作
    • 增加目标数组数据属性[C、D]
    • 修改属性[S、T]
  • 输出
    • sourceArrAndObj:[ { ‘A’ : 1, ‘B’:2, ‘C’ : 3, ‘D’ : 4 }, { ‘S’ : 30, ‘T’ : 40 } ]
    • targetArrAndObj:[ { ‘A’ : 1, ‘B’:2, ‘C’ : 3, ‘D’ : 4 }, { ‘S’ : 30, ‘T’ : 40 } ]
  • 结果&结论
    • 源数组改变,与目标数组一样
    • 浅拷贝作用
console.log('=========复杂数组对象=========')console.log('sourceArrAndObj=', this.sourceArrAndObj);this.targetArrAndObj = this.sourceArrAndObj.concat();// this.targetArrAndObj = this.sourceArrAndObj.slice();console.log('拷贝后:targetArrAndObj=', this.targetArrAndObj);console.log('=====增加目标数组数据属性[C、D];修改属性[S、T]');this.targetArrAndObj[0].C = 3;this.targetArrAndObj[0].D = 4;this.targetArrAndObj[1].S = 30;this.targetArrAndObj[1].T = 40;console.log('更新后:sourceArrAndObj=', this.sourceArrAndObj);console.log('更新后:targetArrAndObj=', this.targetArrAndObj);

4. JSON.parse(JSON.stringify())-简单数据、复杂数据均为深拷贝

目标数组=JSON.parse(JSON.stringify(源数组))

  • 测试简单数据
    • 源数组sourceArr:[‘a’, ‘b’, ‘c’]
    • 目标数组targetArr:[]
  • 操作
    • 增加目标数组数据[d、e]
  • 输出
    • targetArr = a,b,c,d,e
    • sourceArr= a,b,c
  • 结果&结论
    • 源数组未改变
    • 深拷贝作用
console.log('=========简单数组对象=========');console.log('sourceArr = ', this.sourceArr.join());this.targetArr = JSON.parse(JSON.stringify(this.sourceArr));console.log('拷贝后:targetArr = ', this.targetArr.join());console.log('=====增加目标数组数据[d、e]');this.targetArr.push('d');this.targetArr.push('e');console.log('更新后:sourceArr = ', this.sourceArr.join());console.log('更新后:targetArr = ', this.targetArr.join());
  • 测试复杂数据
    • 源数组sourceArrAndObj:[ { ‘A’ : 1, ‘B’:2 }, { ‘S’ : 10, ‘T’ : 20 } ]
    • 目标数组targetArrAndObj: []
  • 操作
    • 增加目标数组数据属性[C、D]
    • 修改属性[S、T]
  • 输出
    • sourceArrAndObj:[ { ‘A’ : 1, ‘B’:2 }, { ‘S’ : 10, ‘T’ : 20 } ]
    • targetArrAndObj:[ { ‘A’ : 1, ‘B’:2, ‘C’ : 3, ‘D’ : 4 }, { ‘S’ : 30, ‘T’ : 40 } ]
  • 结果&结论
    • 源数组未改变
    • 深拷贝作用
console.log('=========复杂数组对象=========')console.log('sourceArrAndObj=', this.sourceArrAndObj);this.targetArrAndObj = JSON.parse(JSON.stringify(this.sourceArrAndObj));console.log('拷贝后:targetArrAndObj=', this.targetArrAndObj);console.log('=====增加目标数组数据属性[C、D];修改属性[S、T]');this.targetArrAndObj[0].C = 3;this.targetArrAndObj[0].D = 4;this.targetArrAndObj[1].S = 30;this.targetArrAndObj[1].T = 40;console.log('更新后:sourceArrAndObj=', this.sourceArrAndObj);console.log('更新后:targetArrAndObj=', this.targetArrAndObj);

参考代码

const copy = {sourceArr: ['a', 'b', 'c'],targetArr: [],sourceArrAndObj: [{'A': 1,'B': 2},{'S': 10,'T': 20}],targetArrAndObj: [],/*** = 赋值*/equalSign: function () {console.log('sourceArr = ', this.sourceArr.join());console.log('sourceArrAndObj=', this.sourceArrAndObj);this.targetArr = this.sourceArr;this.targetArrAndObj = this.sourceArrAndObj;console.log('赋值后:targetArr = ', this.targetArr.join());console.log('赋值后:targetArrAndObj=', this.targetArrAndObj);console.log('=====增加目标数组数据[d、e]');this.targetArr.push('d');this.targetArr.push('e');console.log('=====增加目标数组数据属性[C、D];修改属性[S、T]');this.targetArrAndObj[0].C = 3;this.targetArrAndObj[0].D = 4;this.targetArrAndObj[1].S = 30;this.targetArrAndObj[1].T = 40;console.log('更新后:sourceArr = ', this.sourceArr.join());console.log('更新后:sourceArrAndObj=', this.sourceArrAndObj);},/*** Object.assign()*/objectAssign: function () {console.log('=========简单数组对象=========');console.log('sourceArr = ', this.sourceArr.join());Object.assign(this.targetArr, this.sourceArr);console.log('拷贝后:targetArr = ', this.targetArr.join());console.log('=====增加目标数组数据[d、e]');this.targetArr.push('d');this.targetArr.push('e');console.log('更新后:sourceArr = ', this.sourceArr.join());console.log('更新后:targetArr = ', this.targetArr.join());console.log('=========复杂数组对象=========')console.log('sourceArrAndObj=', this.sourceArrAndObj);Object.assign(this.targetArrAndObj, this.sourceArrAndObj);console.log('拷贝后:targetArrAndObj=', this.targetArrAndObj);console.log('=====增加目标数组数据属性[C、D];修改属性[S、T]');this.targetArrAndObj[0].C = 3;this.targetArrAndObj[0].D = 4;this.targetArrAndObj[1].S = 30;this.targetArrAndObj[1].T = 40;console.log('更新后:sourceArrAndObj=', this.sourceArrAndObj);console.log('更新后:targetArrAndObj=', this.targetArrAndObj);},/*** concat方法*/concataAndSlice: function () {console.log('=========简单数组对象=========');console.log('sourceArr = ', this.sourceArr.join());this.targetArr = this.sourceArr.concat();// this.targetArr = this.sourceArr.slice();console.log('拷贝后:targetArr = ', this.targetArr.join());console.log('=====增加目标数组数据[d、e]');this.targetArr.push('d');this.targetArr.push('e');console.log('更新后:sourceArr = ', this.sourceArr.join());console.log('更新后:targetArr = ', this.targetArr.join());console.log('=========复杂数组对象=========')console.log('sourceArrAndObj=', this.sourceArrAndObj);this.targetArrAndObj = this.sourceArrAndObj.concat();// this.targetArrAndObj = this.sourceArrAndObj.slice();console.log('拷贝后:targetArrAndObj=', this.targetArrAndObj);console.log('=====增加目标数组数据属性[C、D];修改属性[S、T]');this.targetArrAndObj[0].C = 3;this.targetArrAndObj[0].D = 4;this.targetArrAndObj[1].S = 30;this.targetArrAndObj[1].T = 40;console.log('更新后:sourceArrAndObj=', this.sourceArrAndObj);console.log('更新后:targetArrAndObj=', this.targetArrAndObj);},/*** json转换*/json2array: function () {console.log('=========简单数组对象=========');console.log('sourceArr = ', this.sourceArr.join());this.targetArr = JSON.parse(JSON.stringify(this.sourceArr));console.log('拷贝后:targetArr = ', this.targetArr.join());console.log('=====增加目标数组数据[d、e]');this.targetArr.push('d');this.targetArr.push('e');console.log('更新后:sourceArr = ', this.sourceArr.join());console.log('更新后:targetArr = ', this.targetArr.join());console.log('=========复杂数组对象=========')console.log('sourceArrAndObj=', this.sourceArrAndObj);this.targetArrAndObj = JSON.parse(JSON.stringify(this.sourceArrAndObj));console.log('拷贝后:targetArrAndObj=', this.targetArrAndObj);console.log('=====增加目标数组数据属性[C、D];修改属性[S、T]');this.targetArrAndObj[0].C = 3;this.targetArrAndObj[0].D = 4;this.targetArrAndObj[1].S = 30;this.targetArrAndObj[1].T = 40;console.log('更新后:sourceArrAndObj=', this.sourceArrAndObj);console.log('更新后:targetArrAndObj=', this.targetArrAndObj);},test: function () {this.equalSign();// this.objectAssign();// this.concataAndSlice();// this.json2array();}}copy.test();
http://www.15wanjia.com/news/173497.html

相关文章:

  • 平台网站的策划设计好的 做网站的软件公司
  • 无忧中英繁企业网站系统通用版公司资质查询官方网站
  • 简单的公司网站网站seo策划方案设计
  • 电子商务网站开发的目的是什么新手小白怎样运营1688店铺
  • 网站开发公司有资质吗那块做微信平台网站
  • 全球做网站的公司排名小红书信息流广告投放
  • 北京旅游网站排名网站建设运营服务商
  • 网站建设佰首选金手指十四北京网站建设文章
  • 广州做网站哪家公司最好自己的简历怎么制作网站
  • 万户网站做的怎样中国光大国际建设工程公司网站
  • 网站建设文献综述正规外贸网站建设公司
  • 建建建设网站公司电话鞍山58同城二手房
  • sakai wordpress邢台seo推广
  • 做网站备案不少天网站推广入口
  • 如何仿别人网站的莫板seo免费推广
  • 网站建设新手在郑州做网站
  • 临检中心网站建设成都柚米科技公众号开发
  • 福鼎建设局网站首页wordpress安装数据库连接错误
  • 游戏网站建设项目规划做花茶的网站
  • 公司建一个网站国际机票搜索量大涨
  • 怎么在网站中添加百度商桥如何注册一个空壳公司
  • 品牌展示型网站有哪些免费建站的网站有哪些
  • 视频在线直播网站建设云南网页
  • 图书馆建设网站注意点做网站游戏推广赚钱
  • 浙江制做网站的公司织梦更新网站地图
  • 苏州网站推广微博推广别人知道你使用推广了吗
  • 专门做招商的网站是什么wordpress开启gzip
  • 网站错误404代做设计网站
  • 高端娱乐网站建设杭州app开发公司定制外包
  • 网站开发硬件环境怎么填自己建网站 知乎