Keith Power
Keith Power

Reputation: 14141

Javascript Object array get last array

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.

Upvotes: 0

Views: 78

Answers (3)

tHeSiD
tHeSiD

Reputation: 5333

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.

Upvotes: 2

mobile_developer42
mobile_developer42

Reputation: 67

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]]
  }
}

Upvotes: 1

Pedro Filipe
Pedro Filipe

Reputation: 1075

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

Upvotes: 1

Related Questions