从给定的数组创建一个新数组并获得不同的属性作为子 object/Array
Create one new array from given array and get different properties as a Child object/Array
我的 Responses 数组有多个 ID 但问题答案不同。
Responses =
[0:{Id : 1,Name : John, QuestionId :1,Answer :8},
1:{Id : 1,Name : John, QuestionId :2,Answer :9},
2:{Id : 1,Name : John, QuestionId :3,Answer :3},
3:{Id : 1,Name : John, QuestionId :4,Answer :2},
4:{Id : 2,Name : Mark, QuestionId :1,Answer :6},
5:{Id : 2,Name : Mark, QuestionId :2,Answer :2},
6:{Id : 2,Name : Mark, QuestionId :3,Answer :5},
7:{Id : 2,Name : Mark, QuestionId :4,Answer :1}]
----------------------------------------------------------------
Output I want :
Arr =
[0:{Id : 1,Name : John,
Question : [{QuestionId :1,Answer :9},{QuestionId :2,Answer :8},{QuestionId :3,Answer :3},{QuestionId :4,Answer :2}]},
1:{Id : 2,Name : Mark,
Question : [{QuestionId :1,Answer :6},{QuestionId :2,Answer :2},{QuestionId :3,Answer :5},{QuestionId :4,Answer :1}]}]
---------------------------------------------------------
我想创建一个包含 ID、名称和一个问题对象的数组,该对象将包含问题 ID 和答案。我尝试了不同的方式,但这对我不起作用。如果你能在这里帮助我,那就太好了。谢谢。
const a = [{Id : 1,Name : 'John', QuestionId :1,Answer :8},
{Id : 1,Name : 'John', QuestionId :2,Answer :9},
{Id : 1,Name : 'John', QuestionId :3,Answer :3},
{Id : 1,Name : 'John', QuestionId :4,Answer :2},
{Id : 2,Name : 'Mark', QuestionId :1,Answer :6},
{Id : 2,Name : 'Mark', QuestionId :2,Answer :2},
{Id : 2,Name : 'Mark', QuestionId :3,Answer :5},
{Id : 2,Name : 'Mark', QuestionId :4,Answer :1}]
const result = a.reduce((acc,curVal,index,src)=>{
const {Id, Name, QuestionId,Answer} = curVal
if(index===0 ){
acc.push({Id,Name,Question:[{QuestionId, Answer}]})
}else{
const itemId = acc.findIndex(el=>el.Id===curVal.Id)
if(itemId>= 0){
acc[itemId].Question.push({QuestionId, Answer})
}else{
acc.push({Id,Name,Question:[{QuestionId, Answer}]})
}
}
return acc
},[])
console.log(result)
您首先必须按 Name
对数据进行分组。获得分组数据后,映射值。映射时,可以解构第一个问题的Id
和Name
字段,然后将QuestionId
和Answer
字段映射到Question
字段。
const groupBy = (arr, key) => arr.reduce((acc, item) => ({
...acc,
[item[key]]: [ ...(acc[[item[key]]] ?? []), item ]
}), {});
const responses = [
{ "Id": 1, "Name": "John", "QuestionId": 1, "Answer": 8 },
{ "Id": 1, "Name": "John", "QuestionId": 2, "Answer": 9 },
{ "Id": 1, "Name": "John", "QuestionId": 3, "Answer": 3 },
{ "Id": 1, "Name": "John", "QuestionId": 4, "Answer": 2 },
{ "Id": 2, "Name": "Mark", "QuestionId": 1, "Answer": 6 },
{ "Id": 2, "Name": "Mark", "QuestionId": 2, "Answer": 2 },
{ "Id": 2, "Name": "Mark", "QuestionId": 3, "Answer": 5 },
{ "Id": 2, "Name": "Mark", "QuestionId": 4, "Answer": 1 }
];
const arr = Object.values(groupBy(responses, 'Name')).map(questions => {
const [ { Id, Name } ] = questions;
return {
Id, Name, Question: questions.map(({ QuestionId, Answer }) =>
({ QuestionId, Answer }))
};
});
console.log(arr);
.as-console-wrapper { top: 0; max-height: 100% !important; }
或者,您可以跳过分组,只减少。这有点耦合,但它切断了映射。
const reduceBy = (arr, key) => Object.values(arr.reduce((acc, item) => {
const
{ Id, Name, QuestionId, Answer } = item,
prev = acc[[item[key]]] || { Id, Name, Question: [] };
return {
...acc,
[item[key]]: {
...prev,
Question: [ ...prev.Question, { QuestionId, Answer } ]
}
};
}, {}));
const responses = [
{ "Id": 1, "Name": "John", "QuestionId": 1, "Answer": 8 },
{ "Id": 1, "Name": "John", "QuestionId": 2, "Answer": 9 },
{ "Id": 1, "Name": "John", "QuestionId": 3, "Answer": 3 },
{ "Id": 1, "Name": "John", "QuestionId": 4, "Answer": 2 },
{ "Id": 2, "Name": "Mark", "QuestionId": 1, "Answer": 6 },
{ "Id": 2, "Name": "Mark", "QuestionId": 2, "Answer": 2 },
{ "Id": 2, "Name": "Mark", "QuestionId": 3, "Answer": 5 },
{ "Id": 2, "Name": "Mark", "QuestionId": 4, "Answer": 1 }
];
const arr = reduceBy(responses, 'Name');
console.log(arr);
.as-console-wrapper { top: 0; max-height: 100% !important; }
const arr = [
{Id : 1,Name : "John", QuestionId :1,Answer :8},
{Id : 1,Name : "John", QuestionId :2,Answer :9},
{Id : 1,Name : "John", QuestionId :3,Answer :3},
{Id : 1,Name : "John", QuestionId :4,Answer :2},
{Id : 2,Name : "Mark", QuestionId :1,Answer :6},
{Id : 2,Name : "Mark", QuestionId :2,Answer :2},
{Id : 2,Name : "Mark", QuestionId :3,Answer :5},
{Id : 2,Name : "Mark", QuestionId :4,Answer :1}
]
let newObj = {};
arr.forEach(({ Id, Name, QuestionId, Answer }) => {
if(newObj[Id]) {
newObj[Id]["Question"] = [
...newObj[Id]["Question"],
{ QuestionId, Answer }
]
} else {
newObj[Id] = {
Id,
Name,
Question: [{ QuestionId, Answer }]
}
}
})
console.log(Object.values(newObj).flat());
您可以使用 Set
提取唯一名称,然后使用这些名称过滤 Resopnses
数组中的项目:
let Responses=[{Id:1,Name:"John",QuestionId:1,Answer:8},{Id:1,Name:"John",QuestionId:2,Answer:9},{Id:1,Name:"John",QuestionId:3,Answer:3},{Id:1,Name:"John",QuestionId:4,Answer:2},{Id:2,Name:"Mark",QuestionId:1,Answer:6},{Id:2,Name:"Mark",QuestionId:2,Answer:2},{Id:2,Name:"Mark",QuestionId:3,Answer:5},{Id:2,Name:"Mark",QuestionId:4,Answer:1}];
let result = [...new Set(Responses.map(({Name}) => Name))]
.map(e => Responses.filter(({Name}) => Name === e))
console.log(result)
我的 Responses 数组有多个 ID 但问题答案不同。
Responses =
[0:{Id : 1,Name : John, QuestionId :1,Answer :8},
1:{Id : 1,Name : John, QuestionId :2,Answer :9},
2:{Id : 1,Name : John, QuestionId :3,Answer :3},
3:{Id : 1,Name : John, QuestionId :4,Answer :2},
4:{Id : 2,Name : Mark, QuestionId :1,Answer :6},
5:{Id : 2,Name : Mark, QuestionId :2,Answer :2},
6:{Id : 2,Name : Mark, QuestionId :3,Answer :5},
7:{Id : 2,Name : Mark, QuestionId :4,Answer :1}]
----------------------------------------------------------------
Output I want :
Arr =
[0:{Id : 1,Name : John,
Question : [{QuestionId :1,Answer :9},{QuestionId :2,Answer :8},{QuestionId :3,Answer :3},{QuestionId :4,Answer :2}]},
1:{Id : 2,Name : Mark,
Question : [{QuestionId :1,Answer :6},{QuestionId :2,Answer :2},{QuestionId :3,Answer :5},{QuestionId :4,Answer :1}]}]
---------------------------------------------------------
我想创建一个包含 ID、名称和一个问题对象的数组,该对象将包含问题 ID 和答案。我尝试了不同的方式,但这对我不起作用。如果你能在这里帮助我,那就太好了。谢谢。
const a = [{Id : 1,Name : 'John', QuestionId :1,Answer :8},
{Id : 1,Name : 'John', QuestionId :2,Answer :9},
{Id : 1,Name : 'John', QuestionId :3,Answer :3},
{Id : 1,Name : 'John', QuestionId :4,Answer :2},
{Id : 2,Name : 'Mark', QuestionId :1,Answer :6},
{Id : 2,Name : 'Mark', QuestionId :2,Answer :2},
{Id : 2,Name : 'Mark', QuestionId :3,Answer :5},
{Id : 2,Name : 'Mark', QuestionId :4,Answer :1}]
const result = a.reduce((acc,curVal,index,src)=>{
const {Id, Name, QuestionId,Answer} = curVal
if(index===0 ){
acc.push({Id,Name,Question:[{QuestionId, Answer}]})
}else{
const itemId = acc.findIndex(el=>el.Id===curVal.Id)
if(itemId>= 0){
acc[itemId].Question.push({QuestionId, Answer})
}else{
acc.push({Id,Name,Question:[{QuestionId, Answer}]})
}
}
return acc
},[])
console.log(result)
您首先必须按 Name
对数据进行分组。获得分组数据后,映射值。映射时,可以解构第一个问题的Id
和Name
字段,然后将QuestionId
和Answer
字段映射到Question
字段。
const groupBy = (arr, key) => arr.reduce((acc, item) => ({
...acc,
[item[key]]: [ ...(acc[[item[key]]] ?? []), item ]
}), {});
const responses = [
{ "Id": 1, "Name": "John", "QuestionId": 1, "Answer": 8 },
{ "Id": 1, "Name": "John", "QuestionId": 2, "Answer": 9 },
{ "Id": 1, "Name": "John", "QuestionId": 3, "Answer": 3 },
{ "Id": 1, "Name": "John", "QuestionId": 4, "Answer": 2 },
{ "Id": 2, "Name": "Mark", "QuestionId": 1, "Answer": 6 },
{ "Id": 2, "Name": "Mark", "QuestionId": 2, "Answer": 2 },
{ "Id": 2, "Name": "Mark", "QuestionId": 3, "Answer": 5 },
{ "Id": 2, "Name": "Mark", "QuestionId": 4, "Answer": 1 }
];
const arr = Object.values(groupBy(responses, 'Name')).map(questions => {
const [ { Id, Name } ] = questions;
return {
Id, Name, Question: questions.map(({ QuestionId, Answer }) =>
({ QuestionId, Answer }))
};
});
console.log(arr);
.as-console-wrapper { top: 0; max-height: 100% !important; }
或者,您可以跳过分组,只减少。这有点耦合,但它切断了映射。
const reduceBy = (arr, key) => Object.values(arr.reduce((acc, item) => {
const
{ Id, Name, QuestionId, Answer } = item,
prev = acc[[item[key]]] || { Id, Name, Question: [] };
return {
...acc,
[item[key]]: {
...prev,
Question: [ ...prev.Question, { QuestionId, Answer } ]
}
};
}, {}));
const responses = [
{ "Id": 1, "Name": "John", "QuestionId": 1, "Answer": 8 },
{ "Id": 1, "Name": "John", "QuestionId": 2, "Answer": 9 },
{ "Id": 1, "Name": "John", "QuestionId": 3, "Answer": 3 },
{ "Id": 1, "Name": "John", "QuestionId": 4, "Answer": 2 },
{ "Id": 2, "Name": "Mark", "QuestionId": 1, "Answer": 6 },
{ "Id": 2, "Name": "Mark", "QuestionId": 2, "Answer": 2 },
{ "Id": 2, "Name": "Mark", "QuestionId": 3, "Answer": 5 },
{ "Id": 2, "Name": "Mark", "QuestionId": 4, "Answer": 1 }
];
const arr = reduceBy(responses, 'Name');
console.log(arr);
.as-console-wrapper { top: 0; max-height: 100% !important; }
const arr = [
{Id : 1,Name : "John", QuestionId :1,Answer :8},
{Id : 1,Name : "John", QuestionId :2,Answer :9},
{Id : 1,Name : "John", QuestionId :3,Answer :3},
{Id : 1,Name : "John", QuestionId :4,Answer :2},
{Id : 2,Name : "Mark", QuestionId :1,Answer :6},
{Id : 2,Name : "Mark", QuestionId :2,Answer :2},
{Id : 2,Name : "Mark", QuestionId :3,Answer :5},
{Id : 2,Name : "Mark", QuestionId :4,Answer :1}
]
let newObj = {};
arr.forEach(({ Id, Name, QuestionId, Answer }) => {
if(newObj[Id]) {
newObj[Id]["Question"] = [
...newObj[Id]["Question"],
{ QuestionId, Answer }
]
} else {
newObj[Id] = {
Id,
Name,
Question: [{ QuestionId, Answer }]
}
}
})
console.log(Object.values(newObj).flat());
您可以使用 Set
提取唯一名称,然后使用这些名称过滤 Resopnses
数组中的项目:
let Responses=[{Id:1,Name:"John",QuestionId:1,Answer:8},{Id:1,Name:"John",QuestionId:2,Answer:9},{Id:1,Name:"John",QuestionId:3,Answer:3},{Id:1,Name:"John",QuestionId:4,Answer:2},{Id:2,Name:"Mark",QuestionId:1,Answer:6},{Id:2,Name:"Mark",QuestionId:2,Answer:2},{Id:2,Name:"Mark",QuestionId:3,Answer:5},{Id:2,Name:"Mark",QuestionId:4,Answer:1}];
let result = [...new Set(Responses.map(({Name}) => Name))]
.map(e => Responses.filter(({Name}) => Name === e))
console.log(result)