jeffedsell
jeffedsell

Reputation: 135

Retrieving data from a js data object

Say I'm retrieving a JSON object with information about a user. One of the pieces of information I get back is a code, indicating their favorite fruit.

So I have userData.faveFruit and I know its value will be APP (apple), BAN (banana), CHE (cherry), or KIW (kiwi). I need to put data onto the page with details regarding the user's favorite fruit.

So I created this data object:

var fruitDefinitions = [
    { "APP" : [ "Apple",  "Red, green or yellow skin, round, white flesh, hard core with seeds." ] },
    { "BAN" : [ "Banana", "Thick yellow skin, elongated curved cylinder tapered at both ends, white flesh." ] },
    { "CHE" : [ "Cherry", "Red skin and flesh, round, green stem, single inedible pit." ] },
    { "KIW" : [ "Kiwi",   "Brown furry skin, oval, green flesh, many small edible seeds." ] },
];

So how do I go from having CHE to being able to pull out Cherry and its description? It seems like it should be quite simple, but every attempt I make simply does not work. Should I be constructing fruitDefinitions differently?

Thanks.

Upvotes: 0

Views: 87

Answers (6)

Suman Bogati
Suman Bogati

Reputation: 6349

Access for fruites name

fruitDefinitions[0]['APP'][0]

Access for fruites description

  fruitDefinitions[0]['APP'][1]

If your input is dynamic then you can do below code for fruits name and description respectively.

 fruitDefinitions[0][input][0]

and

 fruitDefinitions[0][input][1]

AND

When your array fruitDefinitions more than one values, then there would be index number of array instead of '0' which means your structure would be

       fruitDefinitions[i]['APP'][1]
       ......

Here 'i' is index number of array

But it would be good if the stucture would be as lxe said

Upvotes: -1

Axel Amthor
Axel Amthor

Reputation: 11096

If you actually want to reorganize that entirely, go away from thatJSON stuff and do a complete OO design, it actually cries for that:

var fruit = function(code, name, desc) {
    this.name = name;
    this.code = code;
    this.description = desc;

    this.getName = function() { return this.name }
    ....
}

var fruits = function() {

    this.fav = []
    this.add = function(name, desc, code) {
        var f = new fruit(code, name, desc);
        this.fav.push(f);

    ....
    etc. etc.

Upvotes: 0

Cihad Turhan
Cihad Turhan

Reputation: 2849

It's just a foreach loop. Don't use foreach loop for an array.

Use Array.filter method as lxe suggests.

Upvotes: 0

Axel Amthor
Axel Amthor

Reputation: 11096

could probably done more compact, but as a first approach:

function findfavFruit(fCode) {
    for ( var n = 0, n < fruitDefinitions.length; n++ ) {
          if ( typeof fruitDefinitions[n][fCode] != undefined )
               return fruitDefinitions[n][fCode];
    }
    return null;
}

this would return an Array

[ "Cherry", "Red skin and flesh, round, green stem, single inedible pit." ]

for "CHE"

Upvotes: 0

ic3b3rg
ic3b3rg

Reputation: 14927

I would suggest refactoring your data slightly:

var fruitDefinitions = {
    "APP" : [ "Apple",  "Red, green or yellow skin, round, white flesh, hard core with seeds." ],
    "BAN" : [ "Banana", "Thick yellow skin, elongated curved cylinder tapered at both ends, white flesh." ],
    "CHE" : [ "Cherry", "Red skin and flesh, round, green stem, single inedible pit." ],
    "KIW" : [ "Kiwi",   "Brown furry skin, oval, green flesh, many small edible seeds." ]
};

for use like so:

console.log(fruitDefinitions.CHE[0]); // Cherry
console.log(fruitDefinitions.CHE[1]); // Red skin and flesh, round, green stem, single inedible pit.

Upvotes: 2

lxe
lxe

Reputation: 7599

Use the .filter method:

Example using different data:

var matches = [ { foo : 'bar' }, { zoo : 'zar' } ].filter(function(item) {
  return item.hasOwnProperty('zoo');
});

console.log(matches[0]) // should be { zoo : 'zar' }

You also can just store your object like this:

var fruitDefinitions = {
     "APP" : [ "Apple",  "Red, green or yellow skin, round, white flesh, hard core with seeds." ] ,
     "BAN" : [ "Banana", "Thick yellow skin, elongated curved cylinder tapered at both ends, white flesh." ] ,
     "CHE" : [ "Cherry", "Red skin and flesh, round, green stem, single inedible pit." ] ,
     "KIW" : [ "Kiwi",   "Brown furry skin, oval, green flesh, many small edible seeds." ] ,
};

so you can access your data like this: fruitDefinitions['BAN']

Upvotes: 1

Related Questions