The object result restructuring function works fine at the same time the map result not coming in one array group.
console.clear();
const filterList = {
ageRange: ["ageRange", "picAgeRange"],
goal: ["goal", "motivation"],
hairColor: ["hairColor", "picHairColor"]
};
const questionList = [{
question: "ageRange",
lastUpdated: "2018-07-09T18:13:42.541",
info: null,
type: "SELECT",
answers: [{
value: "18-30",
selected: true
},
{
value: "31-35",
selected: false
},
{
value: "36-45",
selected: false
},
{
value: "46-55",
selected: false
},
{
value: "55+",
selected: false
}
]
},
{
question: "picAgeRange",
lastUpdated: "2018-07-09T18:13:42.541",
info: null,
type: "SELECT",
answers: [{
value: "country 6",
selected: true
},
{
value: "smart casual 4",
selected: false
},
{
value: "dark denim 6",
selected: false
},
{
value: "sporty 6",
selected: false
},
{
value: "adventure 5",
selected: false
},
{
value: "prints 5",
selected: false
},
{
value: "excentric 5",
selected: false
},
{
value: "dont like any private style",
selected: false
}
]
},
{
question: "goal",
lastUpdated: "2018-07-09T18:13:42.541",
info: null,
type: "SELECT",
answers: [{
value: "save time",
selected: true
},
{
value: "personal advice",
selected: false
},
{
value: "inspiration",
selected: false
},
{
value: "testing the service",
selected: false
}
]
},
{
question: "hairColor",
lastUpdated: "2018-07-09T18:13:49.567",
info: null,
type: "SELECT",
answers: [{
value: "blond",
selected: true
},
{
value: "brown",
selected: false
},
{
value: "black",
selected: false
},
{
value: "red",
selected: false
},
{
value: "grey",
selected: false
},
{
value: "noHair",
selected: false
}
]
},
{
question: "picHairColor",
lastUpdated: "2018-07-09T18:13:42.541",
info: null,
type: "SELECT",
answers: [{
value: "suit 3",
selected: true
},
{
value: "business casual",
selected: false
},
{
value: "casual",
selected: false
},
{
value: "have to wear uniform",
selected: false
},
{
value: "classic",
selected: false
},
{
value: "conservative",
selected: false
},
{
value: "relaxed 2",
selected: false
},
{
value: "dont like any work style",
selected: false
}
]
}
];
const ofQuestionsList = Object.entries(filterList).map(
([questionKey, questionNames]) => {
return {
[questionKey]: questionList.filter((item) =>
questionNames.includes(item.question)
)
};
}
);
console.log(ofQuestionsList);
The existing result is
[
{
"ageRange": [
{
"question": "ageRange",
"lastUpdated": "2018-07-09T18:13:42.541",
"info": null,
"type": "SELECT"
},
{
"question": "picAgeRange",
"lastUpdated": "2018-07-09T18:13:42.541",
"info": null,
"type": "SELECT"
}
]
},
{
"goal": [
{
"question": "goal",
"lastUpdated": "2018-07-09T18:13:42.541",
"info": null,
"type": "SELECT"
}
]
},
{
"hairColor": [
{
"question": "hairColor",
"lastUpdated": "2018-07-09T18:13:49.567",
"info": null,
"type": "SELECT"
},
{
"question": "picHairColor",
"lastUpdated": "2018-07-09T18:13:42.541",
"info": null,
"type": "SELECT"
}
]
}
]
The expecting result is
{
"ageRange":[
{
"question":"ageRange",
"lastUpdated":"2018-07-09T18:13:42.541",
"info":null,
"type":"SELECT"
},
{
"question":"picAgeRange",
"lastUpdated":"2018-07-09T18:13:42.541",
"info":null,
"type":"SELECT"
}
],
"goal":[
{
"question":"goal",
"lastUpdated":"2018-07-09T18:13:42.541",
"info":null,
"type":"SELECT"
}
],
"hairColor":[
{
"question":"hairColor",
"lastUpdated":"2018-07-09T18:13:49.567",
"info":null,
"type":"SELECT"
},
{
"question":"picHairColor",
"lastUpdated":"2018-07-09T18:13:42.541",
"info":null,
"type":"SELECT"
}
]
}
2
Answers
Manage with result
Array.prototype.map()
returns an array, for your case if I understood correctly you want to group your elements and return them in one results object.This seems like something that can be achieved with
Array.prototype.reduce()
, so you would have:This reduce takes 2 parameters, a callback taking the
accumulator
and yourcurrent element
that being processed in your array. And theaccumulator
‘s initialization with an empty object{}
.Then in each iteration we return a merged object with the previous values of the iterator, and the current one