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
CodePudding user response:
You can do something like this:
var col = [
{
"key":"event_tag",
"value":"Event tag",
"order":2,
"filter":{
"value":[],
"type":"options",
"options":["refresh","done_sent","send","level","ack","create"]
}
}];
var data = [
{
"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
}];
var colNames = col.map(c => c.key);
var newData = data.map(d => {
var newD = {};
colNames.forEach(cn => {
newD[cn] = d[cn];
}
return newD;
});
CodePudding user response:
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))