0

JSON:

streams = [{ id: 0,
   codec_type: 'video',
   content: '1920x1040 - H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10' },
 { id: 1,
   codec_type: 'audio',
   content: '5.1(side) - cze - undefined' },
 { id: 2,
   codec_type: 'audio',
   content: '5.1(side) - eng - undefined' },
 { id: 3, codec_type: 'subtitle', content: 'cze - Czech' },
 { id: 4, codec_type: 'subtitle', content: 'eng - English' }];

Angular HTML markup:

<md-input-container ng-repeat="stream in streams">
  <label>{{stream.codec_type}}</label>
  <md-select ng-model="stream">
    <md-option value="{{stream.id}}">{{stream.content}}</md-option>
  </md-select>
</md-input-container>

Hi, I need find duplicate string on JSON and repeat only ONE select with many options under this (codec_type) duplicate string. Please have you any idea to do this? Thank you


!!! UPDATE !!!

I must redesign JSON and work with objects separatly or something like that

CodePen.

3 Answers 3

1

What you want to do can be done with two different filters: one to grab a list of unique codec types, and one to grab a list of codecs that fit that type.

CodePen example

HTML

<div ng-app="app">
  <div ng-controller="ctrl">
    <div ng-repeat="codec in streams | unique:'codec_type'">
      {{codec.codec_type}}
      <select ng-model="$parent.selected[codec.codec_type]" ng-options="stream as stream.content for stream in streams | codec:codec.codec_type track by stream.id">
      </select>
    </div>
    Selected streams: {{selected}}
  </div>
</div>

Controller

angular.module('app', [])
  .controller('ctrl', function ($scope) {
    $scope.selected = {
      'video': null,
      'audio': null,
      'subtitle': null
    };
    $scope.streams = [
      {
        id: 0,
        codec_type: 'video',
        content: '1920x1040 - H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10'
      },
      { id: 1,
        codec_type: 'audio',
        content: '5.1(side) - cze - undefined'
      },
      { id: 2,
        codec_type: 'audio',
        content: '5.1(side) - eng - undefined'
      },
      { id: 3,
        codec_type: 'subtitle',
        content: 'cze - Czech'
      },
      { id: 4,
        codec_type: 'subtitle',
        content: 'eng - English'
      }];
  })
  .filter('unique', function () {
    return function (items, id) {
      var filtered = [];
      var ids = {};

      angular.forEach(items, function (item) {
        if (typeof(ids[item[id]]) === 'undefined') {
          filtered.push(item);
          ids[item[id]] = true;
        }
      });

      return filtered;
    };
  })
  .filter('codec', function () {
    return function (items, codec) {
      var filtered = [];

      angular.forEach(items, function (item) {
        if (item.codec_type === codec) {
          filtered.push(item);
        }
      });

      return filtered;
    };
});
Sign up to request clarification or add additional context in comments.

Comments

0

Use ngOptions directive.

Check out the angularJs Documentation AngularJS Docs

Example:

<select ng-model="stream" ng-options="stream.codec_type for stream in streams>
   <option value=""> Choose Option </option>
</select>

There are multiple other forms to use ng-options, the one I used is simply:

label for value in array

The link I posted will mention and explain each form in depth, so you can choose which one will properly fit your needs.

1 Comment

thanks for tip I must redesign JSON and now, It"s working CodePen. link
0

CONTROLLER:

var types_array = [];
$scope.isUnique = function (type) {
     if (types_array.indexOf(type) === 1){
         return false;
     } else {
         types_array.push(type);
         return true;
     }
}

HTML:

    <md-input-container ng-repeat="stream in streams">
     <div ng-if=isUnique(stream.codec_type)>
      <label>{{stream.codec_type}}</label>
      <md-select ng-model="stream">
        <md-option value="{{stream.id}}">{{stream.content}}</md-option>
      </md-select>
     </div>
    </md-input-container>

1 Comment

this get me double subtitle and none of audio

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.