Home > Blockchain >  How to do foreach on node.js like on php
How to do foreach on node.js like on php

Time:09-24

I have a string that looks like this:

  { '19694':
   { ignore_ignore_id: '19694',
     ignore_messages: '1',
     ignore_signatures: '1',
     ignore_topics: '1',
     ignore_chats: '0' },
  '19917':
   { ignore_ignore_id: '19917',
     ignore_messages: '1',
     ignore_signatures: '0',
     ignore_topics: '0',
     ignore_chats: '0' },
  '25895':
   { ignore_ignore_id: '25895',
     ignore_messages: '0',
     ignore_signatures: '0',
     ignore_topics: '0',
     ignore_chats: '1' } }

I only need the IDs in which "ignore_chats" = 1 so I expect this output: [25895]

I used to achieve this on php with the following code:

$igusers = unserialize($ignored);
  
 foreach($igusers as $key => $value) {
  if($value['ignore_chats'] == 1) {
   $ignoredids .= "[".$value['ignore_ignore_id']."]";
  }
 }

but now I am on node.js and I cant get a hold of the proper code to do it. I tried this:

foreach (key, igusers)
      var value, ignoredids; 
      value = igusers[key];
      if(value['ignore_chats'] == 1) {
       ignoredids  = "[" value['ignore_ignore_id'] "]";
      }

but I get a lot of "unexpected token" errors. I did some research but havent been able to find a more straightforward solution. What am I missing here?

Thank you.

CodePudding user response:

You used a wrong for loop, do it like this:

let igusers = {
    '19694':
    {
        ignore_ignore_id: '19694',
        ignore_messages: '1',
        ignore_signatures: '1',
        ignore_topics: '1',
        ignore_chats: '0'
    },
    '19917':
    {
        ignore_ignore_id: '19917',
        ignore_messages: '1',
        ignore_signatures: '0',
        ignore_topics: '0',
        ignore_chats: '0'
    },
    '25895':
    {
        ignore_ignore_id: '25895',
        ignore_messages: '0',
        ignore_signatures: '0',
        ignore_topics: '0',
        ignore_chats: '1'
    }
};

let ignoredids = '';

for (const key in igusers) {
    const value = igusers[key];

    if (value['ignore_chats'] == 1) {
        ignoredids  = "["   value['ignore_ignore_id']   "]";
    }
}

console.log(ignoredids);
// [25895]

CodePudding user response:

There few types of for loop in javascript

  • classic for loop
  • forof loop
  • forin loop
  • foreach loop

here are few examples:


let arrayWithFewMemebrs = ['array m1', 'array m2']
let objectWithFewMembers = {
    'member1': 'object m1',
    'member2': 'object m2'
}

//
// Types of for you can use for an array
//

// Classic for loop
for (let index = 0; index < arrayWithFewMemebrs.length; index  ) {
    const arrayMemebrValue = arrayWithFewMemebrs[index];
}

// Forof loop
for (const arrayMemebrValue of arrayWithFewMemebrs) {
    
}

// Foreach loop
arrayWithFewMemebrs.forEach((value, index) => {
    // You cant access to key 
    // but index is accesible
})


//
// Types of for you can use for an object
//

// Forin loop
for (const key in objectWithFewMembers) {
    if (Object.hasOwnProperty.call(object, key)) {
        const objectMemebrValue = object[key];
        
    }
}
  • Related