如何从第一个数组的index [0]到第二个数组的index [0]分配一个值?

时间:2019-04-18 13:55:22

标签: javascript arrays angular

我需要为一个人分配一种颜色。我有两个数组:人物和颜色。 1.当我选择亚当时,它应该是粉红色,爱沙尼亚-黄色,阿德里安-红色,弗拉基米尔-紫色,萨曼莎-橙色。 2.换句话说:人员阵列中的索引0将颜色数组中的索引0分配(连接), people数组中的index 1与colors数组中的index 1分配(连接), people数组的index 2分配给颜色数组的index 2;等等

示例代码: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]);
    }
  }
});```

2 个答案:

答案 0 :(得分:0)

您可以使用JavaScript optimizer = Adam(lr=5e-3) model.compile(loss=keras.losses.binary_crossentropy, optimizer=optimizer, metrics=['accuracy']) 函数根据索引映射数组。我们可以使用person对象并为其分配color属性,并将其作为People数组中每个项目的新Object返回。结果将分配回People。

.map

答案 1 :(得分:0)

我不确定Angular编写代码的方式,但是我会使用Array.forEachArray.map。这就是我将如何使用香草js

对于每个人:

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

地图:

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