简体   繁体   English

尝试从对象数组中访问javascript函数

[英]Trying to access javascript function from within array of objects

Basically, I'm wanting to output the rating of these guitars but I get a "TypeError: taylorGuitar.guitarRating is not a function. Here's the code: 基本上,我想输出这些吉他的等级,但是会收到“ TypeError:taylorGuitar.guitarRating不是函数。这是代码:

    var taylorGuitar = [


{
    "model": "814ce",
    "stringsCount": 6,
    "pickup": true,
    "stringsTuning": ["E", "A", "D", "G", "B", "E"],
    "playabilityRating": 8,
    "soundRating": 10,
    "lookRating": 10,
    "woods": {
        "front": "Cedar",
        "back": "Rosewood",
        "fretboard": "Rosewood"
             }
},
{
    "model": "410ce",
    "stringsCount": 6,
    "pickup": true,
    "stringsTuning": ["E", "A", "D", "G", "B", "E"],
    "playabilityRating": 8,
    "soundRating": 9,
    "lookRating": 8,
    "woods": {
        "front": "Cedar",
        "back": "Rosewood",
        "fretboard": "Rosewood"
             }
},
{
        "guitarRating":  function(){
        totalScore = taylorGuitar.playabilityRating + taylorGuitar.soundRating + taylorGuitar.lookRating;
        return totalScore;
        }
}



];

var rating = taylorGuitar.guitarRating();
console.log(rating);

Your object taylorGuitar is an array so you would need to include which element you want like so: 您的对象taylorGuitar是一个数组,因此您需要包括所需的元素,如下所示:

taylorGuitar[0].model;

You have also defined your function as part of that array which will make it difficult to access, your taylorGuitar object needs to have two elements, your array and the function: 您还已将函数定义为该数组的一部分,这将使其难以访问,您的taylorGuitar对象需要具有两个元素,即数组和函数:

taylorGuitar.dataArray = [...]
taylorGuitar.guitarRating = function() {...}

Then you need to change your function so that it loops through all items in the array. 然后,您需要更改函数,以使其遍历数组中的所有项。

EDIT: 编辑:

I have rewritten your code so that it now works and added comments at the critical points: 我已经重写了您的代码,使其可以正常工作并在关键点添加了注释:

var taylorGuitar = {"data": [
{
  "model": "814ce",
  "stringsCount": 6,
  "pickup": true,
  "stringsTuning": ["E", "A", "D", "G", "B", "E"],
  "playabilityRating": 8,
  "soundRating": 10,
  "lookRating": 10,
  "woods": 
  {
     "front": "Cedar",
     "back": "Rosewood",
     "fretboard": "Rosewood"
  }
},
{
  "model": "410ce",
  "stringsCount": 6,
  "pickup": true,
  "stringsTuning": ["E", "A", "D", "G", "B", "E"],
  "playabilityRating": 8,
  "soundRating": 9,
  "lookRating": 8,
  "woods": 
  {
    "front": "Cedar",
    "back": "Rosewood",
    "fretboard": "Rosewood"
  }
}], //note array ends
"guitarRating":  function()
{
  totalScore = 0;
  //note you need to loop through the data
  for(i=0;i<this.data.length;i++)
  {
    d = this.data[i];
    totalScore += d.playabilityRating + d.soundRating + d.lookRating;
  }
  return totalScore;
}
};

var rating = taylorGuitar.guitarRating();
console.log(rating);

taylorGuitar.guitarRating is not a function nor is it defined actually. taylorGuitar.guitarRating不是函数,也没有实际定义。

taylorGuitar is an array that has three objects in your case: taylorGuitar是一个数组,在您的情况下有三个对象:

[{/*omitted stuff for object 1 */}, {/*omitted stuff for object 2 */}, {/*omitted stuff for object 3 */}].

var object3 = taylorGuitar[2];
//object 3 now is the one:
{
    "guitarRating":  function(){
    totalScore = taylorGuitar.playabilityRating + taylorGuitar.soundRating + taylorGuitar.lookRating;
    return totalScore;
    }
}

The last object is the object that contains 'guitarRating' 最后一个对象是包含“ guitarRating”的对象

So just like @elclanrs said, taylorGuitar[2].guitarRating this is the function that you can invoke using (). 因此,就像@elclanrs所说的那样, taylorGuitar[2].guitarRating这是可以使用()调用的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM