1đź‘Ť
âś…
Just map
through array an add your property using Object.assing
and Array.prototype.find
:
const a = {
"meeting_summaries":[
{
"id":1,
"company_id":7,
"interaction_id":22,
"nature":"1",
"user_id":1,
"action":"Action Test 1",
"feedback":"Comment Test 1",
"created_at":"2017-06-04 10:15:02",
"updated_at":"2017-06-04 10:15:02",
"deleted_at":null,
"client_name":"Test Company 4",
"mention_name":"Analyst"
},
{
"id":2,
"company_id":8,
"interaction_id":22,
"nature":"2",
"user_id":1,
"action":"Action Test 2",
"feedback":"Comment Test 2",
"created_at":"2017-06-04 10:15:02",
"updated_at":"2017-06-04 10:15:02",
"deleted_at":null,
"client_name":"Test Company 5","mention_name":"Analyst"
}
]
};
const nature = [
{value: 1, label: "Demo 1"},
{value: 2, label: "Demo 2"},
{value: 3, label: "Demo 3"},
{value: 4, label: "Demo 4"},
{value: 5, label: "Demo 5"}
]
const res = a.meeting_summaries.map(ms => Object.assign(ms,
(nature.find(n => n.value == ms.nature)) // if corresponding object exists
? { nature_name: nature.find(n => n.value == ms.nature).label } : {}
))
console.log(res)
👤Egor Stambakio
2đź‘Ť
You could use a hash table and then iterate the meeting_summaries
.
const object = { meeting_summaries: [{ interaction_id: 22, nature: "1", client_name: "Test Company 4" }, { interaction_id: 22, nature: "2", client_name: "Test Company 5", mention_name: "Analyst" }] },
nature = [{ value: 1, label: "Demo 1" }, { value: 2, label: "Demo 2" }, { value: 3, label: "Demo 3" }],
natureMap = Object.assign(...nature.map(o => ({ [o.value]: o.label })));
object.meeting_summaries.forEach(o => o.nature_name = natureMap[o.nature]);
console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }
👤Nina Scholz
0đź‘Ť
You can do Array.prototype.forEach() and add the nature_name
property of the found element label
:
const nature = [{value: 1, label: "Demo 1"},{value: 2, label: "Demo 2"},{value: 3, label: "Demo 3"}];
const obj = {meeting_summaries: [{"interaction_id":22,"nature":"1","client_name":"Test Company 4",},{"interaction_id":22,"nature":"2","client_name":"Test Company 5"}]};
obj.meeting_summaries.forEach(el => el.nature_name = nature.find(n => n.value == el.nature).label);
console.log(obj);
.as-console-wrapper { max-height: 100% !important; top: 0; }
👤Yosvel Quintero
0đź‘Ť
You didn’t provide full context, but let’s assume that “meeting_summaries” is variable:
var meeting_summaries = [{
"interaction_id": 22,
"nature": "1",
"client_name": "Test Company 4",
},
{
"interaction_id": 22,
"nature": "2",
"client_name": "Test Company 5",
"mention_name": "Analyst"
}
]
const nature = [
{ value: 1, label: "Demo 1" },
{ value: 2, label: "Demo 2" },
{ value: 3, label: "Demo 3" }
]
var meeting_summaries = meeting_summaries.map(ms => {
ms.nature_name = nature.find(n => ms.nature == n.value).label;
return ms
})
console.log(meeting_summaries)
0đź‘Ť
I’m just take a “map” approach in my solution for better performance:
const meeting_summaries = [ { "interaction_id":22, "nature":"1", "client_name":"Test Company 4", }, { "interaction_id":22, "nature":"2", "client_name":"Test Company 5", } ];
const nature = [ {value: 1, label: "Demo 1"}, {value: 2, label: "Demo 2"}, {value: 3, label: "Demo 3"} ];
const natureMap = nature.reduce((accum,current)=>{
accum[current.value] = current.label;
return accum;
}, { });
const result = meeting_summaries.map(item => {
item.nature_name = natureMap[item.nature];
return item;
});
console.log(result)
Sorry for indentation, coded from smartphone
👤embarq
0đź‘Ť
const baseObj = {
"meeting_summaries": [
{
"interaction_id": 22,
"nature": "1",
"client_name": "Test Company 4",
},
{
"interaction_id": 22,
"nature": "2",
"client_name": "Test Company 5",
}
]
}
const natures = [
{value: 1, label: "Demo 1"},
{value: 2, label: "Demo 2"}
]
const meeting_summaries = baseObj.meeting_summaries
natures.forEach((nature, index) => {
meeting_summaries[index]["nature_name"] = nature.label
})
console.log(baseObj)
.as-console-wrapper { max-height: 100% !important; top: 0; }
👤john
Source:stackexchange.com