2016-08-01 73 views
3

如果我的標題聽起來很混亂,我不知道如何準確地將它打開。基本上,這是我想要做的。如何在AngularJS的數組中添加數組中的值

例如,我有以下陣列:

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可以嗎?

任何幫助將不勝感激。

非常感謝您的回覆。

+3

你能發表一篇關於你已經完成解決這個問題多遠的報道嗎? – Srijith

回答

2

在這裏你去。請參見下面的工作示例:如下

// 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>

+0

好方法。很喜歡。 – Ved

+0

謝謝@Ved :-) –

+1

你是一名救生員,Shashank。這工作很好。謝謝! –

1

可以組合在一起使用for循環,

下面是代碼:

function transformArr(orig) { 
    var newArr = [], 
     answers = {}, 
     newItem, i, j, cur; 
    for (i = 0, j = orig.length; i < j; i++) { 
     cur = orig[i]; 
     if (!(cur.question in answers)) { 
      answers[cur.question] = {question: cur.question, answers: [],inputType : cur.inputType,id :cur.id}; 
      newArr.push(answers[cur.question]); 
     } 
     answers[cur.question].answers.push(cur.answer); 
    } 
    return newArr; 
} 

工作App

0

您需要filtersourceArray,兩個問題清單

var question1 = $filter('filter')(sourceArray, { 
    question: 'Question 1' 
}); 
var question2 = $filter('filter')(sourceArray, { 
    question: 'Question 2' 
}); 

之後,你應該創建一個新陣列數據如下:

$scope.questionData = [{ 
    "question": "Question 1", 
    "inputType": "radio", 
    "choices": [] 
}, { 
    "question": "Question 2", 
    "inputType": "radio", 
    "choices": [] 
}]; 

然後,您應該運行在兩個過濾一個變量環插入對象的新陣:

1):

angular.forEach(question1, function(value, key) { 
    if (value.question == $scope.questionData[0].question) { 
    $scope.questionData[0].choices.push({ 
     'answer': value.answer, 
     'id': value.id 
    }); 
    } 
}); 

2):

angular.forEach(question2, function(value, key) { 
    if (value.question == $scope.questionData[1].question) { 
    $scope.questionData[1].choices.push({ 
     'answer': value.answer, 
     'id': value.id 
    }); 
    } 
}); 

這裏指的demo