0

I have a json object array I have two functions. One to get the last message and the other to get. I need to keep the outer format the same but only return the one message.

I am getting the Json from the Telegram api and I have a Node Express script to return the reformatted Json

Here is the full Json:

{
    "ok": true,
    "result": [
        {
            "update_id": 650787950,
            "channel_post": {
                "message_id": 258,
                "chat": {
                    "id": -1001497153100,
                    "title": "TestBot",
                    "type": "channel"
                },
                "date": 1592256395,
                "text": "test messge"
            }
        },
        {
            "update_id": 650787951,
            "channel_post": {
                "message_id": 259,
                "chat": {
                    "id": -1001497153100,
                    "title": "TestBot",
                    "type": "channel"
                },
                "date": 1592256604,
                "text": "next"
            }
        }
    ]
}

I have a function to store the object after an api call to Telegram:

 storeUpdates(data){
    this.messageData = data;
 }

For the function to get the last message:

getlastMessage() {
   return
}

I am trying to return the Json:

{
    "ok": true,
    "result": [
        {
            "update_id": 650787951,
            "channel_post": {
                "message_id": 259,
                "chat": {
                    "id": -1001497153100,
                    "title": "TestBot",
                    "type": "channel"
                },
                "date": 1592256604,
                "text": "next"
            }
        }
    ]
}

And for the function to get a specific update_id

getNextMessage(update_id) {
    return
}

Again I am trying to get this format of a single message matching the passed in update_id

{
    "ok": true,
    "result": [
        {
            "update_id": 650787951,
            "channel_post": {
                "message_id": 259,
                "chat": {
                    "id": -1001497153100,
                    "title": "TestBot",
                    "type": "channel"
                },
                "date": 1592256604,
                "text": "next"
            }
        }
    ]
}

I am a little confused with the layers of object and arrays mixed.

1
  • 1
    Use array.filter() to get a new array with only items meeting a criterion (like an id match). Use array.slice(-1) to get a new array with just the last item. Commented Jun 15, 2020 at 22:50

3 Answers 3

2

Does this work?

const messages = {
  ok: true,
  result: [{
      update_id: 650787950,
      channel_post: {
        message_id: 258,
        chat: {
          id: -1001497153100,
          title: 'TestBot',
          type: 'channel',
        },
        date: 1592256395,
        text: 'test messge',
      },
    },
    {
      update_id: 650787951,
      channel_post: {
        message_id: 259,
        chat: {
          id: -1001497153100,
          title: 'TestBot',
          type: 'channel',
        },
        date: 1592256604,
        text: 'next',
      },
    },
  ],
};

const getLastMessage = (messages) => {
final = {
	ok: true,
	result: [],
};
final.result.push(messages.result[messages.result.length - 1]);
return final;
};


const getNextMessage = (update_id, messages) => {
  final = {
    ok: true
  };
  final.result = messages.result.filter((msg) => {
    return msg.update_id === update_id;
  });
  return final;
};

console.log(getLastMessage(messages));
console.log(getNextMessage(650787950, messages));

You get the last message by returning the last element in the array, by getting the length of the array and -1

I used Array.prototype.filter() to find the correct object.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I have one issue. I need the final.result to be an array of objects "result": [{ "update_id": 650787950 I tried final.result[] = messages.result[messages.result.length - 1]; but that does not work
do you not want ok: true in the result?
yes, they are fine. I am just missing the [ ] for the array result even though result will only have one object as its value
updated getLastMessage(), it will return the array as you wanted, and getlastmessage already does.
1

To get the last result you would need to go to results and return the last index:

function getlastMessage(resultObject) {
    return {
         ok: resultObject.ok
         result: [resultObject.result[resultObject.result.length - 1]]
       }
}

To get the message by update_id:

getNextMessage(update_id) {
   return {
         ok: resultObject.ok
         result: [resultObject.result.find(message => message.update_id === update_id)]
       }
}

Something along these lines

3 Comments

Thanks, I need to keep the outer {"ok": true,"result": [ {
You just need to initialize the object first with ok: true already in it and then append this result to that object. Check my ans
Edited my answer
1

Using destructuring you can make your code a little bit more compact:

const someObject = JSON.parse(`{
  "ok": true,
  "result": [
      {
          "update_id": 650787950,
          "channel_post": {
              "message_id": 258,
              "chat": {
                  "id": -1001497153100,
                  "title": "TestBot",
                  "type": "channel"
              },
              "date": 1592256395,
              "text": "test messge"
          }
      },
      {
          "update_id": 650787951,
          "channel_post": {
              "message_id": 259,
              "chat": {
                  "id": -1001497153100,
                  "title": "TestBot",
                  "type": "channel"
              },
              "date": 1592256604,
              "text": "next"
          }
      }
  ]
}`)

const getNextMessage = (update_id) =>  {
  return {
    ...someObject,
    result: someObject.result.find(message => message.update_id === update_id)
  }
}

const getLastMessage = () => {
  const arrayLength = someObject.result.length;
  return {
    ...someObject,
    result: someObject.result[arrayLength - 1] 
  }
}

console.log(getNextMessage(650787950))
console.log(getLastMessage())

If you want to keep the result type as an array you can use filter instead of find and surround the last element of result array with square brackets, like this:

const getNextMessage = (update_id) =>  {

return {
    ...someObject,
    result: someObject.result.filter(message => message.update_id === update_id)
  }
}

const getLastMessage = () => {
  const arrayLength = someObject.result.length;
  return {
    ...someObject,
    result: [someObject.result[arrayLength - 1]]
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.