如何使用javascript中不同数组的键映射数组对象

时间:2017-06-05 09:46:51

标签: javascript arrays vue.js vuejs2

我正在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",
    }
]

6 个答案:

答案 0 :(得分:2)

您可以使用哈希表,然后迭代cursor

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

答案 1 :(得分:1)

只需.as-console-wrapper { max-height: 100% !important; top: 0; }通过数组,使用mapObject.assing添加您的媒体资源:

Array.prototype.find

答案 2 :(得分:0)

您可以执行Array.prototype.forEach()并添加找到的元素nature_name的{​​{1}}属性:

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

答案 3 :(得分: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)




答案 4 :(得分: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)

很抱歉缩进,从智能手机编码

答案 5 :(得分: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; }