0

I'm new to protractor. I want to create a 1d array and a 2d array and want to initialize the array by passing no.of rows and no.of columns like below

I'm mentioning Java array as an example

String[][] data=new String[2][3];

I want to know how to initialize the array in protractor like in Java. And it's better and knowledge-sharing for me by explaining the initialization of a 1d array also.

Azrael
  • 175
  • 1
  • 12
Satish Rongala
  • 209
  • 6
  • 19
  • 1
    Possible duplicate of [How can I create a two dimensional array in JavaScript?](https://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript) – vandench Sep 22 '18 at 13:35
  • @vandench, i agreed that question is related to the same, but the answers posted for that qestion is not 100% to mine.. they are manually storing the values.. i want to store the values dynamically, for that i want to define the size of array – Satish Rongala Sep 22 '18 at 13:46
  • 1
    You should read the answers, the second one has exactly what you’re looking for: https://stackoverflow.com/a/966239/5899776 – vandench Sep 22 '18 at 13:53
  • i tried to run the code that mentioned in the code snippet..but it's not working for me.. – Satish Rongala Sep 22 '18 at 14:04
  • `'it('creating array for storing Data',function(){ Worksheet=Workbook.getWorksheet("TestSteps"); var data=new Array(2); data[0]="test"; data[1]="test"; data[2]="test"; for(var i=0;i – Satish Rongala Sep 22 '18 at 14:05

2 Answers2

1

JavaScript is not a strongly typed language like Java, so arrays don't need to be initialized. However we can still initialize an array if we would like.

To create a 1d array use new Array:

let array = new Array(10) // An array with 10 items

You can then fill the array with a value using fill:

let array = new Array(10).fill('dog') // An array with 10 items with a value of dog

Taking the above, we can then initialize a 2d array by creating a new array with a set length and fill it with arrays. Since fill will take a value an use it once, we cannot pass new Array() to fill. We will need to map the first array and change it's value to an array. Map takes a function so we just return a new array from the map and it will replace the original values with an array.

The result looks like this:

function initArray(rows, cols, filler = null) {
  return [...new Array(rows)].map(() => new Array(cols).fill(filler))
}

// create an array with all nulls
let arr = initArray(2, 3)
console.log(JSON.stringify(arr))
// Change a value in the array
arr[0][1] = 'dog'
console.log(JSON.stringify(arr))

// Create an array with a filler
console.log(JSON.stringify(initArray(5, 2, 'dog')))

Note: Remember that since this is javascript and not java, the size of the array is not set in stone, and it is 100% possible to push more items onto the array making it larger than the specified size without error and javascript will not complain.

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
0

First of all, If you are using protractor to automate your website then you must be using some language like 'java/ javascript / typescript', so its better that you search for "how to declare and initialize array in any of these languages.So for me im using typescript in my project so here it is :

var alphas:string[]; 
alphas = ["1","2","3","4"]; 
console.log(alphas[0]); 
console.log(alphas[1]);`

https://www.tutorialspoint.com/typescript/typescript_arrays.htm

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
AnjuR
  • 91
  • 3