English 中文(简体)
如何获得指数 是否有任何阵列?
原标题:How to get indexOf nested array in any array?

在可可豆类(使用可可造物3.8条游戏发动机)中,我有一阵列“foo”。 我也有一些阵列,如foo = [0,0],[3, 2];

However, when I attempt to call console.log(foo.indexOf([0, 0])) my program gives me a return value of -1. Is there something I m doing wrong?

I was expecting to receive 0 from the console.log statement shown above.

问题回答

它赢得了工作。 射线是参考型号,即使两个阵列内部的数值相同,但它们在记忆中也有不同的参考,因此它们永远不会平等。 如果你想找到与相同价值观的阵容,那么就必须对这些阵列中的价值观进行比较,或者在比较之前仅仅阐明价值观:

index = foo.findIndex(item => JSON.stringify(item) === JSON.stringify([0, 0]));

That is because you are trying to find something by reference and by typing a new [0,0] in the indexOf, you are creating a new reference and thus they don t match because its not the same reference to the array value at index 0.

When using primatives like a number or Boolean, they use value not reference.

如果你从mdn here,他们就提及

指数Of()法将搜索要素与使用严格平等阵列要素(与=操作人使用的相同算法)进行比较。 NaN的数值从来都不相同,因此指数Of()总是在发现N时返回-1。

现在,你知道,如果你试图填写<代码>[0,0] ==[0,0],你会错觉。 这是因为它检查了该参考文献而不是阵列的价值。 在联合材料中,两种数值不相等。

由于 j中的每一件都属于物体,因此也属于物体,而不是对数值或要素数目加以比较,因此,它检查了这些阵列的参照,这些阵列为何在这两种情况下都归还错误。

你们可以采用几种方法比较:

  1. first compare the array length and then the elements of the arrays
  2. Use lodash to compare arrays




相关问题
store data in memory with nestjs

I am trying to persist some data in my nestjs server so I can then use that data in my client app through http requests. I have created a products.service.ts file with the function getAllData() that ...

React Hook Form Error on custom Input component

I am having a problem when I use react hook form + zod in my application, in short the inputs never change value and I get the following error in the console: Warning: Function components cannot be ...

Updatable promises using proxy in JavaScript

EDIT: I ve updated this question, and the old question is moved here. A POC can be found on this jsfiddle or the snippet below GOAL: The goal here is to make or rather simulate a promise that can be ...

热门标签