[英]How to map an array object with key from different array set in javascript
我正在VueJS
中构建一个小型应用程序, VueJS
以以下格式获取响应:
"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"}
]
我想用这个数据集将我的meeting_summaries
映射到meeting_summaries -> nature
点指向nature -> value
以便我的最终输出看起来像这样:
"meeting_summaries":[
{
"interaction_id":22,
"nature":"1",
'nature_name": "Demo 1",
"client_name":"Test Company 4",
},
{
"interaction_id":22,
"nature":"2",
'nature_name": "Demo 2",
"client_name":"Test Company 5",
}
]
您可以使用哈希表,然后迭代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; }
只需通过数组map
使用Object.assing
和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)
您可以执行Array.prototype.forEach()并添加找到的元素label
的nature_name
属性:
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; }
您没有提供完整的上下文,但是让我们假设“ meeting_summaries”是可变的:
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)
我只是在解决方案中采用“映射”方法以获得更好的性能:
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)
很抱歉缩进,通过智能手机编码
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; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.