2017-06-05 42 views
0

我在VueJS建立一个小的应用程序在那里我得到在以下格式的响应设置键映射:如何数组对象来自不同阵列在javascript

"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 -> naturenature -> 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", 
    } 
] 
+1

请创建[MCVE]只用2个或3个属性加的代码,你试过。使用''''''代码片段编辑器。我们不需要通读13个属性并试图找出你添加的内容和你没有的内容 – mplungjan

回答

1

只是map通过阵列的使用Object.assingArray.prototype.find添加您的财产:

const a = { 
 
\t "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)

0

你可以做Array.prototype.forEach()并添加找到的元素labelnature_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; }

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)

0

我只是采取了 “地图” 的做法在我的解决方案更好的性能:

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) 

对不起,由智能手机编码的缩进

2

您可以使用散列表,然后迭代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; }

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; }