getting data by id from object

问题: this is the code The peeps object has the people detail like "Deen": const peeps = { 1: { id: 1, name: 'Deen', readerCategory: 'champ', }, . . . }...

问题:

this is the code

The peeps object has the people detail like "Deen":

const peeps = {
  1: {
    id: 1,
    name: 'Deen',
    readerCategory: 'champ',
  },
  .
  .
  .
};

And the chart array has a combination of which peeps and the book ID the liked:

const chart = [
  {
    id: 1,
    peepsID: '1',
    bookLikedID: '1',
  },
  .
  .
  .
];

The books object has the book names like "Harry Potter Series":

const books = {
  1: {
    id: 1,
    name: 'Harry Potter Series',
  },

From this information, one of the list items might look like this:

* Deen's likes a Harry Potter Series book.

回答1:

You could always do something like this, as you can see, we just iterate over the chart array, getting the relevant id, and returning a relevant string according to the bookLikedID, peepsID & name properties.

If you require some documentation, then I suggest you take a look at something like this, the rest should be relatively straightforward.

Edit

If you want to know more about the syntax that I've decided to use, it's known as currying, if you'd like to read more into topics such as currying and functional programming, then an excellent source of information would be posts by Eric. I've found Eric to be an excellent source to learn how to implement functional style programming into JavaScript applications.

Edit 2

People who like the same book(s), as requested, as you can see in this example the function takes one more argument, it simply takes the book name that you're querying. This function will then use the reduce function to generate an array of names that like book 'x'.

I've also used Object.keys and Object.values for the update to find all people that like 'x' book.

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'}};

// 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;
};

// Results.
console.log(results(chart)(peeps)(books));
console.log(similarTastes(chart)(peeps)(books)('Harry Potter Series'));
console.log(getAllSimilarTastes(chart)(peeps)(books));

  • 发表于 2019-01-11 14:47
  • 阅读 ( 216 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除