Reputation: 692
i have a array of objects which define below
const jsObjects = [
{
"ID": 5,
"Question": "How long have you been using old service?",
"QuestionID": 1,
"Type": "Radio",
"answers": [
{
"SurveyAnswer": "0-1year",
"UniqueID": 6,
},
{
"SurveyAnswer": "1-2years",
"UniqueID": 7,
"isCheck": true,
},
{
"SurveyAnswer": "3-5years",
"UniqueID": 8,
},
],
},
{
"ID": 6,
"Question": "How long have you been using new service?",
"QuestionID": 2,
"Type": "Radio",
"answers": [
{
"SurveyAnswer": "0-1year",
"UniqueID": 6,
},
{
"SurveyAnswer": "1-2years",
"UniqueID": 7,
"isCheck": true,
},
{
"SurveyAnswer": "3-5years",
"UniqueID": 8,
},
],
},
]
What I'm trying to do here is to return a new array which only have isChecked: true
in the answers
array.
So the output will be
const jsObjects = [
{
"ID": 5,
"Question": "How long have you been using old service?",
"QuestionID": 1,
"Type": "Radio",
"answers": [
{
"SurveyAnswer": "1-2years",
"UniqueID": 7,
"isCheck": true,
}
],
},
{
"ID": 6,
"Question": "How long have you been using new service?",
"QuestionID": 2,
"Type": "Radio",
"answers": [
{
"SurveyAnswer": "1-2years",
"UniqueID": 7,
"isCheck": true,
}
],
},
]
I tried filter method on JSObjects but getting undefined
let result = jsObjects.answers.filter(obj => {
return obj.isCheck === true
})
What i'm doing wrong here ?
Thanks in Advance ......
Upvotes: 1
Views: 82
Reputation: 76
You need to loop against every "Outer" jsObject and change the answers array inside it. So it could be something like:
jsObjects.map(jsObj => {
jsObj.answers = jsObj.answers.filter(ans => ans.isCheck)
return jsObj
})
EDIT:
this will mutate the original object. So if you don't need to do that, you might destructure the object in the map
callback or create another reference for the object first using something like const clone = JSON.parse(JSON.stringify(jsObjects));
Upvotes: 0
Reputation: 3849
You can use the map and filter to return the new array with require values. Below is the working snippet with your expected output
const jsObjects = [{
"ID": 5,
"Question": "How long have you been using old service?",
"QuestionID": 1,
"Type": "Radio",
"answers": [{
"SurveyAnswer": "0-1year",
"UniqueID": 6,
},
{
"SurveyAnswer": "1-2years",
"UniqueID": 7,
"isCheck": true,
},
{
"SurveyAnswer": "3-5years",
"UniqueID": 8,
},
],
},
{
"ID": 6,
"Question": "How long have you been using new service?",
"QuestionID": 2,
"Type": "Radio",
"answers": [{
"SurveyAnswer": "0-1year",
"UniqueID": 6,
},
{
"SurveyAnswer": "1-2years",
"UniqueID": 7,
"isCheck": true,
},
{
"SurveyAnswer": "3-5years",
"UniqueID": 8,
},
],
},
]
const data = jsObjects.map(data => {
return {
ID: data.ID,
Question: data.Question,
QuestionID: data.QuestionID,
Type: data.Type,
answers: data.answers.filter(data => data.isCheck)
}
});
console.log(data)
Upvotes: 1
Reputation: 20699
Since you have 2 nested arrays, you can generate a new array using Array#map
and then use Array#filter
to get the desired elements in the nested array like this:
const result = jsObjects.map(({ answers, ...rest }) => ({
...rest,
answers: answers.filter(answer => answer.isCheck === true)
}));
Here's the full example:
const jsObjects = [
{
"ID": 5,
"Question": "How long have you been using old service?",
"QuestionID": 1,
"Type": "Radio",
"answers": [
{
"SurveyAnswer": "0-1year",
"UniqueID": 6,
},
{
"SurveyAnswer": "1-2years",
"UniqueID": 7,
"isCheck": true,
},
{
"SurveyAnswer": "3-5years",
"UniqueID": 8,
},
],
},
{
"ID": 6,
"Question": "How long have you been using new service?",
"QuestionID": 2,
"Type": "Radio",
"answers": [
{
"SurveyAnswer": "0-1year",
"UniqueID": 6,
},
{
"SurveyAnswer": "1-2years",
"UniqueID": 7,
"isCheck": true,
},
{
"SurveyAnswer": "3-5years",
"UniqueID": 8,
},
],
},
]
const result = jsObjects.map(({ answers, ...rest }) => ({
...rest,
answers: answers.filter(answer => answer.isCheck === true)
}));
console.log(result);
Upvotes: 2