Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7656479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:46:27+00:00 2026-05-31T12:46:27+00:00

I am working on a social networking site, where users can send messages to

  • 0

I am working on a social networking site, where users can send messages to each other. I am sending ajax requests to a php file to fetch the messages received by a user in json format, like this:

  {"messages":[
{"mid":"1","name":"Sam","msg":"Hi Anna"},
{"mid":"4","name":"Kelly","msg":"Hi Anna"},
{"mid":"5","name":"Sam","msg":"Hi Anna"},
{"mid":"7","name":"Sam","msg":"Hello Anna"},
{"mid":"8","name":"Kelly","msg":"Anna"} ]}

In the above scenario, the user “Anna” is receiving messages from users “Sam” and “Kelly”.
Now i need to parse this json structure to separate the responses from each user, and show them to Anna.

How can i parse this json array to get 2 javascript arrays, so that i can loop them and show the responses?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:46:28+00:00Added an answer on May 31, 2026 at 12:46 pm

    Assuming you already have the JSON decoded and into a javascript variable:

    var response = {
      "messages":[
        {"name":"Sam","msg":"Hi Anna"},
        {"name":"Kelly","msg":"Hi Anna"},
        {"name":"Sam","msg":"Hi Anna"},
        {"name":"Sam","msg":"Hello Anna"},
        {"name":"Kelly","msg":"Anna"}
      ]
    };
    
    var dataByUser = {}, userName;
    var msgs = response.messages;
    
    for (var i = 0; i < msgs.length; i++) {
        userName = msgs[i].name;
        // if we don't have any msgs for this user, then initialize their data structure
        if (!dataByUser[userName]) {
            dataByUser[userName] = [];
        }
        dataByUser[userName].push(msgs[i].msg);
    }
    

    This results in a data structure that looks like this:

    var dataByUser = {
        "Sam" : ["Hi Anna", "Hi Anna", "Hello Anna"],
        "Kelly": ["Hi Anna", "Anna"]
    };
    

    You can then loop though that whenever you need to with:

    for (var userName in dataByUser) {
        // dataByUser[userName] is the array of messages from userName
    }
    

    Or if you know the userName you are interested in, you can index directly into it:

    dataByUser["Sam"]     // contains array of messages from Sam
    

    Edit: Apparently, you edited your question and added a message id (mid) which was not in the data when I wrote this answer. It is unclear what you want to do with that message id. If need be, the data for each user could be an array of objects instead of an array of strings where each object would contain both the message id and the message.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a social site where users can block other users. Throughout
I'm working on a new social networking site and I want to give users
I'm working on a social networking site and need users to be able to
I am working on a social networking site where people can comment on posts,
I've been working on the database for a very basic social networking site: all
I am working on social networking site. now our team decide to store user
I am working on a social networking site where we are implementing profiles, pages,
I am working on a social networking site with a family tree that is
I am working on a social networking site and it has a wall like
I am currently working on a web app for social networking site but it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.