1
data = [
    ('pale', 'ple', True),
    ('pales', 'pale', True),
    ('pale', 'bale', True),
    ('paleabc', 'pleabc', True),
    ('pale', 'ble', False),
    ('a', 'b', True),
    ('', 'd', True),
    ('d', 'de', True),
    ('pale', 'pale', True),
    ('pale', 'ple', True),
    ('ple', 'pale', True),
    ('pale', 'bale', True),
    ('pale', 'bake', False),
    ('pale', 'pse', False),
    ('ples', 'pales', True),
    ('pale', 'pas', False),
    ('pas', 'pale', False),
    ('pale', 'pkle', True),
    ('pkle', 'pable', False),
    ('pal', 'palks', False),
    ('palks', 'pal', False)
]
def test_one_away(data):
    for [test_s1, test_s2, expected] in data:
       print(test_s1, test_s2)

This data variable is defined in python. How can I create a similar data array in javascript so that I can loop through and print each item as shown above

4 Answers 4

2

A quite straightforward approach would be transforming the Python lists to Javascript arrays, then access them as usual in the loop or reassign their values to local variables, if desired. Also make sure to convert the booleans to lowercase. The following example is compatible to pre-ES6 environments:

var data = [
    ['pale', 'ple', true],
    ['pales', 'pale', true],
    // ...
];

for (var i = 0; i < data.length; ++i)
{
    var row = data[i];
    var test_s1 = row[0];
    var test_s2 = row[1];
    var expected = row[2];
    // ...
}

If your target environment supports ES6, you could use this variant:

const data = [
    ['pale', 'ple', true],
    ['pales', 'pale', true],
    // ...
];

for (let row of data)
{
    let [test_s1, test_s2, expected] = row;
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your python code in Javascript might look like this.

const data = [
    ['pale', 'ple', true],
    ['pales', 'pale', true],
    ['pale', 'bale', true],
    ['paleabc', 'pleabc', true],
    ['pale', 'ble', false],
    ['a', 'b', true],
    ['', 'd', true],
    ['d', 'de', true],
    ['pale', 'pale', true],
    ['pale', 'ple', true],
    ['ple', 'pale', true],
    ['pale', 'bale', true],
    ['pale', 'bake', false],
    ['pale', 'pse', false],
    ['ples', 'pales', true],
    ['pale', 'pas', false],
    ['pas', 'pale', false],
    ['pale', 'pkle', true],
    ['pkle', 'pable', false],
    ['pal', 'palks', false],
    ['palks', 'pal', false]
]

const printMyData = (data) => {
  data.forEach(arr => {
    console.log(arr[0] + ' ' + arr[1])
  })
}

printMyData(data)

You have to do some restructuring of your actual data first, and make sure your booleans are lowercase. printMyData is a function that accepts this data and forEach() of the arrays inside of it, logs the first and second element of it.

Comments

0

Use Javascript objects:

function makeTuple(s1, s2, expected) {
     return {
         s1: s1,
         s2: s2,
         expected: expected
     };
}

var data = [
    makeTuple('pale', 'ple', true),
    makeTuple('pales', 'pale', true),
    ...
];

for (var i = 0; i < data.length; i++) {
    var obj = data[i];
    console.log(obj.s1, obj.s2);
    // console.log(obj.expected);
}

Or you could simply use arrays:

var data = [
    ['pale', 'ple', true],
    ['pales', 'pale', true],
    ...
];

for (var i = 0; i < data.length; i++) {
    var obj = data[i];
    console.log(obj[0], obj[1]);
    // console.log(obj[2]);
}

Comments

0

If you do not want keys to be used, Simply create array of arrays:

var data = [['pale', 'ple', true], ['pale', 'ple', true], ['pale', 'ple', true]];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.