简体   繁体   English

我如何在不使用JavaScript的任何循环的情况下搜索元素

[英]How can i search for an element without using any loops in javascript

I have an array like [1,2,3,4,5,6,7] how can I search for an element without using any loops and built-in methods in javascript ? 我有一个类似[1,2,3,4,5,6,7]的数组,如何在不使用javascript使用任何循环和内置方法的情况下搜索元素?

Thanks in advance 提前致谢

You could use a recursive approach and rest parameters ... for the array, without using some array methods. 您可以使用递归方法并为数组使用rest参数... ,而无需使用某些数组方法。

 function find(search, array) { return function check(search, value, ...rest) { return search === value || rest.length && check(search, ... rest) || false; }(search, ...array); } console.log(find(1, [1, 2, 3, 4, 5, 6, 7])); console.log(find(42, [1, 2, 3, 4, 5, 6, 7])); 

You can use indexOf method to search for element. 您可以使用indexOf方法搜索元素。 I hope this will help you. 我希望这能帮到您。

 var array = [1,2,3,4,5,6,7]; var searchElement = 5; if(array.indexOf(searchElement)>-1) { console.log('element present in array'); } else { console.log('element not present in array'); } 

If you don't want to search all the elements. 如果您不想搜索所有元素。 First iterate through all the elements and construct a object. 首先遍历所有元素并构造一个对象。 The construction of object is one time process and use this object for searching an element. 对象的构建是一个一次性的过程,并使用该对象搜索元素。

 var array = [1,2,3,4,5,6,7]; var obj = {}; for(var i=0;i<array.length;i++) { if(obj.hasOwnProperty(array[i])) continue; obj[array[i]] = true; } var searchElement = 7; if(obj[searchElement] == true) { console.log("element present"); } else { console.log("element not present"); } 

The find() method returns the value of the first element in the array that satisfies the provided testing function. find()方法返回满足提供的测试功能的数组中第一个元素的值。 Otherwise undefined is returned. 否则返回undefined。

var array1 = [5, 12, 8, 130, 44];

var found = array1.find(function(element) {
  return element > 10;
});

console.log(found);

You cannot search for an element in O(1) , however you can check if an element exist in a Set : 您无法在O(1) search元素,但是可以检查Set是否存在元素:

const set = new Set(array);
if(set.has(3)) console.log("3 is in array");

It is possible, but you must have an JavaScript Object like this: 可能,但是您必须具有这样的JavaScript对象:

var Obj = {
    'a':1,
    'b':2,
    'c':3,
    'd':4,
    'e':5
};

Without search you can access to your element like this: 无需搜索,您可以像这样访问元素:

console.log(Obj['a']);

暂无
暂无

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

相关问题 如何在不使用任何mysql查询,仅使用简单的javascript或jquery的情况下在HTML表中进行搜索? - How can I search in a HTML table without using any mysql queries, just a plain javascript or jquery to be specific? 我可以在 JAVASCRIPT 中没有任何循环的 if 语句中使用 continue 和 break 吗? - Can i use continue and break in an if statement without any loops in JAVASCRIPT? 如何在不使用任何循环的情况下增加元素的宽度 - how to increase the width of an element without using any loops 如何在 javascript 中编写没有 for 循环的以下代码 - How can i write the following code without for loops in javascript 搜索和替换字符串而不使用正则表达式而是循环,如何一次替换多个单词? - Search and replacing strings without using Regex but loops instead, how can I replace more than one word at a time? 如何在不传递任何参数的情况下获取 HTML 中 JavaScript function 元素的 ID? - How can I get the ID of an HTML element in a JavaScript function without passing any parameters? 仅使用没有任何方法的循环将 arrays 合并到 javascript - Merge arrays in javascript using only loops without any methods 我如何在不使用任何服务器的情况下在 javascript 中导入 json 文件? - how i can i import json file in javascript without using any server? 有没有办法在使用 javascript 循环迭代时为每个 html 元素调用一个函数 - Is there a way I can call a function for each html element when iterated using javascript loops 如何在不使用任何外部API的情况下使javascript函数等待其他函数完成? - How can I make javascript function to wait for other function to finish without using any external APIs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM