1

I have been trying to filter the nested array of objects and display the details when the min_age_limit===18.

Here is the json :

    "centers": [
    {
            "center_id": 603425,
            "name": "Childrens Hospital",
            "fee_type": "Paid",
            "sessions": [{
                "session_id": "4e687873-5ecc-4086-893e-31a1b450ca3b",
                "date": "22-05-2021",
                "available_capacity": 0,
                "min_age_limit": 18,
            }, {
                "session_id": "54347880-3090-4962-aa56-451fbaca16e1",
                "date": "22-05-2021",
                "available_capacity": 25,
                "min_age_limit": 45,
            }]
    },
    {
            "center_id": 195100,
            "name": "playschool thousandlights",
            "fee_type": "Free",
            "sessions": [{
                "session_id": "d4446397-f35e-4140-9778-6af9d8b92a5c",
                "date": "22-05-2021",
                "available_capacity": 15,
                "min_age_limit": 45,
            }, {
                "session_id": "7553c7e5-d7ec-456c-af88-0a02756cbdbb",
                "date": "23-05-2021",
                "available_capacity": 19,
                "min_age_limit": 18,
                
            }]
    },
{
            "center_id": 254600,
            "name": "calcicut Hospital",
            "fee_type": "Free",
            "sessions": [{
                "session_id": "d4446397-f35e-4140-9778-6af9d8b92a5c",
                "date": "22-05-2021",
                "available_capacity": 15,
                "min_age_limit": 18,
            }, {
                "session_id": "7553c7e5-d7ec-456c-af88-0a02756cbdbb",
                "date": "23-05-2021",
                "available_capacity": 19,
                "min_age_limit": 18,
                
            }]
    },
{
            "center_id": 264987,
            "name": "sholings GH",
            "fee_type": "Free",
            "sessions": [{
                "session_id": "d4446397-f35e-4140-9778-6af9d8b92a5c",
                "date": "22-05-2021",
                "available_capacity": 15,
                "min_age_limit": 45,
            }, {
                "session_id": "7553c7e5-d7ec-456c-af88-0a02756cbdbb",
                "date": "23-05-2021",
                "available_capacity": 19,
                "min_age_limit": 45,
                
            },
{
                "session_id": "7553c7e5-d7ec-456c-af88-0a02756cbdbb",
                "date": "24-05-2021",
                "available_capacity": 25,
                "min_age_limit": 18,
                
            }]
    },
{
            "center_id": 254566,
            "name": "greenland Hospital",
            "fee_type": "Free",
            "sessions": [{
                "session_id": "d4446397-f35e-4140-9778-6af9d8b92a5c",
                "date": "22-05-2021",
                "available_capacity": 15,
                "min_age_limit": 45,
            }, {
                "session_id": "7553c7e5-d7ec-456c-af88-0a02756cbdbb",
                "date": "23-05-2021",
                "available_capacity": 19,
                "min_age_limit": 45,
                
            }]
    }
    ]
            

So to make it clear, I need to display the session details which has the min_age_limit===18 along with his center details..

I tried this code:

 this.availabityDetails=this.centers.filter(item=>item.sessions.some(age=>age.min_age_limit===18)); 
       console.log(this.availabityDetails);

But I am getting the session details of all the center element which has satisfied atleast one condition. Like,if there is a min_age_limit=18 in the element with center_id=195100,I need to display only that session element details along with the corresponding center details which has satifies this. I need to display only this: availableDetails should contain this after filtering.

[
    {
            "center_id": 603425,
            "name": "Childrens Hospital",
            "fee_type": "Paid",
            "sessions": [{
                "session_id": "4e687873-5ecc-4086-893e-31a1b450ca3b",
                "date": "22-05-2021",
                "available_capacity": 0,
                "min_age_limit": 18,
            }]
    },
    {
            "center_id": 195100,
            "name": "playschool thousandlights",
            "fee_type": "Free",
            "sessions": [ {
                "session_id": "7553c7e5-d7ec-456c-af88-0a02756cbdbb",
                "date": "23-05-2021",
                "available_capacity": 19,
                "min_age_limit": 18,
                
            }]
    },
{
            "center_id": 254600,
            "name": "calcicut Hospital",
            "fee_type": "Free",
            "sessions": [{
                "session_id": "d4446397-f35e-4140-9778-6af9d8b92a5c",
                "date": "22-05-2021",
                "available_capacity": 15,
                "min_age_limit": 18,
            }, {
                "session_id": "7553c7e5-d7ec-456c-af88-0a02756cbdbb",
                "date": "23-05-2021",
                "available_capacity": 19,
                "min_age_limit": 18,
                
            }]
    },
{
            "center_id": 264987,
            "name": "sholings GH",
            "fee_type": "Free",
            "sessions": [
{
                "session_id": "7553c7e5-d7ec-456c-af88-0a02756cbdbb",
                "date": "24-05-2021",
                "available_capacity": 25,
                "min_age_limit": 18,
                
            }]
    }
    ]

