3

For an experiment I'm writing, I have to make six of the kind of list below.

var list1 = [];
var enc_len_1 = pregenerated_faces[1].encoding_faces.length;
var rec_len_1 = pregenerated_faces[1].recall_faces.length; 

for (var i = 0; i < enc_len_1; i++){
    var obj_encode = { 
        'encode': pregenerated_faces[1].encoding_faces[i]
    };
    list1.push(obj_encode);
};

for (var i = 0; i < rec_len_1; i++){
  var obj_recall = { 
      'reacall': pregenerated_faces[1].recall_faces[i]
  };
  list1.push(obj_recall);
};

Each list will look like this: list = [{encode: ...jpg}, {encode: ...jpg}..., {recall: ...jpg}, {recall:...jpg}...]

What is a smarter way to make six of these without writing six-fold redundant code? Oh, also, pregenerated_faces is an array of .json objects containing all the encoding and recall faces image strings.

5
  • Can you provide a code block of how you want that function to actually work? Like a dummy function that will work for you? Commented Jun 19, 2020 at 8:11
  • It isn't a function, but a list variable that I'll use as a timeline variable in JsPsych. in the code below, i'll iterate over the list created above. ` var trial1 = { timeline: [generateListStart, displayList, distractor, recognitionTest], timeline_variables: list1 } ` Commented Jun 19, 2020 at 8:16
  • Yes, you want to create 6 variable lists without writing the whole code again... I am actually thinking of separating the create list code to a function, so you can have N list with the given definition. Commented Jun 19, 2020 at 8:24
  • That is a good idea, but I think that might be difficult given that the number of lists is "fixed" in that I create the json array via python; this script is where I decide how many lists to create, in this case, six. But once I set it, the number of lists cannot be changed.. or can it? Maybe the information about how many lists are created can be translated from python to javascript by creating a field in the json file. Commented Jun 19, 2020 at 8:36
  • I hope that you have to iterate over pregenerated_faces and not encoding_faces Commented Jun 19, 2020 at 8:42

2 Answers 2

1
var lists = [];
for (var face of pregenerated_faces) { //should loop 6 time
  var list = [];
  face .encoding_faces.forEach( e => list.push({'encode': e}));
  face .recall_faces.forEach( e => list.push({'recall': e}));
  list.push(list);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Check this, I have created a function that will give you N lists depending upon the input size of your pregenerated_faces, and your list renderer is also out of the implementation so that it can be used anywhere... and can be changed too

function getNObjects(N, keyCallback) {
  const output = []
  for (var i = 0; i < N; i++) {
    output.push(keyCallback(i));
  }
  return output
}

// your code
function createList(index) {
  var list = [];
  var enc_len = pregenerated_faces[index].encoding_faces.length;
  var rec_len = pregenerated_faces[index].recall_faces.length;

  for (var i = 0; i < enc_len; i++) {
    var obj_encode = {
      'encode': pregenerated_faces[index].encoding_faces[i]
    };
    list.push(obj_encode);
  };

  for (var i = 0; i < rec_len; i++) {
    var obj_recall = {
      'reacall': pregenerated_faces[index].recall_faces[i]
    };
    list.push(obj_recall);
  };
  return list
}
// Test case
const pregenerated_faces = [{
    name: "Ardis Pool",
    gender: "male"
  },
  {
    name: "Barbra Panganiban",
    gender: "female"
  },
  {
    name: "Ralph Sales",
    gender: "male"
  },
]
// Test Function
function testFunction(index) {
  var list = [];
  var enc_len = pregenerated_faces[index].name
  var rec_len = pregenerated_faces[index].gender

  var obj_encode = {
    'encode': enc_len
  };
  list.push(obj_encode);
  obj_recall = {
    'reacall': rec_len
  };
  list.push(obj_recall);
  return list
}

const [one, two, three] = getNObjects(3, testFunction);
console.log(one, two, three)

2 Comments

Thanks, but could you clarify what the code is doing below the "test case" comment?
The test case is just to show how the function will work... pregenerated_faces is a list from which we have to create multiple lists for each index... testFunction is the list renderer... which will create the list for that index... and I am using es6 syntax to spread the output from getNObjects to have all the list at once.

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.