Mani
Mani

Reputation: 741

Search and update array based on key

I have two array, need to update the second array by searching the position in the first array.

let arr1 = [{"LEVEL":4,"POSITION":"RGM"},{"LEVEL":5,"POSITION":"GM"},{"LEVEL":5,"POSITION":"GMH"}]

let arr2 = [{"EMAIL":"[email protected]","POSITION":"GM"},
            {"EMAIL":"[email protected]","POSITION":"GMH"},
            {"EMAIL":"[email protected]","POSITION":"RGM"},
            {"EMAIL":"[email protected]","POSITION":"GM"}]

Output Array

 output  = [ {"LEVEL":5,"EMAIL":"[email protected]","POSITION":"GM"}, 
                {"LEVEL":5,"EMAIL":"[email protected]",""POSITION":"GMH"}, 
                {"LEVEL":4,"EMAIL":"[email protected]","POSITION":"RGM"}, 
                {"LEVEL":5,"EMAIL":"[email protected]","POSITION":"GM"}]

I tried using the below code to filter but gives empty array, so not able to proceed further:

const output =arr1.filter((item) => {
  return arr2.indexOf(item.POSITION) !== -1 && (item.POSITION)
});

Upvotes: 0

Views: 66

Answers (4)

yonexbat
yonexbat

Reputation: 3012

Another solution for beginners:

let arr1 = [{"LEVEL":4,"POSITION":"RGM"},{"LEVEL":5,"POSITION":"GM"},{"LEVEL":5,"POSITION":"GMH"}]

let arr2 = [{"EMAIL":"[email protected]","POSITION":"GM"},
            {"EMAIL":"[email protected]","POSITION":"GMH"},
            {"EMAIL":"[email protected]","POSITION":"RGM"},
            {"EMAIL":"[email protected]","POSITION":"GM"}]

function addLevel() {
    const resultingArray = [];
    arr2.forEach(itemarray2 => {
        const copyOfArrayItem2 = Object.assign({}, itemarray2);        
        resultingArray.push(copyOfArrayItem2);
        const itemArray1 = arr1.find(x => x.POSITION === itemarray2.POSITION);
        if(itemArray1) {
            copyOfArrayItem2.LEVEL = itemArray1.LEVEL;
        }
    });
    return resultingArray;
}


const newArray = addLevel();
console.log(newArray);

Upvotes: 2

Hossein Zare
Hossein Zare

Reputation: 580

The simplest way is:

let arr1 = [{"LEVEL":4,"POSITION":"RGM"},{"LEVEL":5,"POSITION":"GM"},{"LEVEL":5,"POSITION":"GMH"}]
let arr2 = [{"EMAIL":"[email protected]","POSITION":"GM"},
            {"EMAIL":"[email protected]","POSITION":"GMH"},
            {"EMAIL":"[email protected]","POSITION":"RGM"},
            {"EMAIL":"[email protected]","POSITION":"GM"}]
            
let output = arr1.map(item => {
  item.Email = arr2.find(a => {
    return a.POSITION === item.POSITION
  }).EMAIL;
  
  return item;
});

console.log(output);

Upvotes: 1

norbitrial
norbitrial

Reputation: 15166

I guess you can use map to create a new array. There you can use find to get the proper LEVEL property for the current POSITION.

One smart solution can be the following:

const positions = [{"LEVEL":4,"POSITION":"RGM"},{"LEVEL":5,"POSITION":"GM"},{"LEVEL":5,"POSITION":"GMH"}];
const emails = [{"EMAIL":"[email protected]","POSITION":"GM"},{"EMAIL":"[email protected]","POSITION":"GMH"},{"EMAIL":"[email protected]","POSITION":"RGM"},{"EMAIL":"[email protected]","POSITION":"GM"}];

const result = emails.map(email => {
  email['LEVEL'] = positions.find(p => p['POSITION'] === email['POSITION'])['LEVEL'];
  return email;
})

console.log(result);

From Array.prototype.map() documentation:

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

From Array.prototype.find() documentation:

The find() method returns the value of the first element in the provided array that satisfies the provided testing function.

I hope this helps!

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386550

You could take a Map and map new objects with LEVEL.

var array1 = [{ LEVEL: 4, POSITION: "RGM" }, { LEVEL: 5, POSITION: "GM" }, { LEVEL: 5, POSITION: "GMH" }],
    array2 = [{ EMAIL: "[email protected]", POSITION: "GM" }, { EMAIL: "[email protected]", POSITION: "GMH" }, { EMAIL: "[email protected]", POSITION: "RGM" }, { EMAIL: "[email protected]", POSITION: "GM" }],
    levels = array1.reduce((m, { LEVEL, POSITION }) => m.set(POSITION, LEVEL), new Map),
    result = array2.map(o => Object.assign({ LEVEL: levels.get(o.POSITION) }, o));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Related Questions