我有两个数组,需要通过搜索第一个数组中的位置来更新第二个数组。
let arr1 = [{"LEVEL":4,"POSITION":"RGM"},{"LEVEL":5,"POSITION":"GM"},{"LEVEL":5,"POSITION":"GMH"}]
let arr2 = [{"EMAIL":"test1@stc.com","POSITION":"GM"},
{"EMAIL":"test2@stc.com","POSITION":"GMH"},
{"EMAIL":"test3@stc.com","POSITION":"RGM"},
{"EMAIL":"test3@CSR.COM.AU","POSITION":"GM"}]
输出数组
output = [ {"LEVEL":5,"EMAIL":"test1@stc.com","POSITION":"GM"},
{"LEVEL":5,"EMAIL":"test2@stc.com",""POSITION":"GMH"},
{"LEVEL":4,"EMAIL":"test3@stc.com","POSITION":"RGM"},
{"LEVEL":5,"EMAIL":"test3@CSR.COM.AU","POSITION":"GM"}]
我尝试使用以下代码进行过滤,但给出了空数组,因此无法继续进行操作:
const output =arr1.filter((item) => {
return arr2.indexOf(item.POSITION) !== -1 && (item.POSITION)
});
答案 0 :(得分:2)
我想您可以使用map
创建一个新数组。您可以在此处使用find
为当前LEVEL
获取正确的POSITION
属性。
以下是一个智能解决方案:
const positions = [{"LEVEL":4,"POSITION":"RGM"},{"LEVEL":5,"POSITION":"GM"},{"LEVEL":5,"POSITION":"GMH"}];
const emails = [{"EMAIL":"test1@stc.com","POSITION":"GM"},{"EMAIL":"test2@stc.com","POSITION":"GMH"},{"EMAIL":"test3@stc.com","POSITION":"RGM"},{"EMAIL":"test3@CSR.COM.AU","POSITION":"GM"}];
const result = emails.map(email => {
email['LEVEL'] = positions.find(p => p['POSITION'] === email['POSITION'])['LEVEL'];
return email;
})
console.log(result);
map()方法创建一个新数组,其结果是在调用数组中的每个元素上调用提供的函数。
find()方法返回提供的数组中满足提供的测试功能的第一个元素的值。
我希望这会有所帮助!
答案 1 :(得分:2)
面向初学者的另一种解决方案:
let arr1 = [{"LEVEL":4,"POSITION":"RGM"},{"LEVEL":5,"POSITION":"GM"},{"LEVEL":5,"POSITION":"GMH"}]
let arr2 = [{"EMAIL":"test1@stc.com","POSITION":"GM"},
{"EMAIL":"test2@stc.com","POSITION":"GMH"},
{"EMAIL":"test3@stc.com","POSITION":"RGM"},
{"EMAIL":"test3@CSR.COM.AU","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);
答案 2 :(得分:1)
您可以使用Map
并使用LEVEL
映射新对象。
var array1 = [{ LEVEL: 4, POSITION: "RGM" }, { LEVEL: 5, POSITION: "GM" }, { LEVEL: 5, POSITION: "GMH" }],
array2 = [{ EMAIL: "test1@stc.com", POSITION: "GM" }, { EMAIL: "test2@stc.com", POSITION: "GMH" }, { EMAIL: "test3@stc.com", POSITION: "RGM" }, { EMAIL: "test3@CSR.COM.AU", 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; }
答案 3 :(得分:1)
最简单的方法是:
let arr1 = [{"LEVEL":4,"POSITION":"RGM"},{"LEVEL":5,"POSITION":"GM"},{"LEVEL":5,"POSITION":"GMH"}]
let arr2 = [{"EMAIL":"test1@stc.com","POSITION":"GM"},
{"EMAIL":"test2@stc.com","POSITION":"GMH"},
{"EMAIL":"test3@stc.com","POSITION":"RGM"},
{"EMAIL":"test3@CSR.COM.AU","POSITION":"GM"}]
let output = arr1.map(item => {
item.Email = arr2.find(a => {
return a.POSITION === item.POSITION
}).EMAIL;
return item;
});
console.log(output);