Tayab
Tayab

Reputation: 71

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

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.

Upvotes: 1

Views: 87

Answers (1)

JO3-W3B-D3V
JO3-W3B-D3V

Reputation: 2134

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"},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'));

Upvotes: 1

Related Questions