English 中文(简体)
如何在一系列扼杀装置内搜寻护卫
原标题:How to search for a string inside an array of strings

After searching for an answer in other posts, I felt I have to ask this. I looked at How do I check if an array includes an object in JavaScript? and Best way to find if an item is in a JavaScript array? and couldn t get the code there to work.

我正在把第html号编码纳入一个阵列,因此每个项目都属于第html号。

例如:

i[0]= <param name="bgcolor" value="#FFFFFF" /> 
i[1]= <param name="width" value="640" /> 
i[2]= <param name="height" value="360" />    
i[3]= more html code  

我想搜索这个阵列,并找到一个字高点。

理想的情况是,我要写出一份职务,把高点的项目列入议程。

幸运的是,这一阵列没有重复,因此只有一次发生。

我获得虚假的回报。

任何想法?

最佳回答

它简单易行,因为它使阵列变得更迭,并寻求实行管制。

function searchStringInArray (str, strArray) {
    for (var j=0; j<strArray.length; j++) {
        if (strArray[j].match(str)) return j;
    }
    return -1;
}

<<>Edit>/strong> - 成为str<>/code>的论据。

问题回答

You can use Array.prototype.find function in javascript. Array find MDN.

因此,为了在一系列扼杀中找到证据,该法典变得非常简单。 加上执行浏览器,它将提供良好的业绩。

页: 1

var strs = [ abc ,  def ,  ghi ,  jkl ,  mno ];
var value =  abc ;
strs.find(
    function(str) {
        return str == value;
    }
);

或使用《玛尔布达宣言》

var strs = [ abc ,  def ,  ghi ,  jkl ,  mno ];
var value =  abc ;
strs.find((str) => str === value);

1. 扩大你与以下事项相关的职能:

containsRegex(a, regex){
  for(var i = 0; i < a.length; i++) {
    if(a[i].search(regex) > -1){
      return i;
    }
  }
  return -1;
}

接着,你用一系列的体力和reg子来形容这一功能,就你而言,要看高度:

containsRegex([  <param name="bgcolor" value="#FFFFFF" /> ,  sdafkdf  ], /height/)

此外,如果发现高点,你还可以返回指数:

containsRegex(a, regex){
  for(var i = 0; i < a.length; i++) {
    int pos = a[i].search(regex);
    if(pos > -1){
      return [i, pos];
    }
  }
  return null;
}

简单和大法典,支持多语

let strArray = ["Please locate where  locate  occurs!","Hi Hashem it","do it","for free"];

strArray.filter(word=> word.includes("it"));
Output
//Hi Hashem it,do it

//-------------------

strArray.filter(word=> word.includes("do it"));
Output
//do it

如果有人想进行某种动态的搜索,则会发生这种情况。

 let searchInArray=(searchQuery, array, objectKey=null)=>{

  return array.filter(d=>{
      let data =objectKey? d[objectKey] : d //Incase If It s Array Of Objects.
       let dataWords= typeof data=="string" && data?.split(" ")?.map(b=>b&&b.toLowerCase().trim()).filter(b=>b)
      let searchWords = typeof searchQuery=="string"&&searchQuery?.split(" ").map(b=>b&&b.toLowerCase().trim()).filter(b=>b)

     let matchingWords = searchWords.filter(word=>dataWords.includes(word))

    return matchingWords.length

})
    
    
}

a 阵列:

let arrayOfStr = [
  "Search for words",
  "inside an array",
  "dynamic searching",
  "match rate 90%"
]

searchInArray("dynamic search", arrayOfStr)

//Results: [ "Search for words", "dynamic searching" ]

物体:

let arrayOfObject = [
  {
    "address": "Karachi Pakistan"
  },
  {
    "address": "UK London"
  },
  {
    "address": "Pakistan Lahore"
  }
]

searchInArray("Pakistan", arrayOfObject,"address")

//Results: [ { "address": "Karachi Pakistan" }, { "address": "Pakistan Lahore" } ]

var newArr= [];
function searchStringInArray (str, strArray) {
       newArr = [];
    strArray.map(item => {
        str.includes(item) ? newArr.push(item) :   ;
    });
  return newArr;
}

var result = searchStringInArray( Definitely",he said in a matter-of-fact tone. , [ matter ,  definitely ]);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

在一阵列内搜寻,你可以简单地使用Iteration methods(每个/地图),并在使用时使用。 Java 搜索方法

参考。 https://www.w3schools.com/jsref/jsref_search.asp

<><>Example>

const arrayOfLists = [ ape ,  apple ,  auto ];
const searchString = "A"

let matchingStrings = [];

arrayOfLists.forEach((list) => {
    if (list.toLocaleLowerCase().search(searchString.toLocaleLowerCase()) > -1) {
        matchingStrings.push(list)
    }
})

<>平坦>阵列将列出所有可能发生事件的清单





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.