这是代码
.Published
对象具有诸如“ Deen”之类的人员详细信息:
.LatestVersion
peeps
数组具有喜欢的窥视和喜欢的书ID的组合:
const peeps = {
1: {
id: 1,
name: 'Deen',
readerCategory: 'champ',
},
.
.
.
};
chart
对象的书名类似“哈利·波特系列”:
const chart = [
{
id: 1,
peepsID: '1',
bookLikedID: '1',
},
.
.
.
];
从此信息中,列表项之一可能看起来像这样:
books
答案 0 :(得分:1)
您总是可以做这样的事情,如您所见,我们只是遍历chart
数组,获取相关的id
,然后根据bookLikedID
返回相关的字符串,peepsID
和name
属性。
如果您需要一些文档,那么我建议您看一下类似this的内容,其余内容应该相对简单。
如果您想了解更多有关我决定使用的语法的信息,那就是currying,如果您想进一步阅读诸如currying和函数式编程之类的主题,那么一个很好的信息来源将是Eric的帖子。我发现Eric是学习如何在JavaScript应用程序中实现功能样式编程的绝佳资源。
根据要求喜欢同一本书的人们,如您在本例中看到的那样,该函数还接受了一个参数,它只是接受您要查询的书名。然后,此函数将使用reduce
函数来生成名称数组,例如书名“ x”。
我还使用了Object.keys
和Object.values
进行更新,以查找所有喜欢'x'
书的人。
const peeps={1:{id:1,name:"Deen",readerCategory:"champ"},2:{id:2,name:"Tom",readerCategory:"noob"},3:{id:3,name:"Jack",readerCategory:"GOD"}};
const chart=[{id:1,peepsID:"1",bookLikedID:"1"},{id:2,peepsID:"2",bookLikedID:"1"},{id:3,peepsID:"3",bookLikedID:"2"}];
const books={1:{id:1,name:"Harry Potter Series"},2:{id:2,name:"Lord Of The Rings Series"},3:{id:3,name:"Fifty Shades of Grey"}};
// Edit
const results = a => b => c => a.map(o => `${b[o.peepsID].name} likes ${c[o.bookLikedID].name}`);
// Edit 2
const similarTastes = a => b => c => n => a.reduce((v, o) => {
const found = Object.values(c).find(({name}) => name == n);
if (found && o.bookLikedID == found.id) v.push(b[o.peepsID].name);
return v;
}, []);
// Edit 3
const getAllSimilarTastes = a => b => c => {
const obj = {};
Object.keys(c).map(k => obj[c[k].name] = similarTastes(a)(b)(c)(c[k].name));
return obj;
};
// Edit 4
const getUnliked = a => b => c => {
const o = getAllSimilarTastes(a)(b)(c);
return Object.keys(o).filter(x => o[x].length <= 0);
};
const isUnliked = a => b => c => n => getUnliked(a)(b)(c).indexOf(n) >= 0;
// Results.
console.log(results(chart)(peeps)(books));
console.log(similarTastes(chart)(peeps)(books)('Harry Potter Series'));
console.log(getAllSimilarTastes(chart)(peeps)(books));
console.log(getUnliked(chart)(peeps)(books));
console.log(isUnliked(chart)(peeps)(books)('Fifty Shades of Grey'));
console.log(isUnliked(chart)(peeps)(books)('Harry Potter Series'));