gpt4 book ai didi

angularjs - 如何在 AngularJS 中的数组中的数组中添加值

转载 作者:行者123 更新时间:2023-12-01 12:26:43 24 4
gpt4 key购买 nike

如果我的标题听起来很困惑,我深表歉意,因为我不知道如何准确地表达它。基本上,这就是我想要做的。

例如,我有以下数组:

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>

关于angularjs - 如何在 AngularJS 中的数组中的数组中添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38691938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com