I need to have the above information in the availableDetails(i.e availableDetails are of type center[]).In above o/p code,centers details are displayed with only the sessions which satisy the condition.i.e session which has min_age_limit =45 are left out . Also In the center ,if there is no session with min_age_limit=18,I dont want that center details in the o/p. Hope it gives a Clear picture of what I want. Help me with this code..

6 Answers 6

1

you can try this

  this.availabityDetails = [].concat.apply([], centers.map(c => c.sessions)).filter(s => s.min_age_limit === 18)
Sign up to request clarification or add additional context in comments.

Comments

0

const input = {"centers":[{"center_id":603425,"name":"Childrens Hospital","fee_type":"Paid","sessions":[{"session_id":"4e687873-5ecc-4086-893e-31a1b450ca3b","date":"22-05-2021","available_capacity":0,"min_age_limit":18,},{"session_id":"54347880-3090-4962-aa56-451fbaca16e1","date":"22-05-2021","available_capacity":25,"min_age_limit":45,}]},{"center_id":195100,"name":"playschool thousandlights","fee_type":"Free","sessions":[{"session_id":"d4446397-f35e-4140-9778-6af9d8b92a5c","date":"22-05-2021","available_capacity":15,"min_age_limit":45,},{"session_id":"7553c7e5-d7ec-456c-af88-0a02756cbdbb","date":"23-05-2021","available_capacity":19,"min_age_limit":18,}]},{"center_id":254600,"name":"calcicut Hospital","fee_type":"Free","sessions":[{"session_id":"d4446397-f35e-4140-9778-6af9d8b92a5c","date":"22-05-2021","available_capacity":15,"min_age_limit":18,},{"session_id":"7553c7e5-d7ec-456c-af88-0a02756cbdbb","date":"23-05-2021","available_capacity":19,"min_age_limit":18,}]},{"center_id":264987,"name":"sholings GH","fee_type":"Free","sessions":[{"session_id":"d4446397-f35e-4140-9778-6af9d8b92a5c","date":"22-05-2021","available_capacity":15,"min_age_limit":45,},{"session_id":"7553c7e5-d7ec-456c-af88-0a02756cbdbb","date":"23-05-2021","available_capacity":19,"min_age_limit":45,},{"session_id":"7553c7e5-d7ec-456c-af88-0a02756cbdbb","date":"24-05-2021","available_capacity":25,"min_age_limit":18,}]},{"center_id":254566,"name":"greenland Hospital","fee_type":"Free","sessions":[{"session_id":"d4446397-f35e-4140-9778-6af9d8b92a5c","date":"22-05-2021","available_capacity":15,"min_age_limit":45,},{"session_id":"7553c7e5-d7ec-456c-af88-0a02756cbdbb","date":"23-05-2021","available_capacity":19,"min_age_limit":45,}]}]}
const res = input.centers
  .map(({sessions, ...r}) => ({
    ...r,
    sessions: sessions.filter(e => e.min_age_limit === 18)
  }))
  .filter(e => e.sessions.length)
console.log(res)
.as-console-wrapper { max-height: 100% !important; top: 0; }

6 Comments

Hey sorry for the mistake, I dont need only the child of sessions which satisfies the condition.If the condition is satified, I need to return all the details of that element. In available details ,I also need to have center_id,name and Fee_type along with the session details.
No bro,this code also displays session details in which min_age_limit=45.I need only the session details which satisifies the condition along with the all the details.
@VenkateshwarS Is this meet your requirements? If not can you edit your post and shows us actual desired output? Cause I think no1 here knows what you exactly want.
Sorry for the Confusions .Now I have updated the code.Kindly check and help me .Thank you
Thank you so much dude.It works really fine .Can you please explain the code!
|
0

