Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating multiple arrays of objects

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.

like image 620
JadeHippo Avatar asked Jun 08 '26 21:06

JadeHippo


1 Answers

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);
}
like image 190
Julien Maret Avatar answered Jun 11 '26 11:06

Julien Maret



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!