简体   繁体   English

如何在JavaScript中实现多维数组

[英]How to implement multi dimensional array in javascript

在此处输入图片说明 I have called a service through ajax and got a list of values and i have parsed the values as js object in below line 我已经通过ajax调用了服务,并获得了值列表,并且在下面的行中将值解析为js对象

   var parseviewpresctiption = $.parseJSON(viewprescription);

i have an array of 20 values as json object which has value in each object.but the value in different object belong to single question id.So i need to group the value and based on the id and store it in multi dimensional array.in my above code i have declared a multi dimensional array but i could not able to store the value in array and it is throwing the error. 我有一个20个值的数组作为json对象,每个对象中都有一个值。但是不同对象中的值属于单个问题id。所以我需要将值分组并基于id并将其存储在多维array中。我上面的代码我已经声明了多维数组,但是我无法将值存储在数组中,并且抛出了错误。

   var questions = {};
   for(var i=0 ; i<3 ; i++){
   questions[questionId][i] = {
                'questionId' : parseviewpresctiption[prop].option.optionId,
                'column' : parseviewpresctiption[prop].option.content,
                'row' : parseviewpresctiption[prop].question.content,
                'value' : parseviewpresctiption[prop].text,
                'table' : tableTypeName,
                'answerId' : parseviewpresctiption[prop].answerId
                          }
             };

If i remove the multi dimensional array like 如果我删除多维数组

           questions[questionId][i] = {
                'questionId' : parseviewpresctiption[prop].option.optionId,
                'column' : parseviewpresctiption[prop].option.content,
                'row' : parseviewpresctiption[prop].question.content,
                'value' : parseviewpresctiption[prop].text,
                'table' : tableTypeName,
                'answerId' : parseviewpresctiption[prop].answerId
                          }

the last value with that id is getting stored in the array.How to declare a multi dimensional array and group the values based on the id 具有该id的最后一个值将存储在数组中。如何声明多维数组并根据id将值分组

i need to get the value and append the value in single table row 我需要获取值并将值附加到单个表行中

Sample data Object answerId : 968 doctor : null option : Object content : "N/A" optionId : 8 proto : Object patient : Object numVisits : 0 patientId : 66 rdoUser : Object vipScore : 0 proto : Object question : Object content : "Personal Physician Last Name" help : null option : null question : null questionId : 43 questionType : "TXT" proto : Object questionnaireTemplate : Object description : "Current Medical Problems" name : "MEDICAL_HISTORY" questionnaireTemplateId : 12 rdoUser : Object shared : false proto : Object sequenceNumber : null text : "S" 样本数据对象答案ID:968医生:空选项:对象内容:“不适用”选项ID:8 原型 :对象患者:对象numVisits:0患者ID:66 rdoUser:对象vipScore:0 原型 :对象问题:对象内容:“个人”医师姓氏”帮助:空选项:空问题:空问题Id:43问题类型:“ TXT” 原型 :对象问卷模板:对象描述:“当前医疗问题”名称:“ MEDICAL_HISTORY”问卷模板ID:12 rdoUser:对象共享:错误原型 :对象sequenceNumber:空文本:“ S”

Use forEach method, You should check this link here . 使用forEach方法,您应该在此处检查此链接。

Can you just console.log response and drop the screenshot here, It's hard to figure out from the data sample you've posted. 您能否只用console.log响应并将屏幕截图放在此处,就很难从您发布的数据样本中找出来。

You can use array map method instead. 您可以改用array map方法。

var questions = parseviewpresctiption.map(function(v) {
                    return {
                      questionId : v.option.optionId,
                      column : v.option.content,
                      row : v.question.content,
                      value: v.text,
                      table : tableTypeName, //I'm assuming that you
                      answerId: v.answerId
                    };
                });

This will return array of objects. 这将返回对象数组。 Sorry for the late reply mate! 对不起,迟到的队友!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM