2

I have an array values declared as follows:

var values = Object.keys(oldvals).map(function(key) {
    return oldvals[key];
});

This is the contents of values:

{ '0': '0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2be' }

Next, I am executing the following code:

block.transactions.forEach( function(e) {
   console.log(e.to);

   if (values.indexOf(e.to) > -1) {
       console.log("FOUND")
   }

});

Now the e.to logging prints the following:

0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2be
0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2be
0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2be
0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2be
0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2be
0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2be
0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2be
0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2be
0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2be
0x88c1ffdcb9d631ef66eec30e706c1baaa415d22c
0x014f34c3f7858c2a5171bc307922a823b658d7bd
0x014f34c3f7858c2a5171bc307922a823b658d7bd
0x014f34c3f7858c2a5171bc307922a823b658d7bd
0x014f34c3f7858c2a5171bc307922a823b658d7bd
0x014f34c3f7858c2a5171bc307922a823b658d7bd
0x014f34c3f7858c2a5171bc307922a823b658d7bd
0x014f34c3f7858c2a5171bc307922a823b658d7bd
0x014f34c3f7858c2a5171bc307922a823b658d7bd
0x014f34c3f7858c2a5171bc307922a823b658d7bd

clearly showing that the address I am searching for exists in values, however, FOUND is never printed. I do not understand why.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Alk
  • 5,215
  • 8
  • 47
  • 116
  • 1
    what do you see in the indexOf result? `console.log(values.indexOf(e.to))` – AussieJoe Jun 21 '18 at 15:10
  • 1
    it prints -1 for everything – Alk Jun 21 '18 at 15:24
  • console.log(oldvals[key]) before returning it. and check manually. you may have an idea. – Sindhoor Nov 12 '18 at 06:14
  • `map` always returns an array, which is represented in JavaScript by `[ '0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2be' ]`. The question has it represented using `{ '0': '0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2be' }`, which is typically reserved for representing objects. I'm not sure how you are seeing it represented like that, but generally the best way to get a cleaner representation of what is contained in a variable is to use `console.log(JSON.stringify(variable, null, 2))`, for variables that hold values representable by JSON. – Heretic Monkey Apr 18 '23 at 22:01
  • Does this answer your question? [How do I check if an array includes a value in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-a-value-in-javascript) – Heretic Monkey Apr 18 '23 at 22:03

3 Answers3

0

Try this:

const obj = {'0': 'data0', '1': 'data1'},
    exist = Object.values(obj).indexOf('data1') > -1;

console.log('exist:', exist);
Slaawwa
  • 185
  • 12
0
var oldvals =[{ '0': 0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2be }];

var transactions =[{'to':'0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2be'},
{'to':'0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2b1'}, 
{'to':'0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2b2'},];


transactions.forEach( function(e) {
   console.log(e.to);

if (values.indexOf(e.to) > -1) {
   console.log("FOUND")
   }

});

output result...

0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2be
FOUND
0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2b1
0x3dedc2e13c8d8f944c9c6b99c414d67f62e1a2b2

please attached a sample structure how oldvals and transactions look like.

Indrakumara
  • 1,595
  • 17
  • 22
-2

You can use underscorejs library it has many utility tools which will help you reduce your code Have a look to that library

Danny Galiyara
  • 197
  • 2
  • 8