简体   繁体   English

使用Vanilla JavaScript搜索对象数组

[英]Search Array of Objects Using Vanilla JavaScript

I'm 9 weeks into learning to program and I'm attempting to get into Hack Reactor. 我距离学习编程已有9周的时间,并且正尝试进入Hack Reactor。 Currently I've hit a complete wall with a website I'm building and was hoping you might be able to help me (several days of testing and combing the internet - my previous strategy for getting around blocks - has proven fruitless). 目前,我在建立的网站上碰壁了很多,希望您能对我有所帮助(经过数天的测试和整理互联网-我以前的绕过街区策略-已证明是徒劳的)。

In summary, I have an array of objects, each object is a teaching strategy which contains five properties. 总之,我有一个对象数组,每个对象都是一个包含五个属性的教学策略。 My goal is to add a search function that iterates over every object looking for keywords so a teacher could search the array of strategies. 我的目标是添加一个搜索功能,该功能可以遍历每个对象以寻找关键字,以便教师可以搜索一系列策略。 Currently I'm using the .map method to iterate a function over each object that checks the "tags" property for the keyword but for some reason or another I can't get the function to access "tags". 当前,我正在使用.map方法在检查“ tags”属性的每个对象上迭代一个函数,但是由于某种原因或其他原因,我无法访问该函数。

I've created a JSFiddle for you to check out: https://jsfiddle.net/jnemec/4ps8rLLx/ 我创建了一个JSFiddle供您签出: https ://jsfiddle.net/jnemec/4ps8rLLx/

var myIdeas = [

   {name: "Choral",
   image: "img/xideas/choral.jpg",
   grade: [1, 9, 9],
   theory: "Theory:" + "<br>" + "Coming Soon.",
   tags: ["CFU", "literacy"]},

   {name: "Highlighters",
   image: "img/xideas/highlighters.jpg",
   grade: [2, 2, 3],
   theory: "Theory:" + "<br>" + "Coming Soon.",
   tags: ["manipulative", "differentiation"]},

];

var input = "literacy"

var output = myIdeas.map(function(input) {
   if (input == "tags") {
      output += "name";
   } else {
      return "no matches, please try again";}
});

Last thing - Hack Reactor requires my project use only vanilla JavaScript (no frameworks - which from what I've read would make this task easier). 最后一件事-Hack Reactor要求我的项目仅使用原始JavaScript(没有框架-从我阅读的内容来看,这会使此任务更加容易)。

Unless you know why you're doing it, in this case you shouldn't be map() 'ing, you should be filter() 'ing. 除非您知道为什么要这么做,否则在这种情况下,您不应该map() 'ing,而应该在filter() 'ing。 The way you have this written right now isn't making good use of map() -- you might as well be using forEach() . 您现在编写的方式没有充分利用map() -您可能还需要使用forEach() And what you're returning from the callback doesn't make sense and isn't going to be accessible in a sensible way to detect and report to the user (although you could just use the value of output to detect the same condition). 从回调中返回的内容没有意义,也无法以明智的方式进行访问以检测并向用户报告(尽管您可以只使用output的值来检测相同的情况)。

Read about map() , filter() , and forEach() and think about how they apply to this situation and what kind of output you need. 阅读有关map()filter()forEach()并思考它们如何应用于这种情况以及需要哪种输出。

In each invocation of the callback input is the object, so if you want to access the tags it'd be input.tags . 在每次调用回调input ,对象都是对象,因此,如果要访问标签,将输入input.tags If you want to access the outer input (your search term) you need to name one of them differently. 如果要访问外部input (您的搜索词),则需要使用不同的名称。

var input = "literacy";

// `input` here declares a variable name that will be set to the current
// element of the array during each invocation of the callback.
var output = myIdeas.map(function(input) {
  // `input` now refers to the argument to this function. You've shadowed the
  // outer var of the same name and can no longer access it.
  // `input` is now an object from the array. If you want to access a prop like
  // `tags`, it's `input.tags`;
  if (input == "tags") {
    output += "name";
  } else {
    return "no matches, please try again";}
});

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

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