0

Assuming that I have this multiple data and I need to filter set of elements. How could I only filter the data with only message and createdAt will produce:

var getMessage = [
  0: {
    status: 'SENT',
    type: 'text',
    createdAt: "2021-07-07T08:11:51.686Z",
    web: {
      message: {
        text: "Get Started"
      }
    }
  },

  1: {
    status: 'SENT',
    type: 'text',
    createdAt: "2021-07-07T08:11:53.547Z",
    web: {
      message: {
        text: "Etrt"
      }
    }
  },

  2: {
    status: 'SENT',
    type: 'text',
    createdAt: "2021-07-07T08:12:07.785Z",
    web: {
      message: {
        text: "No answer found."
      }
    }
  }

];

const findKeywords = "O"

const messageData = getMessage.map(x => x);
const findMessage = messageData.filter(x => x.web.message.text.toLowerCase().includes(findKeywords.toLowerCase()));

console.log(findMessage);

How could I only filter the data with only message and createdAt will produce:

[
 createdAt: "2021-07-07",
 text: "No answer found."
]
Ayaka
  • 197
  • 1
  • 13
  • Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available [`Object`](//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods) methods (both static and on prototype). – Sebastian Simon Jul 08 '21 at 02:04
  • Your result is not a valid array. Arrays don't have key:value, objects do. – Barmar Jul 08 '21 at 02:08

0 Answers0