Use map() function to list all sessions with the condition min_age_limit===18.

const centers = [
    {
            "center_id": 603425,
            "name": "Childrens Hospital",
            "fee_type": "Paid",
            "sessions": [{
                "session_id": "4e687873-5ecc-4086-893e-31a1b450ca3b",
                "date": "22-05-2021",
                "available_capacity": 0,
                "min_age_limit": 18,
            }, {
                "session_id": "54347880-3090-4962-aa56-451fbaca16e1",
                "date": "22-05-2021",
                "available_capacity": 25,
                "min_age_limit": 45,
            }]
    },
    {
            "center_id": 195100,
            "name": "playschool thousandlights",
            "fee_type": "Free",
            "sessions": [{
                "session_id": "d4446397-f35e-4140-9778-6af9d8b92a5c",
                "date": "22-05-2021",
                "available_capacity": 15,
                "min_age_limit": 45,
            }, {
                "session_id": "7553c7e5-d7ec-456c-af88-0a02756cbdbb",
                "date": "23-05-2021",
                "available_capacity": 19,
                "min_age_limit": 18,
                
            }]
    }
]
var result = centers.map(item => {
  for (var i = 0, l = item.sessions.length; i < l; i += 1) {
    if (item.sessions[i].min_age_limit === 18) {
      return item.sessions[i]
    }
   }
})
document.write('<pre><ul>' + result.map(o => `<li>${o.session_id}</li>`).join('') + '</ul></pre>')

Comments

0

You can use the filter() method which creates and returns a new array with all elements that pass the test implemented by the provided function.

const centers = [
{
        "center_id": 603425,
        "name": "Childrens Hospital",
        "fee_type": "Paid",
        "sessions": [{
            "session_id": "4e687873-5ecc-4086-893e-31a1b450ca3b",
            "date": "22-05-2021",
            "available_capacity": 0,
            "min_age_limit": 18,
        }, {
            "session_id": "54347880-3090-4962-aa56-451fbaca16e1",
            "date": "22-05-2021",
            "available_capacity": 25,
            "min_age_limit": 45,
        }]
},
{
        "center_id": 195100,
        "name": "playschool thousandlights",
        "fee_type": "Free",
        "sessions": [{
            "session_id": "d4446397-f35e-4140-9778-6af9d8b92a5c",
            "date": "22-05-2021",
            "available_capacity": 15,
            "min_age_limit": 45,
        }, {
            "session_id": "7553c7e5-d7ec-456c-af88-0a02756cbdbb",
            "date": "23-05-2021",
            "available_capacity": 19,
            "min_age_limit": 18,
            
        }]
}
];
const sessions = [];
centers.forEach(x => {
   let filtered = x.sessions.find(y => y.min_age_limit === 18);
   let res = JSON.parse(JSON.stringify(filtered));
   res.center_id = x.center_id;
   res.name = x.name;
   res.fee_type = x.fee_type;
   sessions.push(res);
});

console.log(sessions);

3 Comments

Hey sorry for the mistake, I dont need only the child of sessions which satisfies the condition.If the condition is satified, I need to return all the details of that element. In available details ,I also need to have center_id,name and Fee_type along with the session details.
I updated the code for your request my friend @VenkateshwarS
Is that what you we're looking for @VenkateshwarS?
0

I think we can use reduce.

this.availabityDetails = this.centers.reduce((acc = [], curr) => {
    if (curr.sessions.length) {curr.sessions = curr.sessions.filter(session => session.min_age_limit === 18)
        acc.push(curr)
    }
    return acc
}, [])

.reduce will loop through each element and with each element we will filter session which is min_age_limit === 18 and then push it to accumulator of reduce

Hope this will work for you.

3 Comments

Hey sorry for the mistake, I dont need only the child of sessions which satisfies the condition.If the condition is satified, I need to return all the details of that element. In available details ,I also need to have center_id,name and Fee_type along with the session details.
@venkateshwar Please check the latest code.
yeah bro this works. I got what I want.But this is also displaying the remaining centers which dont satify the condition with the empty sessions array.Like consider if a certain center id doesnt have min_age_limit=18,then also that center id details is displaying with empty session details.Like if I have 100 array elements.,I have 20 element with 18 age,then i want to display only that 20 elements . but It displays 100 elements with remaining 80 elements displaying empty sessions.
0

Here is a flexible, iterative solution using object-scan

