简体   繁体   English

在对象中搜索单词并返回结果

[英]Search object for a word and return results

I have basic object and I want to search the title and description values and return the found objects. 我有基本对象,我想搜索标题和描述值并返回找到的对象。 After your help, I can output them in HTML. 在您的帮助之后,我可以将它们输出为HTML。 I currently get a syntax error. 我目前收到语法错误。

In the example I want to return all objects that contain the search term "dog" 在示例中,我想返回所有包含搜索项“ dog”的对象

var search_term = "dog";
var search_results = [];
var page_json = [{
  "page_title": "Display",
  "page_url": "/display/",
  "page_description": "We display stuff. search this description for the word dog.",
}, {
  "page_title": "DRAM",
  "page_url": "/dram/",
  "page_description": "stuff about dram here",
}, {
  "page_title": "TVs",
  "page_url": "/tv/",
  "page_description": "stuff about tvs here",
},];

$(page_json).each(function(i, value) {
  //console.log( value.page_description );
  // search for our term in the description AND title values
  if ($(value.page_description).contains(search_term)) {
    console.log(value);
    // put that object into search_results array
  }
});

The answer I went with... 我的答案...

var search_term = "dog";
var search_results = [];
var page_json = [{
        "page_title": "Display",
        "page_url": "/display/",
        "page_description": "search this description for the word dog.",
    },{
        "page_title": "this title has a dog",
        "page_url": "/dram/",
        "page_description": "stuff about dram here",
    },{
        "page_title": "TVs",
        "page_url": "/tv/",
        "page_description": "stuff about tvs here",
    },
];

var search = function(search_term) {
    return page_json.filter(function(item) {
        // if search term is inside the title OR description
        if (item.page_description.indexOf(search_term) !== -1 || item.page_title.indexOf(search_term) !== -1){
            return item;
        }
    })
}

search_results = search(search_term);

console.log(search_results);

I'm not sure how this's done with jquery, but here's how you'd do it in JS: 我不确定如何使用jquery进行此操作,但是这是您在JS中执行的操作:

const foundObject = page_json.find(obj=> 
     obj.page_title.includes(search_term) ||
     obj.page_description.includes(search_term)
   )

console.log(foundObject) returns the desired output. console.log(foundObject)返回所需的输出。

If you want to do it with a plain for loop: 如果要使用普通的for循环执行此操作:

 let foundObject=null;
 for(let i=0; i < page_json.length; i++){
   const exists = page_json[i].page_title === search_term || page_json[i].page_description === search_term;
   if(exists) {
      foundObject = page_json[i]; // What you want.
   }
 }
if(foundObject) { // Do something.. }

Let me know if that isn't clear! 让我知道是否不清楚!

You do not need use Jquery here. 您不需要在这里使用Jquery。 Javascript have the filter function, which fits very well for this situation. Javascript具有过滤器功能,非常适合这种情况。

var search_term = "dog";
var search_results = [];
var page_json = [{
    "page_title": "Display",
    "page_url": "/display/",
    "page_description": "We display stuff. search this description for the word dog.",
},
{
    "page_title": "DRAM",
    "page_url": "/dram/",
    "page_description": "stuff about dram here",
},
{
    "page_title": "TVs",
    "page_url": "/tv/",
    "page_description": "stuff about tvs here",
},
];

search = function(search_term, json) {
    return json.filter(function(i) {
        if (i.page_description.indexOf('about') !== -1) return i;
    })
}

search_results = search(search_term, page_json);

console.log(search_results);

For searching for the correct page_title you'd put the following: 为了搜索正确的page_title,您需要输入以下内容:

if(page_json[i]["page_title"] == 'whatever title' && page_json[i]["page_description"] == 'whatever description'){
//do something here
}

or 要么

  if(page_json[i]["page_title"] == 'whatever title'){ 
   //do something
  }
    if (page_json[i]["page_description"] == 'whatever description'){
    //do something here
    }

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

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