4

I have stored group of objects into one array called 'resData' and i'm having one more array of data called 'approvedIds', there have included all approved id's. Here i want to match these two arrays and add one new key into 'resData' array like 'approveStatus:"approve"'. How to do this one in javascript?

All data's,

var resData = [
    {
        firstName:"Jhon",
        lastName:"adam",
        emailId:"[email protected]",
        id:"01"
    },
    {
        firstName:"Kyle",
        lastName:"Miller",
        emailId:"[email protected]",
        id:"02"
    },
    {
        firstName:"Jhonathan",
        lastName:"adam",
        emailId:"[email protected]",
        id:"03"
    },
    {
        firstName:"Lewis",
        lastName:"harber",
        emailId:"[email protected]",
        id:"04"
    }
];

Approved id's array,

var approvedIds = ['01', '03'];

My output will be like this,

var resData = [
        {
            firstName:"Jhon",
            lastName:"adam",
            emailId:"[email protected]",
            id:"01",
            approveStatus:'approved'
        },
        {
            firstName:"Kyle",
            lastName:"Miller",
            emailId:"[email protected]",
            id:"02"
        },
        {
            firstName:"Jhonathan",
            lastName:"adam",
            emailId:"[email protected]",
            id:"03",
            approveStatus:'approved'
        },
        {
            firstName:"Lewis",
            lastName:"harber",
            emailId:"[email protected]",
            id:"04"
        }
    ];

2 Answers 2

4

You can try this. Use forEach and indexOf functions

var resData = [
    {
        firstName:"Jhon",
        lastName:"adam",
        emailId:"[email protected]",
        id:"01"
    },
    {
        firstName:"Kyle",
        lastName:"Miller",
        emailId:"[email protected]",
        id:"02"
    },
    {
        firstName:"Jhonathan",
        lastName:"adam",
        emailId:"[email protected]",
        id:"03"
    },
    {
        firstName:"Lewis",
        lastName:"harber",
        emailId:"[email protected]",
        id:"04"
    }
];


var approvedIds = ['01', '03'];

resData.forEach(item => {
  if(approvedIds.indexOf(item.id) !== -1){
       item.approvedStatus = 'approved';
    }
} );

console.log(resData);

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

Comments

2

Using ES6 array functions, which is more functional and doesn't alter the original objects:

var resData = [
    {
        firstName:"Jhon",
        lastName:"adam",
        emailId:"[email protected]",
        id:"01"
    },
    {
        firstName:"Kyle",
        lastName:"Miller",
        emailId:"[email protected]",
        id:"02"
    },
    {
        firstName:"Jhonathan",
        lastName:"adam",
        emailId:"[email protected]",
        id:"03"
    },
    {
        firstName:"Lewis",
        lastName:"harber",
        emailId:"[email protected]",
        id:"04"
    }
];

var approvedIds = ['01', '03'];

//Solution:
var newData = resData
                 .filter(rd => approvedIds.indexOf(rd.id) >= 0)
                 .map(rd => Object.assign({}, rd, {approvedStatus: "approved"}));

console.log(newData, resData);

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.