Javascript 从id上的两个对象数组角度类型脚本映射新对象
我有类似的东西Javascript 从id上的两个对象数组角度类型脚本映射新对象,javascript,angular,typescript,foreach,Javascript,Angular,Typescript,Foreach,我有类似的东西 export class Question { id: string; title: string; description: string; answers: Answer[]; } export class Answer { id: string; text: string; questionId: string; } answers = [ { "id": 1, "text&qu
export class Question {
id: string;
title: string;
description: string;
answers: Answer[];
}
export class Answer {
id: string;
text: string;
questionId: string;
}
answers = [
{
"id": 1,
"text": "some comment1",
"questionId": 1
},
{
"id": 2,
"text": "some comment2",
"questionId": 3
},
{
"id": 3,
"text": "some comment3",
"questionId": 3
}
];
questions = [
{
"id": 1,
"title": "Name1",
"description": "typicode1"
},
{
"id": 2,
"title": "Name2",
"description": "typicode2"
},
{
"id": 3,
"title": "Name3",
"description": "typicode3"
}
];
questionsAndAnswers: Question[];
questionsAndAnswers = [{
id: 1,
title: Name1,
description: typicode1;
answers: [{
"id": 1,
"text": "some comment1",
"questionId": 1
}]
},
{
id: 2,
title: Name2,
description: typicode2;
answers: []
},
{
id: 3,
title: Name3,
description: typicode3;
answers: [{
"id": 2,
"text": "some comment2",
"questionId": 3
},
{
"id": 3,
"text": "some comment3",
"questionId": 3
}]
}
];
我有两个像这样的物体
export class Question {
id: string;
title: string;
description: string;
answers: Answer[];
}
export class Answer {
id: string;
text: string;
questionId: string;
}
answers = [
{
"id": 1,
"text": "some comment1",
"questionId": 1
},
{
"id": 2,
"text": "some comment2",
"questionId": 3
},
{
"id": 3,
"text": "some comment3",
"questionId": 3
}
];
questions = [
{
"id": 1,
"title": "Name1",
"description": "typicode1"
},
{
"id": 2,
"title": "Name2",
"description": "typicode2"
},
{
"id": 3,
"title": "Name3",
"description": "typicode3"
}
];
questionsAndAnswers: Question[];
questionsAndAnswers = [{
id: 1,
title: Name1,
description: typicode1;
answers: [{
"id": 1,
"text": "some comment1",
"questionId": 1
}]
},
{
id: 2,
title: Name2,
description: typicode2;
answers: []
},
{
id: 3,
title: Name3,
description: typicode3;
answers: [{
"id": 2,
"text": "some comment2",
"questionId": 3
},
{
"id": 3,
"text": "some comment3",
"questionId": 3
}]
}
];
现在我需要将答案映射到有关属性的正确问题答案
我的新问题和答案应该是这样的
export class Question {
id: string;
title: string;
description: string;
answers: Answer[];
}
export class Answer {
id: string;
text: string;
questionId: string;
}
answers = [
{
"id": 1,
"text": "some comment1",
"questionId": 1
},
{
"id": 2,
"text": "some comment2",
"questionId": 3
},
{
"id": 3,
"text": "some comment3",
"questionId": 3
}
];
questions = [
{
"id": 1,
"title": "Name1",
"description": "typicode1"
},
{
"id": 2,
"title": "Name2",
"description": "typicode2"
},
{
"id": 3,
"title": "Name3",
"description": "typicode3"
}
];
questionsAndAnswers: Question[];
questionsAndAnswers = [{
id: 1,
title: Name1,
description: typicode1;
answers: [{
"id": 1,
"text": "some comment1",
"questionId": 1
}]
},
{
id: 2,
title: Name2,
description: typicode2;
answers: []
},
{
id: 3,
title: Name3,
description: typicode3;
answers: [{
"id": 2,
"text": "some comment2",
"questionId": 3
},
{
"id": 3,
"text": "some comment3",
"questionId": 3
}]
}
];
您可以尝试将数组与函数一起使用。试试下面的方法
var-questions=[{“id”:1,“title”:“Name1”,“description”:“typicode1”},{“id”:2,“title”:“Name2”,“description”:“typicode2”},{“id”:3,“title”:“Name3”,“description”:“typicode3”}];
var answers=[{“id”:1,“文本”:“一些评论1”,“问题id”:1},{“id”:2,“文本”:“一些评论2”,“问题id”:3},{“id”:3,“文本”:“一些评论3”,“问题id”:3}];
var questions and Answers=问题。减少((会计科目、当前、索引)=>{
acc[指数]=当前汇率;
acc[index].answers=answers.filter(answer=>answer.questionId==curr.id);
返回acc;
}, []);
控制台日志(问题和答案)代码>你试过什么?你有没有考虑过在你的答案数组中使用.filter
来获得某个特定问题的答案?我甚至不知道如何开始,我是否必须先回答每个问题,然后再回答内部答案?这很好,我从未使用过reduce,谢谢,你能在地图和foreach上做出答案吗,我能理解,因为我需要学习,这对我来说太复杂了:(我认为最好了解一下reduce
函数。它几乎在所有主要语言中都可用。通过在内部打印变量acc
,curr
,并查看它如何在数组中迭代,可能更容易开始。