WerdZerg
WerdZerg

Reputation: 23

Store data into a javascript variable

I'm trying to store data into a javascript variable.

Here is my scenario : I have a list of people (person_id, person_name), and each person is succeptible to have pets (pet_id, pet_name) and/or toys (toys_id).

[Persons]
   Person_Id
   Person_Name
   [Pets]
      Pet_Id
      Pet_Name
   [Toys]
      Toy_Id

I tried various things and even if sometimes I can get everything stored I'm unsure i'm doing it the right way.

Should i store the IDs as keys ? Is like this correctly done ? Knowing later i will try to know how many pets and toys each person have. Also, my variable is supposed to store 1 or many persons.

 var myVar = {person_id:{
                person_name:'',
                pet_id:{
                    pet_name:''
                },
                toys:{
                    toys_id:''
                }
            }};

Some help would be much appreciated.

Upvotes: 2

Views: 89

Answers (4)

Gene R
Gene R

Reputation: 3744

some scenarios require to repeat elements in list, so storing IDs as keys dont let this to implement. But if still decide to use IDs as keys put also IDs as Values:

{
    "123":
    {
        "ID": 123,
        "Name": "some name"
    }
}

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386520

You can use a structure like below. When the ids are different, then there is no need to wrap them in an array.

Another good ides is to keep the properties simple as name instead of pet_name or toy_name.

Below is an example how to iterate over the object and get all pets and toys from the object.

var object = {
    p01: {
        name: 'Anakin',
        pets: {
            pet01: {
                name: 'Cat'
            }
        },
        toys: {
            R2D2: {
                name: 'Roboter 1'
            },
            C3PO: {
                name: 'Roboter 2'
            }
        }
    },
    'FN-2187': {
        name: 'Finn',
        pets: {
            pet03: {
                name: 'Sam'
            }
        },
        toys: {
            BB8: {
                name: 'Roboter 3'
            }
        }
    }
};

function getItem(type) {
    var array = [];
    Object.keys(object).forEach(function (k) {
        Object.keys(object[k][type]).forEach(function (kk) {
            array.push(object[k][type][kk].name + ' (' + kk + ') owned by ' + object[k].name  + ' (' + k + ')');
        });
    });
    return array;
}

document.write('<pre>' + JSON.stringify(getItem('pets'), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(getItem('toys'), 0, 4) + '</pre>');

Upvotes: 0

agentscarn
agentscarn

Reputation: 146

I think this depends on how you intend to use the collection of people. Keeping an array of person objects would mean that modifying a person involves finding the person in the array, splicing the person off of the array, making the modification, then inserting them back. If individual persons will rarely get modified then an array may fit the bill.

However, if you will be modifying individual person objects often, then storing them as properties of an object may be simpler. For instance

var people = {
    person_id1: {
        person_id: "person_id1",
        person_name: "person_name1",
        pets: [
            {
                pet_id: "pet_id1",
                pet_name: "pet_name1"
            },
            {
                pet_id: "pet_id2",
                pet_name: "pet_name2"
            }
        ],
        toys: []
    },
    person_id2: {
        person_id: "person_id2",
        person_name: "person_name2",
        pets: [],
        toys: [
            {
                toy_id: "toy_id1",
                toy_name: "toy_name1"
            }
        ]
    }
};

This would allow you to modify any person knowing their id.

people["person_id1"].toys.push({toy_id: "toy_id2", toy_name: "toy_name2"});

or

people.person_id1.toys.push({toy_id: "toy_id2", toy_name: "toy_name2"});

Upvotes: 0

Suhail Keyjani
Suhail Keyjani

Reputation: 488

I think best way is to store persons in array

var persons = [
               {
                person_id:1,
                person_name:'per1',
                pets:[
                      {id:1,name:"petname1"},
                      {id:2,name:"petname2"}
                    ]
                ,
                toys:[{id:1,name:"toyname1"},{id:2,name:"toyname2"}]
              }
            ];
//to find person by id=1
var arr=persons.filter(function(obj){return obj.person_id=1});
alert(arr.length)
if(arr.length>0){
//to find pets length 
  alert(arr[0].pets.length)
  }

Upvotes: 1

Related Questions