javascript - 此场景中 array.push 和 array.splice 之间的区别

标签 javascript

我正在开发一个开源项目,我不想更改线路...

this.model[this.config.childrenPropertyName].push(child.model);

this.model[this.config.childrenPropertyName].splice(this.model.length, 0, child.model);

但是它破坏了对象模型上的顺序(这是一个树实现)。但是当我在测试工具中测试这两种方法时,它们似乎保留了顺序。我认为按照上面的方式实现,他们会做完全相同的事情......

注意:这只是我的一个愚蠢的编码错误,下面正确回答的两者之间没有区别

最佳答案

第一行插入 child.model 作为 this.model[this.config.childrenPropertyName] 数组的最后一个元素。

第二行将 child.model 插入到 this.model[this.config.childrenPropertyName] 数组内的某个位置。

仅当 this.model[this.config.childrenPropertyName].length 等于 this.model.length 时,这些行才相同,但第一行要简洁得多。

关于javascript - 此场景中 array.push 和 array.splice 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25834632/

相关文章:

javascript - jQuery UI Sliders - 根据拖动方向选择重叠的 slider

javascript - 用jquery褪色?

javascript - 检测点击可调整大小元素的调整大小 handle

javascript - 无法在同一台机器上加载 XMLHttpRequest

javascript - 为什么使用 Knockout.js 时数据没有出现在 HTML 字段中

javascript - AngularJS UI 路由器 : add variable to scope of state without specifying it in URL

javascript - Jquery加载功能在IE中不起作用

javascript - 如何解决错误 "THREE.GLTFLoader: Failed to load buffer "scene.bin“。”?

javascript - Google Analytics 如何跟踪虚假网页浏览量?

javascript - d3-根据 y 坐标在某个位置绘制网格线?