检查对象数组中的对象中是否存在值

时间:2022-03-06 21:20:29

I am trying to write the code of following algorithm

我正在尝试编写以下算法的代码

  • I have an array of active_id (array)
  • 我有一个active_id数组(数组)
  • ID coming from url (string)
  • 来自网址的ID(字符串)
  • if value of ID does not exist in active_id array
    • run the function A()
    • 运行函数A()
  • 如果在active_id数组中不存在ID值,则运行函数A()
  • else do nothing.
  • 别无所求。

Note - function A() should be run only once.

注意 - 函数A()应该只运行一次。

I tried writing the code

我尝试编写代码

for (var i = 0; i < activeIds.length; i++) {
    if (activeIds[i] != uid) {
         A();  //This is running multiple times.
    }

I tried using while loop

我尝试使用while循环

var i = 0;
while (activeIds[i] != uid) {
     A();  //This is running multiple times again.
    i++;
}

There is something i am missing. not able to figure it out.

有一些我想念的东西。无法弄清楚。

5 个解决方案

#1


3  

You can just use indexOf function which will return -1 if element is not exist on array and positive number start 0 till array (length -1) if element exist:

您可以使用indexOf函数,如果元素不存在于数组上,则返回-1,如果元素存在,则正数开始0到数组(长度-1):

if (activeIds.indexOf(uid) == -1) {
    A();  
}

function A(); 

#2


2  

You can use indexOf, like this:

您可以使用indexOf,如下所示:

if( activeIds.indexOf(id) < 0 ) A();

#3


1  

If you want to invoke function A() only if the a particular ID (uid) does not exist in your array activeIds, you may want to alter your loop approach with this:

如果只想在数组activeIds中不存在特定ID(uid)时调用函数A(),则可能需要使用以下方法更改循环方法:

if (activeIds.filter(function(n){ return n===uid }).length==0){
    A();
}

Where you have a definition of function A() ready to use already.

你已经准备好使用函数A()的定义了。

Side note You syntax with function A(){} is just defining the function A but it's not going to run it. If you want to define and run it once, you may do:

旁注你的函数A(){}的语法只是定义函数A但它不会运行它。如果要定义并运行一次,您可以执行以下操作:

(function A(){
   // logic goes here
})();

#4


1  

You can use array.indexof() function in order to find the value. It will look something like that:

您可以使用array.indexof()函数来查找值。它看起来像这样:

if(activeIds.indexOf(uid) === -1){
    A();
}

#5


0  

Try this, code.

试试这个,代码。

var i=0;
var isMatchFound = false;

while (activeIds.length >= i) {
  if(activeIds[i] ==uid){
    isMatchFound = true;
    break;
}
 i++;
}

if(isMatchFound){
   //Call Function A
    A();
}

Hope this will help

希望这会有所帮助

#1


3  

You can just use indexOf function which will return -1 if element is not exist on array and positive number start 0 till array (length -1) if element exist:

您可以使用indexOf函数,如果元素不存在于数组上,则返回-1,如果元素存在,则正数开始0到数组(长度-1):

if (activeIds.indexOf(uid) == -1) {
    A();  
}

function A(); 

#2


2  

You can use indexOf, like this:

您可以使用indexOf,如下所示:

if( activeIds.indexOf(id) < 0 ) A();

#3


1  

If you want to invoke function A() only if the a particular ID (uid) does not exist in your array activeIds, you may want to alter your loop approach with this:

如果只想在数组activeIds中不存在特定ID(uid)时调用函数A(),则可能需要使用以下方法更改循环方法:

if (activeIds.filter(function(n){ return n===uid }).length==0){
    A();
}

Where you have a definition of function A() ready to use already.

你已经准备好使用函数A()的定义了。

Side note You syntax with function A(){} is just defining the function A but it's not going to run it. If you want to define and run it once, you may do:

旁注你的函数A(){}的语法只是定义函数A但它不会运行它。如果要定义并运行一次,您可以执行以下操作:

(function A(){
   // logic goes here
})();

#4


1  

You can use array.indexof() function in order to find the value. It will look something like that:

您可以使用array.indexof()函数来查找值。它看起来像这样:

if(activeIds.indexOf(uid) === -1){
    A();
}

#5


0  

Try this, code.

试试这个,代码。

var i=0;
var isMatchFound = false;

while (activeIds.length >= i) {
  if(activeIds[i] ==uid){
    isMatchFound = true;
    break;
}
 i++;
}

if(isMatchFound){
   //Call Function A
    A();
}

Hope this will help

希望这会有所帮助