JavaScript Accessing objects accessing objects in array javascript,accessing objects js,accessing objects

I wrote a simple function to access some values in the array below, and nested in the array are multiple objects.

My question has to do with my 2 loops. I understand that I must initially loop through the array to gain access to the 3 objects, but why must I loop through the 3 objects before I can access the values?

Basically why can't I run an initial for loop and access a sound by doing this:

animalNoises[i][animal][country]

the above returns as undefined for me. Does it have something to do with the way each 3 animal objects are structured?

Thanks for the help. I really appreciate the stack overflow community for the constant help.

function petSounds(animal, country) {
let phrase = ''
for (let i = 0; i < animalNoises.length; i++) {
  for (let key in animalNoises[i]){
    if (animal === key){
      let sound = animalNoises[i][key][country];
      phrase = animal + 's' + ' in ' + country + ' say ' + sound
      }
    }
  }
 return phrase
}

    let animalNoises = [
  { 'dog': {
    'America' : 'Woof! Woof!',
    'Germany' : 'Wau Wau!',
    'England' : 'Bow wow!',
    'Uruguay' : 'Jua jua!',
    'Afrikaans' : 'Blaf!',
    'Korea' : 'Mong mong!',
    'Iceland' : 'Voff voff!',
    'Albania' : 'Ham!',
    'Algeria' : 'Ouaf ouaf!'
    }
  },
  { 'cat': {
    'America' : 'Meow',
    'Germany' : 'Miauw!',
    'England' : 'mew mew',
    'Uruguay' : 'Miau Miau!',
    'Afrikaans' : 'Purr',
    'Korea' : 'Nyaong!',
    'Iceland' : 'Kurnau!',
    'Albania' : 'Miau',
    'Algeria' : 'Miaou!'
    }
  },
  { 'chicken': {
    'America' : 'Cluck cluck',
    'Germany' : 'tock tock tock',
    'England' : 'Cluck Cluck',
    'Uruguay' : 'gut gut gdak',
    'Afrikaans' : 'kukeleku',
    'Korea' : 'ko-ko-ko',
    'Iceland' : 'Chuck-chuck!',
    'Albania' : 'Kotkot',
    'Algeria' : 'Cotcotcodet'
    }
  }
];
Answer:1

You need to update your json to simplify function (use an object keyed by animal type):

function petSounds(animal, country) {
  const sound = animalNoises[animal][country];
  const phrase = animal + 's' + ' in ' + country + ' say ' + sound;
  return phrase;
}

let animalNoises = {
  'dog': {
    'America': 'Woof! Woof!',
    'Germany': 'Wau Wau!',
    'England': 'Bow wow!',
    'Uruguay': 'Jua jua!',
    'Afrikaans': 'Blaf!',
    'Korea': 'Mong mong!',
    'Iceland': 'Voff voff!',
    'Albania': 'Ham!',
    'Algeria': 'Ouaf ouaf!'

  },
  'cat': {
    'America': 'Meow',
    'Germany': 'Miauw!',
    'England': 'mew mew',
    'Uruguay': 'Miau Miau!',
    'Afrikaans': 'Purr',
    'Korea': 'Nyaong!',
    'Iceland': 'Kurnau!',
    'Albania': 'Miau',
    'Algeria': 'Miaou!'
  },
  'chicken': {
    'America': 'Cluck cluck',
    'Germany': 'tock tock tock',
    'England': 'Cluck Cluck',
    'Uruguay': 'gut gut gdak',
    'Afrikaans': 'kukeleku',
    'Korea': 'ko-ko-ko',
    'Iceland': 'Chuck-chuck!',
    'Albania': 'Kotkot',
    'Algeria': 'Cotcotcodet'
  }
};

console.log(petSounds('cat', 'America'));
console.log(petSounds('dog', 'Iceland'));
console.log(petSounds('chicken', 'Germany'));
Answer:2

Your solution works, with single for loop. You missed specifying single quotes. When I simply console this it returns specific value.

animalNoises[0]['dog']['Korea']
"Mong mong!"

In loop you need to access from your array by specifying key name and country values.

It doesn't have anything to do with the way each 3 animal objects are structured

When I call your function it is running fine.

petSounds('dog', 'Korea')
"dogs in Korea say Mong mong!"

Or you can modify your function as below which does same reducing inner loop:

function petSounds(animal, country) {
let phrase = ''
  for (let i = 0; i < animalNoises.length; i++) {
    let key = Object.keys(animalNoises[i])[0];
    if (animal === key){
      let sound = animalNoises[i][key][country];
      phrase = animal + 's' + ' in ' + country + ' say ' + sound
    }
  }
  return phrase
}
Answer:3

I am new in php and i want to do capthca ... I wrote the code, validation etc , and as a result i get only small picture ... and nothing on it ... and my program dont tell me where is an error This ...

I am new in php and i want to do capthca ... I wrote the code, validation etc , and as a result i get only small picture ... and nothing on it ... and my program dont tell me where is an error This ...

In our system we have so far been using Keycloak 3.4.3 and the Script Authenticator authentication provider functionality as part of the browser based authentication. Now, after upgrading to Keycloak ...

In our system we have so far been using Keycloak 3.4.3 and the Script Authenticator authentication provider functionality as part of the browser based authentication. Now, after upgrading to Keycloak ...

  1. keycloak script authenticator
  2. keycloak script authenticator example
  3. keycloak script authenticator missing
  4. keycloak script authentication flow
  5. keycloak enable script authenticator
  6. keycloak script based authenticator
  7. keycloak script based authentication

I have a list of the simple objects (containing only names) in my model: var list = [{name: "Jane"}, {name: "Mary"}]; Here I display them: <table> <tr data-ng-repeat="pers in list | filter:...

I have a list of the simple objects (containing only names) in my model: var list = [{name: "Jane"}, {name: "Mary"}]; Here I display them: <table> <tr data-ng-repeat="pers in list | filter:...

  1. angularjs directive with parameters
  2. angularjs directive with controller
  3. angularjs directive with template
  4. angularjs directive with ng-model
  5. angularjs directive with typescript
  6. angularjs directive with controller and link
  7. angularjs directive with attribute
  8. angularjs directive with html template
  9. angularjs directive with scope
  10. angularjs directive with templateurl
  11. angularjs directive with input
  12. angularjs directive with dynamic template
  13. angularjs directive with params
  14. angularjs directive with arguments
  15. angularjs directive with service
  16. angularjs directive with controller example
  17. angularjs directive with value
  18. angularjs directive with css
  19. angularjs directive with ng-repeat
  20. angularjs directive with function

I have this simple class : class myCustomClass{ foo(value){ //do some stuff } faa(){ //do some stuff $.getJSON( someUrl, function(data) { // how ...

I have this simple class : class myCustomClass{ foo(value){ //do some stuff } faa(){ //do some stuff $.getJSON( someUrl, function(data) { // how ...

  1. javascript from ajax response
  2. javascript ajax from json
  3. execute javascript from ajax response jquery
  4. execute javascript from ajax response
  5. javascript return from ajax
  6. call javascript from ajax
  7. load javascript from ajax
  8. javascript array from ajax
  9. javascript variable from ajax
  10. javascript validation from ajax
  11. javascript pdf from ajax
  12. jquery javascript from ajax
  13. javascript ajax
  14. javascript ajax post
  15. javascript ajax request
  16. javascript ajax example
  17. javascript ajax get
  18. javascript ajax post json
  19. javascript ajax file upload example
  20. javascript ajax get json