var authors = [
{authorIndex:1, author:"John Steinbeck"},
{authorIndex:2, author:"Franz Kafka"},
{authorIndex:3, author:"J. R. R. Tolkien"},
{authorIndex:4, author:"Charles Dickens"}];
var books = [
{title:"The Grapes of Wrath",authorIndex:4,pubYear:1936},
{title:"The Hobbit",authorIndex:2,pubYear:1937},
{title:"The Trial",authorIndex:1,pubYear:1937},
{title:"A Tale of Two Cities",authorIndex:3,pubYear:1859}];
我想做的是在书中插入作者并与authorsIndex联系
最佳答案
const result = authors.map(val => {
return Object.assign({}, val, books.filter(v => v.authorIndex === val.authorIndex)[0]);
console.log(result);
});
console.log(result);
这将使作者和书籍组合在一起