我有一个复杂的对象并使用它我试图创建一个包含所有选项的单个对象。我现在的结构。
model = {
"questionnaires": [
{
"number": "377",
"self": "http://172.26.60.63:3000/spi/v2/fundTransfers/questionnaires/377",
"version": "V2",
"categoryCode": "103",
"typeCode": "201",
"questionAndAnswerOptions": [
{
"questionCode": "q0101",
"questionTypeCode": "100",
"answerOptionCodes": [
"010101",
"010102",
"010103"
],
"questionCodeDes": " cat 1 question 1",
"answerOptionCodesDes": {
"010101": "T?m parami riske ederek getirilerimi iyilestirmeye ?alisirim",
"010102": "Biriktirdigim paranin yarisini riske ederek getirilerimi iyilestirmeye ?alisirim",
"010103": "Tasarrufta bulundugum tutarlari artirarak birikimlerimi artirmaya ?alisirim"
}
},
{
"questionCode": "q0102",
"questionTypeCode": "200",
"answerOptionCodes": [
"010201",
"010202",
"010203"
],
"questionCodeDes": "cat 1 question 2",
"answerOptionCodesDes": {
"010201": "Option1",
"010202": "Option2",
"010203": "Option3"
}
}
],
"extension": {
"contractNumber": "43385",
"accountNumber": "MP373697",
"product": {
"number": "378",
"typeCode": "100",
"nameCode": "201"
}
}
},
{
"number": "394",
"self": "http://172.26.60.63:3000/spi/v2/fundTransfers/questionnaires/394",
"version": "V2",
"categoryCode": "108",
"typeCode": "205",
"questionAndAnswerOptions": [
{
"questionCode": "q0201",
"questionTypeCode": "300",
"answerOptionCodes": [
"020101",
"020102",
"020103",
"020104",
"020105"
],
"questionCodeDes": "cat 2 ques 1",
"answerOptionCodesDes": {
"020101": "80-100% arasinda",
"020102": "60-80% arasinda",
"020103": "40-60%? arasinda",
"020104": "20-40%? arasinda",
"020105": "20% ve daha azini"
}
},
{
"questionCode": "q0202",
"questionTypeCode": "400",
"answerOptionCodes": [
"020201",
"020202",
"020203",
"020204",
"020205"
],
"questionCodeDes": "cat 2 ques 2",
"answerOptionCodesDes": {
"020201": "80-100% arasinda",
"020202": "60-80% arasinda",
"020203": "40-60%? arasinda",
"020204": "20-40%? arasinda",
"020205": "20% ve daha azini"
}
},
{
"questionCode": "q0203",
"questionTypeCode": "500",
"answerOptionCodes": [
"020301",
"020302",
"020303",
"020304",
"020305"
],
"questionCodeDes": "cat 2 ques 3",
"answerOptionCodesDes": {
"020301": "80-100% arasinda",
"020302": "60-80% arasinda",
"020303": "40-60%? arasinda",
"020304": "20-40%? arasinda",
"020305": "20% ve daha azini"
}
}
],
"extension": {
"contractNumber": "63271",
"accountNumber": "MP373697",
"product": {
"number": "395",
"typeCode": "100",
"nameCode": "102"
}
}
}
]
}
必需输出:
{
"010101": "T?m parami riske ederek getirilerimi iyilestirmeye ?alisirim",
"010102": "Biriktirdigim paranin yarisini riske ederek getirilerimi iyilestirmeye ?alisirim",
"010103": "Tasarrufta bulundugum tutarlari artirarak birikimlerimi artirmaya ?alisirim",
"010201": "Option1",
"010202": "Option2",
"010203": "Option3",
"020101": "80-100% arasinda",
"020102": "60-80% arasinda",
"020103": "40-60%? arasinda",
"020104": "20-40%? arasinda",
"020105": "20% ve daha azini",
"020201": "80-100% arasinda",
"020202": "60-80% arasinda",
"020203": "40-60%? arasinda",
"020204": "20-40%? arasinda",
"020205": "20% ve daha azini",
"020301": "80-100% arasinda",
"020302": "60-80% arasinda",
"020303": "40-60%? arasinda",
"020304": "20-40%? arasinda",
"020305": "20% ve daha azini"
}
我尝试做的就是收集所有的answerOptionCodesDes并将其放在一个对象中。
请帮忙, 感谢
答案 0 :(得分:3)
首先,您需要定义所有值的对象:
questionAndAnswerOptions
然后遍历模型数组中的每个条目,并在该循环中,再次循环遍历mapped
- 数组中的每个条目。
然后,您可以使用Object.assign
model.questionnaires.forEach(function(questionnaire){
questionnaire.questionAndAnswerOptions.forEach(function(qAndA){
Object.assign(mapped,qAndA.answerOptionCodesDes);
});
});
- 变量中
{{1}}
答案 1 :(得分:3)
您可以使用reduce
返回该结果:
let result = model.questionnaires.reduce(
(acc, q) => q.questionAndAnswerOptions.reduce(
(acc, o) => Object.assign(acc, o.answerOptionCodesDes), acc), {});
let model = {
"questionnaires": [
{
"number": "377",
"self": "http://172.26.60.63:3000/spi/v2/fundTransfers/questionnaires/377",
"version": "V2",
"categoryCode": "103",
"typeCode": "201",
"questionAndAnswerOptions": [
{
"questionCode": "q0101",
"questionTypeCode": "100",
"answerOptionCodes": [
"010101",
"010102",
"010103"
],
"questionCodeDes": " cat 1 question 1",
"answerOptionCodesDes": {
"010101": "T?m parami riske ederek getirilerimi iyilestirmeye ?alisirim",
"010102": "Biriktirdigim paranin yarisini riske ederek getirilerimi iyilestirmeye ?alisirim",
"010103": "Tasarrufta bulundugum tutarlari artirarak birikimlerimi artirmaya ?alisirim"
}
},
{
"questionCode": "q0102",
"questionTypeCode": "200",
"answerOptionCodes": [
"010201",
"010202",
"010203"
],
"questionCodeDes": "cat 1 question 2",
"answerOptionCodesDes": {
"010201": "Option1",
"010202": "Option2",
"010203": "Option3"
}
}
],
"extension": {
"contractNumber": "43385",
"accountNumber": "MP373697",
"product": {
"number": "378",
"typeCode": "100",
"nameCode": "201"
}
}
},
{
"number": "394",
"self": "http://172.26.60.63:3000/spi/v2/fundTransfers/questionnaires/394",
"version": "V2",
"categoryCode": "108",
"typeCode": "205",
"questionAndAnswerOptions": [
{
"questionCode": "q0201",
"questionTypeCode": "300",
"answerOptionCodes": [
"020101",
"020102",
"020103",
"020104",
"020105"
],
"questionCodeDes": "cat 2 ques 1",
"answerOptionCodesDes": {
"020101": "80-100% arasinda",
"020102": "60-80% arasinda",
"020103": "40-60%? arasinda",
"020104": "20-40%? arasinda",
"020105": "20% ve daha azini"
}
},
{
"questionCode": "q0202",
"questionTypeCode": "400",
"answerOptionCodes": [
"020201",
"020202",
"020203",
"020204",
"020205"
],
"questionCodeDes": "cat 2 ques 2",
"answerOptionCodesDes": {
"020201": "80-100% arasinda",
"020202": "60-80% arasinda",
"020203": "40-60%? arasinda",
"020204": "20-40%? arasinda",
"020205": "20% ve daha azini"
}
},
{
"questionCode": "q0203",
"questionTypeCode": "500",
"answerOptionCodes": [
"020301",
"020302",
"020303",
"020304",
"020305"
],
"questionCodeDes": "cat 2 ques 3",
"answerOptionCodesDes": {
"020301": "80-100% arasinda",
"020302": "60-80% arasinda",
"020303": "40-60%? arasinda",
"020304": "20-40%? arasinda",
"020305": "20% ve daha azini"
}
}
],
"extension": {
"contractNumber": "63271",
"accountNumber": "MP373697",
"product": {
"number": "395",
"typeCode": "100",
"nameCode": "102"
}
}
}
]
};
let result = model.questionnaires.reduce(
(acc, q) => q.questionAndAnswerOptions.reduce(
(acc, o) => Object.assign(acc, o.answerOptionCodesDes), acc), {});
console.log(result);