0

I'm building a small application in VueJS where I'm getting a response in following format:

"meeting_summaries":[
    {
        "interaction_id":22,
        "nature":"1",
        "client_name":"Test Company 4",
    },
    {
        "interaction_id":22,
        "nature":"2",
        "client_name":"Test Company 5",
    }
]

And I'm having a data set of nature as:

const nature = [
    {value: 1, label: "Demo 1"},
    {value: 2, label: "Demo 2"},
    {value: 3, label: "Demo 3"}
]

I want to map my meeting_summaries with this data set were in meeting_summaries -> nature points to nature -> value so that my final output can look something like this:

"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
  • 1
    Please create a minimal reproducible example with only 2 or 3 attributes plus the code you tried. Use the <> snippet editor. We do not need to read through 13 attributes and try to figure out what you added and what you did not Commented Jun 5, 2017 at 9:49

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

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)

Comments

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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.