The difference between javascript array if objects and objects inside an object

Hi,

I was doing a small coding on React and needed to create a Schema , i found this code , here below -

education: [
    {
      school: {
        type: String,
        required: true
      },
      degree: {
        type: String,
        required: true
      },
      fieldofstudy: {
        type: String,
        required: true
      },
      from: {
        type: Date,
        required: true
      },
      to: {
        type: Date
      },
      current: {
        type: Boolean,
        default: false
      },
      description: {
        type: String
      }
    }
  ],
  social: {
    youtube: {
      type: String
    },
    twitter: {
      type: String
    },
    facebook: {
      type: String
    },
    linkedin: {
      type: String
    },
    instagram: {
      type: String
    }
  },
  date: {
    type: Date,
    default: Date.now
  }

Now here i can access my data and everything , but what i need to know is why here one category was taken as array of objects(education) and other as multiple objects inside object(social)…

Is there any special reason for this, kindly guide in this regard…

Education is an array of objects because it starts with [ square bracket. The other one is object of objects because it starts with { curly brace. Hope this helps.

Hi @hbar1st , thanks i totally get that , my question is why the social was also not taken as an array of objects as well, is there any reason or difference in both …

There doesn’t seem to be a reason without seeing the rest of the code. I woukd say the opposite. Education shoukd be an object of objects too.

@hbar1st here is how it is used ,


if (typeof req.body.skills !== 'undefined') {
      profileFields.skills = req.body.skills.split(',');
    }

    // Social
    profileFields.social = {};
    if (req.body.youtube) profileFields.social.youtube = req.body.youtube;
    if (req.body.twitter) profileFields.social.twitter = req.body.twitter;

Can you now tell why the author created them differently… Thanks

Nope. No idea based on what i see.

Yeah that is what i was thinking that may be there is some advanced reason , guess its there as it can there … but still i will leave the post open for some more time so if any one know any major difference in the 2 can guide … Thanks

Social is a dictionary of social links: you do a lookup like user.youtube or user.twitter. It’s exactly the same for every user. Education is an ordered collection of places the user was educated, you don’t look it up like that, you just list the entries by time. If you had a large amount of users would need a key for every school/university in existence anywhere in the world, and that’s unfeasible and makes it difficult to look anything up.

An array is the correct data structure for education. An object is the correct data structure for social links.

1 Like

@DanCouper thanks a lot, that is so deep understanding, now i totally get it

1 Like