Neal
Neal

Reputation: 167

Creating and accessing array of objects in Javascript

I'm having trouble figuring out how to access data in an array of objects. I've spent hours trying to find some example of this, but all I find is you must reference arrays by an index number, which doesn't seem efficient.

For example, I have a table of animals and the number of legs on that animal. How do I access the value (number of legs) for that animal based on the name of the animal. If I pass "human" to a function I want to be able to return "2".

Is this concept called something I'm not yet familiar with yet? Is it just not possible to use a "key" to access data in an array? Do I really have to use a loop to search through the entire array to fine the right entry if I don't know the index number?

What is the simplest way to do this?

let animalsLegs = [{animal: "human", legs: 2},
                   {animal: "horse", legs: 4},
                   {animal: "fish", legs: 0}]

function findLegs(animalToFind) {
  return animalLegs[animalToFind];
}

console.log(findLegs("human"));

I would expect an output of 2.

Upvotes: 0

Views: 181

Answers (3)

Fortuna
Fortuna

Reputation: 609

I think an array is best for this kind of data, you simply have to write a small function to retrieve what you want.

const animals = [
  { animal: "human",  legs: 2 },
  { animal: "horse",  legs: 4 },
  { animal: "fish",   legs: 0 }
]

function getAnimalByName(name) {
  return animals.reduce((a, b) => b.animal !== name ? a : b, null)
}

// Human?!
console.log(getAnimalByName('fish'))

Upvotes: 1

Elias Garcia
Elias Garcia

Reputation: 7282

If you want to keep the Array data structure:

function findLegs(animalToFind) {
  const animal = animalsLegs.find(animal => animal.name === animalToFind);

  return animal.legs;
}

PS: An array data structure is more inefficient than an object, it will take O(n) to find an item, while an object is like a hash table, you can find values in O(1). You can read more about Big O notation here.

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 370729

Use an object instead of an array:

const animalLegsByAnimalName = {
  human: 2,
  horse: 4,
  fish: 0
};
function findLegs(animalToFind) {
  return animalLegsByAnimalName[animalToFind];
}
console.log(findLegs("human"));

If you want to keep using the array, but also use an object for quick, easy lookup, just reduce the initial array into the above object first:

const animalsLegs = [{animal: "human", legs: 2},
                   {animal: "horse", legs: 4},
                   {animal: "fish", legs: 0}];
const animalLegsByAnimalName = animalsLegs.reduce((a, { animal, legs }) => {
  a[animal] = legs;
  return a;
}, {});

function findLegs(animalToFind) {
  return animalLegsByAnimalName[animalToFind];
}
console.log(findLegs("human"));

If the object in the array is more complicated than that, and you (for example) want access to additional properties, you can have the object's values be the object in the array, instead of just the value of the leg property:

const animalsLegs = [{animal: "human", legs: 2},
                   {animal: "horse", legs: 4},
                   {animal: "fish", legs: 0}];
const animalLegsByAnimalName = animalsLegs.reduce((a, animalObj) => {
  a[animalObj.animal] = animalObj;
  return a;
}, {});

function findLegs(animalToFind) {
  const foundAnimal = animalLegsByAnimalName[animalToFind];
  if (!foundAnimal) {
    return 'No animal found with that name!';
  }
  return foundAnimal.legs;
}
console.log(findLegs("human"));

Upvotes: 1

Related Questions