javascript - Remove a row from an Array of values by row index - Stack Overflow

I want to remove rows from an array by row IndexI havefunction test() {arr1 = [[Id, From, To], [1.0, A

I want to remove rows from an array by row Index

I have

function test() {
arr1 = [[Id, From, To], [1.0, AA1, BB1], [2.0, AA2, BB2], [3.0, AA3, BB3], [4.0, AA4, BB4], [5.0, AA5, BB5], [6.0, AA6, BB6]]

arr1.splice(1,1);
return arr1
}

I get

[[Id, From, To], [2.0, AA2, BB2], [3.0, AA3, BB3], [4.0, AA4, BB4], [5.0, AA5, BB5], [6.0, AA6, BB6]]

Whitch is what I want

But If I have instead

function test() {
var sht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
arr1 = [[Id, From, To], [1.0, AA1, BB1], [2.0, AA2, BB2], [3.0, AA3, BB3], [4.0, AA4, BB4], [5.0, AA5, BB5], [6.0, AA6, BB6]]

var result = arr1.splice(1,1);

sht.getRange(1,1,result.length,result[0].length).setValues(result)
}

I get

result = [[1.0, AA1, BB1]]

I do not get what is going on, how do I get

result = [[Id, From, To], [2.0, AA2, BB2], [3.0, AA3, BB3], [4.0, AA4, BB4], [5.0, AA5, BB5], [6.0, AA6, BB6]]

I want to remove rows from an array by row Index

I have

function test() {
arr1 = [[Id, From, To], [1.0, AA1, BB1], [2.0, AA2, BB2], [3.0, AA3, BB3], [4.0, AA4, BB4], [5.0, AA5, BB5], [6.0, AA6, BB6]]

arr1.splice(1,1);
return arr1
}

I get

[[Id, From, To], [2.0, AA2, BB2], [3.0, AA3, BB3], [4.0, AA4, BB4], [5.0, AA5, BB5], [6.0, AA6, BB6]]

Whitch is what I want

But If I have instead

function test() {
var sht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
arr1 = [[Id, From, To], [1.0, AA1, BB1], [2.0, AA2, BB2], [3.0, AA3, BB3], [4.0, AA4, BB4], [5.0, AA5, BB5], [6.0, AA6, BB6]]

var result = arr1.splice(1,1);

sht.getRange(1,1,result.length,result[0].length).setValues(result)
}

I get

result = [[1.0, AA1, BB1]]

I do not get what is going on, how do I get

result = [[Id, From, To], [2.0, AA2, BB2], [3.0, AA3, BB3], [4.0, AA4, BB4], [5.0, AA5, BB5], [6.0, AA6, BB6]]
Share Improve this question edited Jun 3, 2020 at 13:58 0Valt 10.4k9 gold badges39 silver badges64 bronze badges asked Jul 14, 2019 at 13:10 xyzxyz 2,30011 gold badges49 silver badges71 bronze badges 4
  • why not simply use method deleteRow method ? – Code Maniac Commented Jul 14, 2019 at 13:22
  • 3 Return arr1 rather than the result array – James Commented Jul 14, 2019 at 13:33
  • @CodeManiac It'll be slow pared to whatever can be done inside js itself. – TheMaster Commented Jul 14, 2019 at 15:43
  • @op Quote your strings: [["Id", "From","To"], [1.0,.. or provide number arrays. Your example isn't reproducible otherwise. – TheMaster Commented Jul 14, 2019 at 15:46
Add a ment  | 

1 Answer 1

Reset to default 5

Please, carefully read the documentation on splice() method. When invoked on an Array, it takes three possible arguments:

  1. start - index of Array element to start from;
  2. deleteCount - number of elements to delete starting from start;
  3. item - ma-separated list of values to replace deleted values by.

Scenario 1

As you invoke it with two arguments splice(1,1), the method starts from index 1 ([1.0, AA1, BB1]), deletes only this element and replaces it with nothing, mutating the original Array as expected.

Scenario 2

The method also returns all elements you deleted as a separate Array, if you want to return the original Array (mutated by splice()), you will need to remove the var result and return the arr1 directly.

Useful links

  1. splice() method reference;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744338830a4569278.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信