'filter a javascript object to only return key value pairs based on another object

I have an array that looks like this, this array is called Columns

[
    {
         "key":"event_tag",
         "value":"Event tag",
         "order":2,
         "filter":{
             "value":[],
             "type":"options",
            "options":["refresh","done_sent","send","level","ack","create"]
        }
    },
    {
        "key":"created_timestamp",
        "order":0,
        "value":"Created timestamp"
    },
    {
        "key":"workflow_tag",
        "value":"Workflow tag",
        "order":1,
        "filter":{
            "value":[],
            "type":"options",
            "options":[]
        }
     }
]

I have another array that looks like this, this is called rowData

[
    {
        "created_timestamp":"2022-04 04T16:21:44.327926Z",
        "created_by":"xxxxxxxxx",
        "workflow_tag":"xxxxx",
        "event_tag":"refresh",
        "user_email":"xxxxx",
        "counterparty":"GAMMA",
        "success":true,
        "chat_message_id":"xxxxx",
        "chat_stream_id":"xxxxx",
        "chat_platform":"xxxxx",
        "routing_rule":null,"notification":13600
}]

What I am wanting to do in the second array is remove all attributes from the attribute where key doesn't match a key:value from the first array,

So in the first array I each object has a "key" attribute, if the value of that key attribute exists in the second arrays object I i want to keep it, if not I want to remove it, so based on the first array objects the final array for the second array should look like,

[
    {
        "created_timestamp":"2022-04 04T16:21:44.327926Z",
        "workflow_tag":"xxxxx",
        "event_tag":"refresh",
}]

I cannot work out how to do this, should I be doing some kind of map on the rowData array and doing some kind of condition to check the key exists in the columns array, and if so how?

const newRowData = rowData.map((obj) => {
   if(Object.keys(obj).includes(columns.key)) {
      return obj[key];
   }

});

To be honest I am pretty lost, all I want is a new array objects and for those objects to only include the keys that match the key attribute from the columns array



Solution 1:[1]

First create keys need to be used from columns using map. Using map on rowData and Object.assign.

const cols = [
  {
    key: "event_tag",
    value: "Event tag",
    order: 2,
    filter: {
      value: [],
      type: "options",
      options: ["refresh", "done_sent", "send", "level", "ack", "create"],
    },
  },
  {
    key: "created_timestamp",
    order: 0,
    value: "Created timestamp",
  },
  {
    key: "workflow_tag",
    value: "Workflow tag",
    order: 1,
    filter: {
      value: [],
      type: "options",
      options: [],
    },
  },
];

const rowData = [
  {
    created_timestamp: "2022-04 04T16:21:44.327926Z",
    created_by: "xxxxxxxxx",
    workflow_tag: "xxxxx",
    event_tag: "refresh",
    user_email: "xxxxx",
    counterparty: "GAMMA",
    success: true,
    chat_message_id: "xxxxx",
    chat_stream_id: "xxxxx",
    chat_platform: "xxxxx",
    routing_rule: null,
    notification: 13600,
  },
];

const keys = cols.map(({ key }) => key);

const output = rowData.map((item) =>
  Object.assign(
    {},
    ...keys.map((key) => ({ [key]: item[key] }))
  )
);

console.log(output);

Solution 2:[2]

this will work

const data = [
    {
         "key":"event_tag",
         "value":"Event tag",
         "order":2,
         "filter":{
             "value":[],
             "type":"options",
            "options":["refresh","done_sent","send","level","ack","create"]
        }
    },
    {
        "key":"created_timestamp",
        "order":0,
        "value":"Created timestamp"
    },
    {
        "key":"workflow_tag",
        "value":"Workflow tag",
        "order":1,
        "filter":{
            "value":[],
            "type":"options",
            "options":[]
        }
     }
]

const rowData = [
    {
        "created_timestamp":"2022-04 04T16:21:44.327926Z",
        "created_by":"xxxxxxxxx",
        "workflow_tag":"xxxxx",
        "event_tag":"refresh",
        "user_email":"xxxxx",
        "counterparty":"GAMMA",
        "success":true,
        "chat_message_id":"xxxxx",
        "chat_stream_id":"xxxxx",
        "chat_platform":"xxxxx",
        "routing_rule":null,"notification":13600
}]

const extractData = (row) => {
  const keys = data.map(d => d.key)
  return row.map(r => Object.fromEntries(Object.entries(r).filter(([k, v]) => keys.includes(k))))
 
}

console.log(extractData(rowData))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Siva K V
Solution 2 R4ncid