试图制作可重用的javascript函数,但数据没有返回

时间:2017-08-09 11:34:11

标签: javascript csv d3.js

我想创建一个函数,当我调用它时,我可以给出一个性别,它会返回一个随机名称。问题是我希望它可以重复使用,这是我遇到困难的地方。

var Name = '';
var Gender = '';

var NameChild1(){
  var Child1Gender = 'Male'
  Child1Name = NameFunction(Child1Gender, Child1Name)
}

function NameFunction(Gender, Name) {
  var NameChance1 = Math.floor(Math.random() * 10) + 1;
  NameChance1 = NameChance1.toString();
  d3.csv('Names.csv', function(NameData) {
    var NameFilter = NameData.filter(function(d) {
      return (d.Rank == NameChance1)
    })
    if (Gender == 'Female') {
      Name = NameFilter[0]['NameFemale'];
      return (Name);
    } else if (Gender == 'Male') {
      return (Name);
    };
  });
};

如果我在其中执行console.log,那么名称过滤器在if语句中有效 - 但它不会返回过去。

有谁知道问题是什么?

此外,如果有帮助,这是CSV!

Rank,NameMale,NameFemale
1,Oliver ,Amelia 
2,Jack ,Olivia 
3,Harry ,Emily 
4,George ,Isla 
5,Jacob ,Ava 
6,Charlie ,Ella 
7,Noah ,Jessica 
8,William ,Isabella 
9,Thomas ,Mia 
10,Oscar ,Poppy

2 个答案:

答案 0 :(得分:1)

var Name = '';
var Gender = '';

var NameChild1(){
  var Child1Gender = 'Male'
  Child1Name = NameFunction(Child1Gender);
  return Child1Name;
}

function NameFunction(Gender) {
  var Name;
  var NameChance1 = Math.floor(Math.random() * 10) + 1;
  NameChance1 = NameChance1.toString();
  d3.csv('Names.csv', function(NameData) {
    var NameFilter = NameData.filter(function(d) {
      return (d.Rank.toString() == NameChance1)
    })
    if (Gender == 'Female') {
      Name = NameFilter[0]['NameFemale'];
      return (Name);
    } else if (Gender == 'Male') {
      Name = NameFilter[0]['NameMale'];
      return (Name);
    };
  });
};

答案 1 :(得分:1)

此函数将返回undefined。

function NameFunction(Gender, Name) {
  var NameChance1 = Math.floor(Math.random() * 10) + 1;
  NameChance1 = NameChance1.toString();
  d3.csv('Names.csv', function(NameData) {
    var NameFilter = NameData.filter(function(d) {
      return (d.Rank == NameChance1)
    })
    if (Gender == 'Female') {
      Name = NameFilter[0]['NameFemale'];
      return (Name);
    } else if (Gender == 'Male') {
      return (Name);
    };
  });
};

原因d3.csv(' Names.csv')是异步的。

所以你可以这样做:

var Name = '';
var Gender = '';
function NameFunction(Gender, Name, NameData) {
  var NameChance1 = Math.floor(Math.random() * 10) + 1;
  NameChance1 = NameChance1.toString();
    var NameFilter = NameData.filter(function(d) {
      return (d.Rank == NameChance1)
    })
    if (Gender == 'Female') {
      Name = NameFilter[0]['NameFemale'];
      return (Name);
    } else if (Gender == 'Male') {
      Name = NameFilter[0]['NameMale'];
      return (Name);
    };
  });
};
var NameChild1 = function(NameData){
  var Child1Gender = 'Male'
  Child1Name = NameFunction(Child1Gender, Child1Name, NameData)
  //you can do stuff with Child1Name
}
function loadMyCSV(){
  return d3.csv('Names.csv', function(NameData) {
    NameChild1(NameData);
  }
}
loadMyCSV();

坚果壳:

1您首先调用loadMyCSV()

1.1。如果loadMyCSV成功,将调用NameChild1并将完整解析的数据传递给函数。

1.2。 NameChild1将调用NameFunction,它将获取Child1Name。

希望这有帮助!