splice

区分join(),split(),slice(),splice()

一曲冷凌霜 提交于 2020-01-15 04:10:30
先来体会他们的英文意思,join连接,加入;split,分裂;slice, 裁剪,切片;splice,拼接, 1,Array.prototype.splice(): 数组方法,删除数个元素,并插入新元素; 语法: Array.prototype.splice(start,deleteCount,item): start,从start后面开始删除, deleteCount,删除的数量, item,要添加的项目,可以是很多个, var ar = ['a','b','c']; console.log(ar.splice(1,0,'Joe')); //output:['a','Joe','b','c']; 2,Array.prototype.slice(): 数组方法,浅拷贝,不会改变原数组; 语法: Array.prototype.slice( [ begin [,end] ] ) begin,开始的地方,可选参数,如果没有这个参数,就从零开始, end,结束的地方,可选参数,如果没有这个参数,一直到最后, 裁剪的地方包括begin,不包括end var ar = ['a','b','c']; console.log(ar.slice(1,2,)); //output:['b'] 3,String.prototype.slice(): 此方法和上一个一样,只不过也适用于字符串 4

js 数组 删除指定元素/对象

不想你离开。 提交于 2020-01-14 07:29:00
删除数组首元素 var a = [ 'dog' , 2 , 3 ] ; var b = a . shift ( ) ; //会改变数组,b为当前数组a中的首元素 b = dog,a = [2,3] 新增元素为数组首元素 var a = [ 1 , 2 , 3 ] ; var b = a . unshift ( 'a' ) ; // b为当前数组a中包含元素个数 b = 4 a = ['a',1,2,3] 删除数组末尾元素 var a = [ 1 , 2 , 3 ] ; var b = a . pop ( ) ; // b = 3 a = [1,2] 新增元素为数组末位元素 var a = [ 1 , 2 , 3 ] ; var b = a . push ( 'a' ) ; // b为当前数组a中包含元素个数 b = 4 a = [1,2,3,'a'] 删除数组指定位置元素 splice(改变原数组) var a = [ 1 , 2 , 3 ] var b = a . splice ( 0 , 1 ) ; // b = [1] a = [2,3] 删除下标从 0 开始,包括0的第一位 var c = a . splice ( 1 ) ; // c = [3] a = [2] 删除下标从1开始(包括1)到最后一位 var a1 = [ 10 , 2 , 3 , 4 , 5 , 6 ,

数组方法处理

孤街醉人 提交于 2020-01-11 19:54:27
splice() splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目。 该方法会改变原始数组。 splice(index,number,item1…itemx) 参数详解:index-需要删除元素在数组中的起始下标; number-需要删除的数量 item1…itemx-在数组删除的地方插入元素 删除单个元素: let newArr = [1,2,3,4,5] console.log(newArr.splice(0,1)) [1] console.log(newArr) (4) [2, 3, 4, 5] 删除多个元素: let newArr = [1,2,3,4,5] console.log(newArr.splice(2,2)) (2) [3, 4] console.log(newArr) (3) [1, 2, 5] 插入元素: let newArr = [1,2,3,4,5] console.log(newArr.splice(2,0,'88')) [] console.log(newArr) (6) [1, 2, "88", 3, 4, 5] 替换元素: let newArr = [1,2,3,4,5] console.log(newArr.splice(2,1,'88')) [] console.log(newArr) (6) [1, 2, "88",

splice(0) vs splice(0,undefined)

喜夏-厌秋 提交于 2020-01-11 12:29:07
问题 Splice with no second argument behaves as expected: ['a','b','c'].splice(0) // Returns ['a','b','c'] But Splice with a undefined second argument behaves differently: ['a','b','c'].splice(0, undefined) // Returns [] Can someone explain to me the difference? I would expect the same (first) result. It suggests that internally, splice is using "arguments.length" or similar, to change its behaviour, rather than checking the arguments. 回答1: It suggests that internally, splice is using "arguments

Deleting an object based on the id in javascript

纵饮孤独 提交于 2020-01-06 17:01:11
问题 This is a follow up of Pushing an object into array where I was pushing an object into the array by identifying the parentActivityId. Now I wanted to remove the object based on its id.I have tried the below code based on the follow up question but its not working.Can anyone tell me what I'm doing wrong here? function getParent(r, a) { return a.id === child.parentActivityId ? a : a.items.reduce(getParent, r); } var node = data.reduce(getParent, {}); 'items' in node && node.items.splice(child,1

Deleting an object based on the id in javascript

陌路散爱 提交于 2020-01-06 17:01:05
问题 This is a follow up of Pushing an object into array where I was pushing an object into the array by identifying the parentActivityId. Now I wanted to remove the object based on its id.I have tried the below code based on the follow up question but its not working.Can anyone tell me what I'm doing wrong here? function getParent(r, a) { return a.id === child.parentActivityId ? a : a.items.reduce(getParent, r); } var node = data.reduce(getParent, {}); 'items' in node && node.items.splice(child,1

Unable to render an array into table after splice and unshift in Javascript

时间秒杀一切 提交于 2020-01-06 13:09:04
问题 I am not sure if I am doing some mistake or is it a default behaviour. I am using splice/unshift to add a new element on top of a 2d array and then try to render that array to a table. for (var i = 0; i < arr1[i].length; i++) { for (var j = 0; j < arr1.length; j++) { var r = arr1[j][i]; dt(r); } dttop(i); } function dt(x) { if (isFinite(x) == true) { numeric++; } else { str++; } } function dttop(x) { if (numeric > str) arr1.splice(0, 0, "number"); else arr1.splice(0, 0, "string"); } alert

Unable to render an array into table after splice and unshift in Javascript

柔情痞子 提交于 2020-01-06 13:08:42
问题 I am not sure if I am doing some mistake or is it a default behaviour. I am using splice/unshift to add a new element on top of a 2d array and then try to render that array to a table. for (var i = 0; i < arr1[i].length; i++) { for (var j = 0; j < arr1.length; j++) { var r = arr1[j][i]; dt(r); } dttop(i); } function dt(x) { if (isFinite(x) == true) { numeric++; } else { str++; } } function dttop(x) { if (numeric > str) arr1.splice(0, 0, "number"); else arr1.splice(0, 0, "string"); } alert

Javascript splice changing earlier values in an array

痞子三分冷 提交于 2020-01-06 04:11:52
问题 I am creating a game that randomly displays circles onto a canvas. The circles objects are added to an array and whenever the player collides with one of them I want to remove that object. Here is my code currently for the collision - for(var i = 0; i < currentGame.items.length; i++) { if (player1.x < currentGame.items[i].x + currentGame.items[i].radius*2 && player1.x + currentGame.items[i].radius*2 > currentGame.items[i].x && player1.y < currentGame.items[i].y + currentGame.items[i].radius*2

duplicating arrays javascript splicing

安稳与你 提交于 2020-01-02 14:38:09
问题 I have come across a strange bug in my code and I cannot understand why it happens. I have an array array1. I duplicate array1 by making array2 equal to array1. I then modify array2 using splice to add a number. Array1 should not be touched? But both output the same change. var array1 = [0,1,2,3,4,5]; var array2 = array1; array2.splice(1,0,1) //add console.log(array1); console.log(array2); I am assuming I am confusing array assignment? What is the proper way to duplicate arrays without this