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 7635071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:20:52+00:00 2026-05-31T07:20:52+00:00

Using Javascript I want to have a JSON or array that will store id

  • 0

Using Javascript I want to have a JSON or array that will store id & ip data pairs, such that if I want the ip of a particular id=123, I can easily get it, ie by using data[123] or data['123']['ip'].

I tried this but cant seem to select the ip based on id:

var connected_clients = {
    "clients": [{id: "12345", ip: "123.123.123.123"}]
};

Also tried using this below, but I cant seem to add new id/ip pairs to it…

var connected_clients = {
    "12345": {
        id: "12345",
        ip: "123.123.123.123"
    }
};

How should it be done?

  • 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-31T07:20:53+00:00Added an answer on May 31, 2026 at 7:20 am

    (First up, you don’t have “a JSON”, you have an object. JSON is a string representation of data – or you could think of it as an object serialised to string form. Anyway…)

    If you want to be able to look up the details for a known id, your second structure will work fine.

    With more clients it would look like this:

    var connected_clients = {
        "12345": {
            id: "12345",
            ip: "123.123.123.123"
        },
        "12346": {
            id: "12346",
            ip: "123.234.345.456"
        },
        "12347": {
            id: "12347",
            ip: "555.444.333.222"
        }
    };
    

    And you’d get the ip of client “12346” like this:

    connected_clients["12346"]["ip"]
    // OR
    connected_clients["12346"].ip
    // OR if the id was in a variable
    var currentId = "12346";
    connected_clients[currentId]["ip"]
    

    If you want to add a client:

    connected_clients[newId] = { id: newId, ip : someIp };
    

    If you want to check if a particular id already exists:

    if (connected_clients.hasOwnProperty(someId)) {
       // do something
    }
    // OR use 'in' since your object doesn't really have a prototype chain to worry about
    if (someId in connected_clients) {
       // do something
    }
    

    To iterate through all the clients:

    for (var key in connected_clients) {
       console.log(key + " - ip is " + connected_clients[key]["ip"]);
    }
    

    Further reading:

    • https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects
    • https://developer.mozilla.org/en/JavaScript/Reference/Statements/for…in
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some JSON data that I get from a server. In my JavaScript,
I want to modify a form using javascript (after a user must have clicked
I have a javascript code using which I want to accomplish the following .
I have one page website only using HTML, CSS and JavaScript. I want to
Let's say using Javascript, I want to match a string that ends with [abcde]*
I want to filter and search through Json using multiple keywords. I have it
I have a json object array. I want to search the array and for
I have an interactive JavaScript app that runs through arrays of data. I don't
I have an array of hashes that consists of my product data in @product_records.
i want to send a json string from a html webpage using javascript to

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.