Javascript - 打印包含多个对象的数组中的所有名称[关闭]

Posted

技术标签:

【中文标题】Javascript - 打印包含多个对象的数组中的所有名称[关闭]【英文标题】:Javascript - print all names in array containing multiple objects [closed] 【发布时间】:2020-01-15 00:28:35 【问题描述】:

所以我是 javascript 和一般编程的新手,我在下面有一组对象。我正在尝试生成并打印一个数组,其中包含所有考试成绩为 80 或更高的学生的全名(名字和姓氏用“”分隔)。格式有点像 ['James Johnson'、'Stephanie Ottesen'、'Leonard Arvan'、'Beverly Mott'、'Beatrice Jaco']。

我已经创建了一个单独的数组,但是我无法弄清楚如何只访问数组中的某些对象并将它们添加到我的新数组中。通过谷歌搜索,我遇到了 .map()、.reduce() 和 .filter() 方法,但我正在努力弄清楚如何采用这些方法并将它们格式化到我的程序中。任何帮助或参考将不胜感激。

let students = [fname: "Jane", lname: "Brazier", snum: "100366942", agrade: 67.59127376966494, tgrade: 64.86530868914188, egrade: 70.52944558104066, fname: "Ricardo", lname: "Allen", snum: "100345641", agrade: 65.80370345301014, tgrade: 75.40211705841241, egrade: 55.39348896202821, fname: "Mary", lname: "Hernandez", snum: "100221207", agrade: 71.20761408935981, tgrade: 71.37529197926764, egrade: 75.82038980457698, fname: "James", lname: "Johnson", snum: "100200842", agrade: 72.5791318299902, tgrade: 81.65883679807183, egrade: 85.19664228946989, fname: "Stephanie", lname: "Ottesen", snum: "100225067", agrade: 88.19738810849226, tgrade: 84.68339894849353, egrade: 82.23947265645927, fname: "Martin", lname: "Conway", snum: "100358379", agrade: 71.28759059295344, tgrade: 79.13194908266965, egrade: 77.61880623797336, fname: "Andrew", lname: "Weaver", snum: "100376243", agrade: 70.01798139244363, tgrade: 78.64811561086252, egrade: 78.68650242850617, fname: "Rhonda", lname: "Ford", snum: "100296902", agrade: 56.14580882764524, tgrade: 63.9209865108888, egrade: 60.186613967770334, fname: "Leonard", lname: "Arvan", snum: "100220616", agrade: 80.67865525396981, tgrade: 92.73557717342663, egrade: 88.32126970338336, fname: "William", lname: "Culler", snum: "100307637", agrade: 65.75251699043244, tgrade: 62.18172136246404, egrade: 63.065185542933094, fname: "David", lname: "Nakasone", snum: "100353719", agrade: 62.63260239883763, tgrade: 58.352794766947866, egrade: 59.80461902691901, fname: "Maria", lname: "Young", snum: "100311331", agrade: 70.13767021264486, tgrade: 76.09348747016176, egrade: 79.99207130929622, fname: "Beverly", lname: "Mott", snum: "100325579", agrade: 83.08140516644137, tgrade: 94.80666640692787, egrade: 85.15875656837004, fname: "Patrick", lname: "Francis", snum: "100257773", agrade: 66.79534616079296, tgrade: 47.744928296560076, egrade: 64.05723052865763, fname: "Tracy", lname: "Bonds", snum: "100233277", agrade: 70.2289028670531, tgrade: 65.32258294210156, egrade: 77.04816321925091, fname: "Richard", lname: "Akers", snum: "100216705", agrade: 52.446722363991015, tgrade: 49.205597783687374, egrade: 53.72940974941982, fname: "Beatrice", lname: "Jaco", snum: "100233935", agrade: 81.89338938644417, tgrade: 71.05459078971688, egrade: 83.08235397281308, fname: "Guy", lname: "Wendelin", snum: "100336379", agrade: 68.17788319655493, tgrade: 63.82273085525137, egrade: 68.31559946786807, fname: "Logan", lname: "Olsen", snum: "100265736", agrade: 59.89037739094347, tgrade: 71.76550299333657, egrade: 64.61665695830132, fname: "Gene", lname: "Jeanlouis", snum: "100341666", agrade: 74.16481515505846, tgrade: 68.20592386917109, egrade: 78.25975050135006]

let studentNames = [];

【问题讨论】:

为什么不选择一个,比如Array#filter 并查看描述和示例。如果您有问题,请尝试并添加问题。 你试过什么?至少给我们看一个例子,否则我们只会假设你要求我们为你做功课。 如果您坚持实施也没关系,但请将您的努力发布到问题中。你甚至不需要过滤、映射或减少。一个简单的for 循环遍历数组 -> 检查if (array[i].grade > 80) -> push 输出数组的全名。 【参考方案1】:
    仅提取具有Array.filter 的80 或更高年级的学生。 使用Array.map 将剩余学生映射到新格式。