.as-console-wrapper {max-height: 100% !important; top: 0}
<script type="module">
import objectScan from 'https://cdn.jsdelivr.net/npm/[email protected]/lib/index.min.js';

const myData = { centers: [{ center_id: 603425, name: 'Childrens Hospital', fee_type: 'Paid', sessions: [{ session_id: '4e687873-5ecc-4086-893e-31a1b450ca3b', date: '22-05-2021', available_capacity: 0, min_age_limit: 18 }, { session_id: '54347880-3090-4962-aa56-451fbaca16e1', date: '22-05-2021', available_capacity: 25, min_age_limit: 45 }] }, { center_id: 195100, name: 'playschool thousandlights', fee_type: 'Free', sessions: [{ session_id: 'd4446397-f35e-4140-9778-6af9d8b92a5c', date: '22-05-2021', available_capacity: 15, min_age_limit: 45 }, { session_id: '7553c7e5-d7ec-456c-af88-0a02756cbdbb', date: '23-05-2021', available_capacity: 19, min_age_limit: 18 }] }, { center_id: 254600, name: 'calcicut Hospital', fee_type: 'Free', sessions: [{ session_id: 'd4446397-f35e-4140-9778-6af9d8b92a5c', date: '22-05-2021', available_capacity: 15, min_age_limit: 18 }, { session_id: '7553c7e5-d7ec-456c-af88-0a02756cbdbb', date: '23-05-2021', available_capacity: 19, min_age_limit: 18 }] }, { center_id: 264987, name: 'sholings GH', fee_type: 'Free', sessions: [{ session_id: 'd4446397-f35e-4140-9778-6af9d8b92a5c', date: '22-05-2021', available_capacity: 15, min_age_limit: 45 }, { session_id: '7553c7e5-d7ec-456c-af88-0a02756cbdbb', date: '23-05-2021', available_capacity: 19, min_age_limit: 45 }, { session_id: '7553c7e5-d7ec-456c-af88-0a02756cbdbb', date: '24-05-2021', available_capacity: 25, min_age_limit: 18 }] }, { center_id: 254566, name: 'greenland Hospital', fee_type: 'Free', sessions: [{ session_id: 'd4446397-f35e-4140-9778-6af9d8b92a5c', date: '22-05-2021', available_capacity: 15, min_age_limit: 45 }, { session_id: '7553c7e5-d7ec-456c-af88-0a02756cbdbb', date: '23-05-2021', available_capacity: 19, min_age_limit: 45 }] }] };

const extract = objectScan(['centers[*].sessions[*].min_age_limit'], {
  beforeFn: (state) => {
    state.context = new Map();
  },
  afterFn: (state) => {
    state.result = [...state.context.values()];
  },
  breakFn: ({ context, value, parents }) => {
    if (value === 18) {
      const { center_id } = parents[2];
      if (!context.has(center_id)) {
        context.set(center_id, { ...parents[2], sessions: [] });
      }
      context.get(center_id).sessions.push(parents[0]);
    }
  },
  reverse: false
});

console.log(extract(myData));
/* => [ { center_id: 603425, name: 'Childrens Hospital', fee_type: 'Paid', sessions: [ { session_id: '4e687873-5ecc-4086-893e-31a1b450ca3b', date: '22-05-2021', available_capacity: 0, min_age_limit: 18 } ] }, { center_id: 195100, name: 'playschool thousandlights', fee_type: 'Free', sessions: [ { session_id: '7553c7e5-d7ec-456c-af88-0a02756cbdbb', date: '23-05-2021', available_capacity: 19, min_age_limit: 18 } ] }, { center_id: 254600, name: 'calcicut Hospital', fee_type: 'Free', sessions: [ { session_id: 'd4446397-f35e-4140-9778-6af9d8b92a5c', date: '22-05-2021', available_capacity: 15, min_age_limit: 18 }, { session_id: '7553c7e5-d7ec-456c-af88-0a02756cbdbb', date: '23-05-2021', available_capacity: 19, min_age_limit: 18 } ] }, { center_id: 264987, name: 'sholings GH', fee_type: 'Free', sessions: [ { session_id: '7553c7e5-d7ec-456c-af88-0a02756cbdbb', date: '24-05-2021', available_capacity: 25, min_age_limit: 18 } ] } ] */
</script>

Disclaimer: I'm the author of object-scan

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.