skip to Main Content

I’m working on an telegram anti-flood bot, so I created a simple one, save the text and sender ID in a JSON when a message is sent, when another message is sent, check if both are the same and if so, deletes the message; however the method doesn’t work sometimes and deletes a different message (eg.: 2 different images without a caption are sent by a user quickly, the second image will be deleted), so I wanted to save all JSON excluding specific elements (like message id and date), because they’re always different, so how can i do that?
Here is a JSON message for examples:

{
    message_id: 0,
    from: {
        id: 0,
        is_bot: false,
        first_name: 'John',
        last_name: 'Doe',
        username: 'JohnDoe',
        language_code: 'en-us'
    },
    chat: { id: 0, title: 'Cool Group.', type: 'group' },
    date: 12345,
    text: 'Test Message.'
}

2

Answers


  1. First store your Object in some variable than delete key-value which you want to exclude.

    var x = {
        message_id: 0,
        from: {
            id: 0,
            is_bot: false,
            first_name: 'John',
            last_name: 'Doe',
            username: 'JohnDoe',
            language_code: 'en-us'
        },
        chat: { id: 0, title: 'Cool Group.', type: 'group' },
        date: 12345,
        text: 'Test Message.'
    }; 
    delete x["message_id"]
    delete x["date"]
    
    Login or Signup to reply.
  2. It may be overkill, but you could make the structure which you expect, with fallback defaults, then merge in the incoming object, ignoring any properties not in the schema or of a different type… call it strict merge.

    function strictMerge(a, b) {
      if (!b || typeof b !== typeof a) return a
    
      if (Array.isArray(a) && Array.isArray(b)) {
        return a.map((item, index) => {
          if (b[index]) {
            return Object.keys(item).reduce((acc, key) => {
              acc[key] = typeof b[index][key] === 'object' && !Array.isArray(b[key]) ?
                this.strictMerge(a[index][key], b[index][key]) : (
                  typeof b[index][key] !== 'undefined' ? (
                    typeof b[index][key] !== typeof a[index][key] ? a[index][key] : b[index][key]
                  ) : a[index][key]
                );
              return acc
            }, {})
          }
          return item
        }, [])
      } else {
        return Object.keys(a).reduce((acc, key) => {
          acc[key] = typeof b[key] === 'object' && !Array.isArray(b[key]) ?
            strictMerge(a[key], b[key]) : (
              typeof b[key] !== 'undefined' ? (
                typeof b[key] !== typeof a[key] ? a[key] : b[key]
              ) : a[key]
            )
          return acc
        }, {})
      }
    }
    
    
    const message = {
      message_id: 0,
      from: {
        id: 0,
        is_bot: false,
        first_name: 'John',
        last_name: 'Doe',
        username: 'JohnDoe',
        language_code: 'en-us'
      },
      chat: {
        id: 0,
        title: 'Cool Group.',
        type: 'group'
      },
      date: 12345,
      text: 'Test Message.',
      foooo: 'barrr'
    }
    
    
    console.log(strictMerge({
      from: {
        id: 0,
        is_bot: false,
        first_name: '',
        last_name: '',
        username: '',
        language_code: ''
      },
      chat: {
        id: 0,
        title: '',
        type: ''
      },
      text: ''
    }, message))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search