【问题标题】:Trying to build a json object to send to laravel. Problems with key name尝试构建一个 json 对象以发送到 laravel。键名问题
【发布时间】:2015-07-23 14:15:58
【问题描述】:

我正在尝试创建一个键和值数组以发送到我的 laravel 后端。

这是我当前的代码

$http.post('../survey/'+$scope.clientID+'/getSurvey', {client: $scope.clientID }).

        success(function(data, status, headers, config) {
            console.log(data);
            $scope.survey_id = data[0];

            $scope.questions = data;

            //$scope.dLength = data.length;
            $scope.dLength = 5;
            console.log($scope.questions);

            // When an answer button is clicked

            $scope.clicky = function(value) {

                // Add a class to hide the div cntainng the question
                $scope.class = "hideit";

                //Set a timeout function so the question can fadeOut before we proces anything
                var callAtTimeout = function() {

                    // Check to see if this is the last question
                    if($scope.qid >= $scope.dLength -1){

                        // All questions have been answered. AJAX the data back to Laravel

                        alert("complete")

                        $http.post('../survey/'+$scope.survey_id.survey_id+'/submitSurvey', {data: angular.fromJson($scope.answers)}).
                        success(function(data, status, headers, config) {
                            console.log(data);
                        })
                        //})

                        //console.log($scope.answers)

                    }else{  

                        //Build up the Array of answeres

                        // Get Question name - this will be the key
                        var questionName = $scope.questions[$scope.qid].name;

                        // This is the value
                        var questionAnswer = value

                        //build the aray element
                        var line = { questionName : value };

                        //Push it to the array
                        $scope.answers.push({questionName: questionAnswer});

                        //console.log($scope.answers)

                        //add to the count variable                 
                        $scope.qid += 1;

                        // Animate the question div back up to display the next question
                        $scope.class = "showit";

                    }
                }

                $timeout(callAtTimeout, 1000)
            }

        }).

当我查看我正在构建的数据时,它显示问题名称作为键,而不是变量问题名称中的数据。

当我警告变量问题名称时,它显示了正确的数据,但数组只是将键显示为“问题名称”,我是否构建了错误的数组,如果是,我应该如何构建它?

【问题讨论】:

    标签: php json angularjs laravel


    【解决方案1】:

    如果我理解正确,您可以尝试这样构建它:

    var question = {};
    question[questionName] = questionAnswer;
    $scope.answers.push(question);
    

    此外,使用 ECMAScript 6 似乎可以以更漂亮的方式实现相同的目标:

    $scope.answers.push({ [questionName]: questionAnswer});
    

    更新:

    根据 cmets,您不需要创建数组,而是构建一个对象。可能是这样的:

    $scope.answers = {};
    // loop through the questions:
    $scope.answers[questionName] = questionAnswer;
    

    之后,您将能够访问这样的答案:

    var answer = $scope.answers["MyQuestionName"];
    

    【讨论】:

    • 我认为这是迄今为止我所获得的最接近的结果。这给了我一个包含 3 个对象的数组。我想要的是一个完整的键值集合,所以我可以引用它们来获取值。例如我使用 $array[senior_1] 这将包含答案(值)
    • @ChrisTownsend,我已经更新了答案。看起来您在这里根本不需要数组。只需构建一个简单的对象。
    猜你喜欢
    • 2011-02-13
    • 1970-01-01
    • 2016-08-13
    • 2022-01-09
    • 1970-01-01
    • 2018-09-16
    • 2014-10-22
    • 2015-12-03
    • 2016-06-21
    相关资源
    最近更新 更多