javascript indexOf class 包含元素
javascript indexOf class containing element
我有一组相同的 class 元素,如何仅使用 class
中的一个元素搜索 indexOf
class Dhash {
constructor(Dlable, tophash) {
this.Dlable = Dlable;
this.tophash = tophash;
}
}
let listohashs = [];
listohashs[0] = new Dhash(0x001003, 0xfffffffffffffffffffffffffffffffffa);
listohashs[1] = new Dhash(0x011003, 0xfffffffffffffffffffffffffffffffffb);
listohashs[2] = new Dhash(0x021003, 0xfffffffffffffffffffffffffffffffffc);
console.log(listohashs.indexOf(0x001003)); // <-- here be the problem
对于此示例,我需要它 return 0,因为它匹配 listohashs[0].dlable
这样我可以获得对应的 tophash
值
我试过:
console.log(listohashs.indexOf(0x001003));
并把 .dlable
放在我能想到的任何地方。
我可以在其中一个元素位置使用通配符进行搜索吗?
即 * 将匹配任何内容
searchohash = new Dhash(0x001003, *);
console.log(listohashs.indexOf(searchohash));
json 我应该使用什么?我是 js 新手,几天前才开始使用 json
The indexOf() method returns the first index at which a given element
can be found in the array, or -1 if it is not present. - MDN
您要查找的元素的值如果 0x001003
但 listohashs
是一个对象数组。因此,您将对象与 0x001003
进行比较,后者不相等,因此它将 return -1
.
这里可以用findindex
,你要找到Dlable
属性值为0x001003
[=21=的对象的index
]
class Dhash {
constructor(Dlable, tophash) {
this.Dlable = Dlable;
this.tophash = tophash;
}
}
let listohashs = [];
listohashs[0] = new Dhash(0x001003, 0xfffffffffffffffffffffffffffffffffa);
listohashs[1] = new Dhash(0x011003, 0xfffffffffffffffffffffffffffffffffb);
listohashs[2] = new Dhash(0x021003, 0xfffffffffffffffffffffffffffffffffc);
console.log(listohashs.findIndex((o) => o.Dlable === 0x001003)); // <-- here be the problem
indexOf
仅当参数是数组的实际元素时才有效,而不仅仅是它的 属性。
使用 findIndex()
使用将执行适当比较的函数查找元素。
class Dhash {
constructor(Dlable, tophash) {
this.Dlable = Dlable;
this.tophash = tophash;
}
}
let listohashs = [];
listohashs[0] = new Dhash(0x001003, 0xfffffffffffffffffffffffffffffffffa);
listohashs[1] = new Dhash(0x011003, 0xfffffffffffffffffffffffffffffffffb);
listohashs[2] = new Dhash(0x021003, 0xfffffffffffffffffffffffffffffffffc);
console.log(listohashs.findIndex(h => h.Dlable == 0x001003));
我有一组相同的 class 元素,如何仅使用 class
中的一个元素搜索 indexOfclass Dhash {
constructor(Dlable, tophash) {
this.Dlable = Dlable;
this.tophash = tophash;
}
}
let listohashs = [];
listohashs[0] = new Dhash(0x001003, 0xfffffffffffffffffffffffffffffffffa);
listohashs[1] = new Dhash(0x011003, 0xfffffffffffffffffffffffffffffffffb);
listohashs[2] = new Dhash(0x021003, 0xfffffffffffffffffffffffffffffffffc);
console.log(listohashs.indexOf(0x001003)); // <-- here be the problem
对于此示例,我需要它 return 0,因为它匹配 listohashs[0].dlable
这样我可以获得对应的 tophash
值
我试过:
console.log(listohashs.indexOf(0x001003));
并把 .dlable
放在我能想到的任何地方。
我可以在其中一个元素位置使用通配符进行搜索吗? 即 * 将匹配任何内容
searchohash = new Dhash(0x001003, *);
console.log(listohashs.indexOf(searchohash));
json 我应该使用什么?我是 js 新手,几天前才开始使用 json
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present. - MDN
您要查找的元素的值如果 0x001003
但 listohashs
是一个对象数组。因此,您将对象与 0x001003
进行比较,后者不相等,因此它将 return -1
.
这里可以用findindex
,你要找到Dlable
属性值为0x001003
[=21=的对象的index
]
class Dhash {
constructor(Dlable, tophash) {
this.Dlable = Dlable;
this.tophash = tophash;
}
}
let listohashs = [];
listohashs[0] = new Dhash(0x001003, 0xfffffffffffffffffffffffffffffffffa);
listohashs[1] = new Dhash(0x011003, 0xfffffffffffffffffffffffffffffffffb);
listohashs[2] = new Dhash(0x021003, 0xfffffffffffffffffffffffffffffffffc);
console.log(listohashs.findIndex((o) => o.Dlable === 0x001003)); // <-- here be the problem
indexOf
仅当参数是数组的实际元素时才有效,而不仅仅是它的 属性。
使用 findIndex()
使用将执行适当比较的函数查找元素。
class Dhash {
constructor(Dlable, tophash) {
this.Dlable = Dlable;
this.tophash = tophash;
}
}
let listohashs = [];
listohashs[0] = new Dhash(0x001003, 0xfffffffffffffffffffffffffffffffffa);
listohashs[1] = new Dhash(0x011003, 0xfffffffffffffffffffffffffffffffffb);
listohashs[2] = new Dhash(0x021003, 0xfffffffffffffffffffffffffffffffffc);
console.log(listohashs.findIndex(h => h.Dlable == 0x001003));