Searching JQuery arrays of taxonomy terms for word matches in frontend images upload

Answering own questions is becoming a habit 🙂

But hope this helps others after finding a solution that worked for me.

First defining the three variables I needed in the upload function:

      var features = "";
      var colors = "";
      var styles = "";

Then, added the following to prevent error if pwTags was empty:

     if (pwTags != null) {
       pwTags = pwTags;
     }else{
     pwTags = room_type;
     }

Then the following function to checkcompare each string for matches:

  function stringToCheck (arr1,arr2) {
    var common = new Array();
      for (var i = 0, n = arr1.length, j = 0, k = arr2.length; (i < n) && (j < k); i ++){
       if (arr1[i] == arr2[j]){
          common.push(arr1[i]);
          j ++;
        }
       else if (arr1[i] > arr2[j]){
          j ++;
          i --;
       }
    }
  return common.toString();
}

Next The call to create each matched words into string:

   var Tags = pwTags.split(",").sort();

    var roomGroupTerms = imageroom_group.split(",").sort();
    var roomGroup = stringToCheck(Tags,roomGroupTerms);

    var colorTerms = imagecolors.split(",").sort();
    var colors = stringToCheck(Tags,colorTerms);

    var featuresTerms = imagefeatures.split(",").sort();
    var features = stringToCheck(Tags,featuresTerms);

    var styleTerms = imagestyles.split(",").sort();
    var styles = stringToCheck(Tags,styleTerms);

Finally in the jQuery plupload function, the following return on each image upload:

  holder += "<li class="gallery-thumb">" +
        "<div class="gallery-thumb-holder">" +
        "<a class="mark-featured" data-property-id='0' data-attachment-id='"+imageID+"' href="#mark-featured"><i class="fa fa fa-star-o"></i></i></a>" +
        "<span class="deleteAttachment" data-attachment-id='"+imageID+"'><i class="fa fa-times-circle"></i></span>" +
        "<img src=""+thumbnail+"" exif="true"/>" +
        "<input class="dt-image-name" type="text" name="items_name[]" value=""+title+"" />" +
        "</div>" +
        "<div class="input-holder">"+
        "<label>Tags</label><input class="dt-image-tags" type="text" name="items_tags[]" value=""+pwTags+""/>" +
        "<label>Room Type</label><input class="dt-image-colors" type="text" name="items_image_room_group[]" value=""+room_type+"" />" +
        "<label>Colors</label><input class="dt-image-colors" type="text" name="items_image_colors[]" value=""+colors+"" />" +
        "<label>Features</label><input class="dt-image-colors" type="text" name="items_image_features[]" value=""+features+"" />" +
        "<label>Styles</label><input class="dt-image-colors" type="text" name="items_image_styles[]" value=""+styles+"" />" +
        "<input type="hidden" name="items[]" value=""+full+"" />" +
        "<input type="hidden" name="items_thumbnail[]" value=""+thumbnail+"" />" +
        "<input type="hidden" name="items_id[]" value=""+imageID+"" />" +
        "</li>";

For the PHP to create jQuery strings for each taxonomy, first placed the following into theme footer:

if( is_page_template('tpl-dashboard-admin.php')):
  echo "\n<script type="text/javascript">\n";
  echo "/* <![CDATA[ */\n";;
  echo pw_get_taxonomies_for_attachments();
  echo "/* ]]> */\n";
  echo " </script>\n";
endif;

And the function itself:

  function pw_get_taxonomies_for_attachments( $output="names") {
    $taxonomies = array();
    $out="";
    $termsOut="";
      foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) {
         foreach ( $taxonomy->object_type as $object_type ) {
           if ( 'attachment' == $object_type || 0 === strpos( $object_type, 'attachment:' ) ) {
              if ( 'names' == $output ){
                 $image_terms = get_terms( $taxonomy->name, array('hide_empty' => false,) );
                   if ( ! empty( $image_terms ) && ! is_wp_error( $image_terms ) ){
                      $out .= 'var image'.$taxonomy->name.' = "';
                      $image_terms = array_values($image_terms);
                        for($cat_count=0; 
                         $cat_count<count($image_terms); $cat_count++) {
                          $out .= $image_terms[$cat_count]->slug;
                           if ($cat_count<count($image_terms)-1){
                              $out .= ',';
                           }
                         }
                        }
                           $out .= '"'."\n";
                   }else{
                     $taxonomies[ $taxonomy->name ] = $taxonomy;
                    break;
              }
           }
         }
      }

    return $out;
  }

Hope it helps someone