0

I need to assign a color to a person. I have two arrays: people and colors. 1.When I choose Adam, it should be pink, Estefania - yellow, Adrian -red, Wladimir - purple, Samantha - orange. 2. In other words: index 0 from the array of people assign (connect) to the index 0 from the colors array, index 1 from the people array assign(connect) with index 1 from the colors array, index 2 from the people array assign(connect) to index 2 from the colors array; and so on

Example code: http://plnkr.co/edit/TPZjXkkSRrIc5ApzP07F?p=preview

index.html

<html lang="en" ng-app="demo">
<head>
  <meta charset="utf-8">
  <title>AngularJS ui-select</title>

  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular-sanitize.js"></script>
  <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.css">

  <!-- ui-select files -->
  <script src="select.js"></script>
  <link rel="stylesheet" href="select.css">

  <script src="demo.js"></script>

  <!-- Select2 theme -->
  <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/select2/3.4.5/select2.css">
  <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.8.5/css/selectize.default.css">

  <style>
    body {
      padding: 15px;
    }

    .select2 > .select2-choice.ui-select-match {
      /* Because of the inclusion of Bootstrap */
      height: 29px;
    }

    .selectize-control > .selectize-dropdown {
      top: 36px;
    }
  </style>
</head>

<body ng-controller="DemoCtrl">



  <h3>Array of strings</h3>
  <button ng-click='clearTag()'>Delete</button>
  <ui-select tagging tagging-label="new tag" multiple ng-model="vm.multipleDemo" 
  on-select="OnClickSelect($item)" on-remove="OnRemoveSelect($item)"
  theme="select2" ng-disabled="disabled" style="width: 300px;">
    <ui-select-match placeholder="Select name...">{{$item.name}}</ui-select-match>
    <ui-select-choices  repeat="item in people | filter:$select.search">
      {{item.name}}
    </ui-select-choices>
  </ui-select>
  <p>Selected: {{vm.multipleDemo}}</p>
  <hr>



</body>
</html>

**demo.js**


'use strict';

var app = angular.module('demo', ['ngSanitize', 'ui.select']);



app.controller('DemoCtrl', function($scope, $http, $timeout) {
  $scope.vm = {multipleDemo: []};
    $scope.people = [
    { name: 'Adam',      email: 'adam@email.com',      age: 12, country: 'United States' },
    { name: 'Amalie',    email: 'amalie@email.com',    age: 12, country: 'Argentina' },
    { name: 'Estefanía', email: 'estefania@email.com', age: 21, country: 'Argentina' },
    { name: 'Adrian',    email: 'adrian@email.com',    age: 21, country: 'Ecuador' },
    { name: 'Wladimir',  email: 'wladimir@email.com',  age: 30, country: 'Ecuador' },
    { name: 'Samantha',  email: 'samantha@email.com',  age: 30, country: 'United States' },
    { name: 'Nicole',    email: 'nicole@email.com',    age: 43, country: 'Colombia' }

  ];

  $scope.colors = [
   'pink', 'yellow', 'red', 'purple', 'orange', 'green'
  ];


  $scope.OnClickSelect=function(item)
  {
   $scope.vm.multipleDemo.push(item.name);
  }

  $scope.OnRemoveSelect = function(item) { 
    window.console.log($scope.people);
   var index = $scope.people.indexOf(item.name);
   $scope.people.splice(index, 1); 
  }

  $scope.clearTag = function() {
    /*for(var i =0; i < $scope.vm.multipleDemo.length; i++) {
      $scope.vm.multipleDemo.splice($scope.vm.multipleDemo[i]);
      $scope.people.push($scope.vm.multipleDemo[i]);
    }*/
    var i =0;
    while(i < $scope.vm.multipleDemo.length) {
      i++;
      $scope.vm.multipleDemo.splice($scope.vm.multipleDemo[i]);
      $scope.people.push($scope.vm.multipleDemo[i]);
    }
  }
});```
zer00ne
  • 41,936
  • 6
  • 41
  • 68

2 Answers2

0

You can use javascript .map function to map the array based on index. We can use the person object and assign color property to it and return it as a new Object for each item in People array. The result is assigned back to People.

let people = [
    { name: 'Adam',      email: 'adam@email.com',      age: 12, country: 'United States' },
    { name: 'Amalie',    email: 'amalie@email.com',    age: 12, country: 'Argentina' },
    { name: 'Estefanía', email: 'estefania@email.com', age: 21, country: 'Argentina' },
    { name: 'Adrian',    email: 'adrian@email.com',    age: 21, country: 'Ecuador' },
    { name: 'Wladimir',  email: 'wladimir@email.com',  age: 30, country: 'Ecuador' },
    { name: 'Samantha',  email: 'samantha@email.com',  age: 30, country: 'United States' },
    { name: 'Nicole',    email: 'nicole@email.com',    age: 43, country: 'Colombia' }
   
  ];
  
 const colors = [
   'pink', 'yellow', 'red', 'purple', 'orange', 'green'
  ];

  people= people.map((person, index) =>
    ({ ...person,
      color: colors[index]
    }))
    
    console.log(people);
nircraft
  • 8,242
  • 5
  • 30
  • 46
Khaled Osman
  • 1,251
  • 12
  • 17
0

I'm not sure about the Angular way of writing the code but I would use either Array.forEach or Array.map. Here is how I would do it with vanilla js

For Each:

people.forEach((person, index) => {
    person.color = colors[index];
}

Map:

people = people.map((person, index) => {
    person.color = colors[index];
    return person;
}
Chris Sandvik
  • 1,787
  • 9
  • 19