有这么一个需求,有两个数组,我们要将第一个数组的name属性的值,换成第二个数组的对应的项的值,将name换成fullName
var persons = [
{name: 'jason', age: 18, hobbit:'sleep', id: 1},
{name: 'jams', age: 20, hobbit: 'eat', id: 2}
];
var owner = [
{firstName: 'zhang', lastName: 'jason'},
{firstName: 'li', lastName: 'jams1'}
];
function findPerson(id) {
return new Promise((resolve, reject) => {
setTimeout(() => {
var person = persons.find(person => person.id == id);
if (person) {
resolve(person);
} else {
reject(Error('not find the person'))
}
}, 3000);
})
}
function fullName(res) {
return new Promise((resolve, reject) => {
setTimeout(() => {
var name = owner.find(name => name.lastName == res.name);
res.name = name;
if(name) {
resolve(res)
}else {
reject(Error('name is not found'))
}
}, 3000);
})
}
findPerson(2).then(data => fullName(data)).then(data => console.log(data))
网友评论