如何在AngularJS中的数组内的数组中添加值

迪·J。

如果我的标题听起来如此混乱,我深表歉意,因为我不知道该如何准确地写出它。基本上,这就是我想要做的。

例如,我有以下数组:

var sourceArray = [
     { question: "Question 1", inputType: "radio", answer: "Answer 1", id: "1" },
     { question: "Question 1", inputType: "radio", answer: "Answer 2", id: "2" },
     { question: "Question 1", inputType: "radio", answer: "Answer 3", id: "3" },
     { question: "Question 2", inputType: "radio", answer: "Answer 1", id: "4" },
     { question: "Question 2", inputType: "radio", answer: "Answer 2", id: "5" },
     { question: "Question 2", inputType: "radio", answer: "Answer 3", id: "6" }
]

我想对其进行重组,使其看起来像这样:

var newArray = [
     {
      question: "Question 1", inputType: "radio", 
      choices: [{answer: "Answer 1", id: "1"},
                {answer: "Answer 2", id: "2"},
                {answer: "Answer 3", id: "3"}
               ]},
     {
      question: "Question 2", inputType: "radio", 
      choices: [{answer: "Answer 1", id: "4"},
                {answer: "Answer 2", id: "5"},
                {answer: "Answer 3", id: "6"}
     ]}
]

答案按问题分组,因此,如果中的下一个问题sourceArray与当前问题相同,它将把答案推入choices数组。

在AngularJS中使用angular.forEach是否可能?

任何帮助将不胜感激。

一如既往地感谢您的回复。

沙尚克·阿格劳瓦尔

干得好。请参见下面的工作示例:

// Add a hepler method in the Array to find the value
Array.prototype.find = function(key, value) {
  var index = -1;
  angular.forEach(this, function(item, i) {
    if (item[key] === value) {
      index = i;
    }
  });

  return this[index];
};

var app = angular.module("sa", []);

app.controller("FooController", function($scope) {

  $scope.sourceArray = [{
    question: "Question 1",
    inputType: "radio",
    answer: "Answer 1",
    id: "1"
  }, {
    question: "Question 1",
    inputType: "radio",
    answer: "Answer 2",
    id: "2"
  }, {
    question: "Question 1",
    inputType: "radio",
    answer: "Answer 3",
    id: "3"
  }, {
    question: "Question 2",
    inputType: "radio",
    answer: "Answer 1",
    id: "4"
  }, {
    question: "Question 2",
    inputType: "radio",
    answer: "Answer 2",
    id: "5"
  }, {
    question: "Question 2",
    inputType: "radio",
    answer: "Answer 3",
    id: "6"
  }];

  $scope.newArray = [];

  $scope.convert = function() {
    // Iterate array
    angular.forEach($scope.sourceArray, function(item) {
      // Check if the question alrady exists
      if (!$scope.newArray.find('question', item.question)) {
        // If not, push the question to the array with empty choices
        $scope.newArray.push({
          question: item.question,
          inputType: item.inputType,
          choices: []
        });
      }

      var newArrayItem = $scope.newArray.find('question', item.question);
      
      // Push the choices
      newArrayItem.choices.push({
        answer: item.answer,
        id: item.id
      });
    });
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">


<div ng-app="sa" ng-controller="FooController">
  Before:
  <br>{{sourceArray | json}}
  <br>
  <a href="" ng-click="convert()">convert</a>
  <br>After:
  <br>{{newArray | json}}
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从数组列表中添加值INT

如何在循环内的字典中的键中添加值?

如何在jquery中为数组文本框添加值?

在Scala中为数组添加值

从数组中的对象添加值

如何在Node JS中向现有JSON文件数组添加值

从MongoDB中的数组元素中添加值

如何在React Native中的其他对象数组内的对象数组中添加值?

如果NodeJS中的日期相同,如何在对象数组中添加值?

如果存储在json字段中,postgres如何在数组内添加值

如何在JavaScript的JSON对象数组中添加值?

如何在python中的dic内的列表中添加值

如何在Python中创建for循环以向数组中的每个元素添加值

如何使用JS在特定位置的数组中添加值

如何在Tableau中的特定列内添加值

如何在具有相同名称和相同日期的对象数组中添加值

如何在C中的2d数组中的特定位置添加值

如何在用两个方括号括起来的numpy数组中添加值

如何在angularjs中向本地存储添加数组

如何使用for循环从bash中的数组中添加值

无法在angularjs的字符串数组中添加值?

在JavaScript中从数组添加值

我如何在结构内部的数组中添加值(Swift)

如何在jQuery中向数组添加值?

如何在C#中为对象数组添加值

在 java 中,如何在 int 数组中为该数组中的另一个值添加值?

如何在 Reactjs 中的状态对象内为数组添加值

如何在单个数组中添加值(用于解析)

在数组对象中添加值