结果:

let students = [
  fname: "Jane",
  lname: "Brazier",
  snum: "100366942",
  agrade: 67.59127376966494,
  tgrade: 64.86530868914188,
  egrade: 70.52944558104066
, 
  fname: "Ricardo",
  lname: "Allen",
  snum: "100345641",
  agrade: 65.80370345301014,
  tgrade: 75.40211705841241,
  egrade: 55.39348896202821
, 
  fname: "Mary",
  lname: "Hernandez",
  snum: "100221207",
  agrade: 71.20761408935981,
  tgrade: 71.37529197926764,
  egrade: 75.82038980457698
, 
  fname: "James",
  lname: "Johnson",
  snum: "100200842",
  agrade: 72.5791318299902,
  tgrade: 81.65883679807183,
  egrade: 85.19664228946989
, 
  fname: "Stephanie",
  lname: "Ottesen",
  snum: "100225067",
  agrade: 88.19738810849226,
  tgrade: 84.68339894849353,
  egrade: 82.23947265645927
, 
  fname: "Martin",
  lname: "Conway",
  snum: "100358379",
  agrade: 71.28759059295344,
  tgrade: 79.13194908266965,
  egrade: 77.61880623797336
, 
  fname: "Andrew",
  lname: "Weaver",
  snum: "100376243",
  agrade: 70.01798139244363,
  tgrade: 78.64811561086252,
  egrade: 78.68650242850617
, 
  fname: "Rhonda",
  lname: "Ford",
  snum: "100296902",
  agrade: 56.14580882764524,
  tgrade: 63.9209865108888,
  egrade: 60.186613967770334
, 
  fname: "Leonard",
  lname: "Arvan",
  snum: "100220616",
  agrade: 80.67865525396981,
  tgrade: 92.73557717342663,
  egrade: 88.32126970338336
, 
  fname: "William",
  lname: "Culler",
  snum: "100307637",
  agrade: 65.75251699043244,
  tgrade: 62.18172136246404,
  egrade: 63.065185542933094
, 
  fname: "David",
  lname: "Nakasone",
  snum: "100353719",
  agrade: 62.63260239883763,
  tgrade: 58.352794766947866,
  egrade: 59.80461902691901
, 
  fname: "Maria",
  lname: "Young",
  snum: "100311331",
  agrade: 70.13767021264486,
  tgrade: 76.09348747016176,
  egrade: 79.99207130929622
, 
  fname: "Beverly",
  lname: "Mott",
  snum: "100325579",
  agrade: 83.08140516644137,
  tgrade: 94.80666640692787,
  egrade: 85.15875656837004
, 
  fname: "Patrick",
  lname: "Francis",
  snum: "100257773",
  agrade: 66.79534616079296,
  tgrade: 47.744928296560076,
  egrade: 64.05723052865763
, 
  fname: "Tracy",
  lname: "Bonds",
  snum: "100233277",
  agrade: 70.2289028670531,
  tgrade: 65.32258294210156,
  egrade: 77.04816321925091
, 
  fname: "Richard",
  lname: "Akers",
  snum: "100216705",
  agrade: 52.446722363991015,
  tgrade: 49.205597783687374,
  egrade: 53.72940974941982
, 
  fname: "Beatrice",
  lname: "Jaco",
  snum: "100233935",
  agrade: 81.89338938644417,
  tgrade: 71.05459078971688,
  egrade: 83.08235397281308
, 
  fname: "Guy",
  lname: "Wendelin",
  snum: "100336379",
  agrade: 68.17788319655493,
  tgrade: 63.82273085525137,
  egrade: 68.31559946786807
, 
  fname: "Logan",
  lname: "Olsen",
  snum: "100265736",
  agrade: 59.89037739094347,
  tgrade: 71.76550299333657,
  egrade: 64.61665695830132
, 
  fname: "Gene",
  lname: "Jeanlouis",
  snum: "100341666",
  agrade: 74.16481515505846,
  tgrade: 68.20592386917109,
  egrade: 78.25975050135006
];

const result = students
  .filter(student => student.egrade >= 80)
  .map(student => `$student.fname $student.lname`);
  
console.log(result);

【讨论】:

以上是关于Javascript - 打印包含多个对象的数组中的所有名称[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript -- 内置对象数组

如何用JavaScript替换数组中间隔多个参数

html 在Javascript和打印结果中过滤对象数组

比较数组中的多个对象,并在某个公共值JavaScript / Vue.js时求和值

html 在Javascript和打印结果中的对象数组中过滤(使用箭头功能)

在 JavaScript 中多次重复包含多个元素的数组