[Vuejs]-Relation between two JSONs through keys

0👍

You could use loash’s find method to do this e.g.

Documentation: https://lodash.com/docs/4.17.11#find

_.find(authors, { 'id': 1 });

0👍

I’d use the native Array.filter method, like so:

let tolkien = authors.filter(author => !!author.id === 1)

Using .filter means you don’t need to include a library like lodash just for this one method.

0👍

You can create a new object with keys mapped to authors (i.e author[1] = {id: 1, ...}) which will let you quickly and easily access any author by his/her id. Example:

let authorByIds = {};
authors.forEach(author => {
    authorByIds[author.id] = {...author};
});

let firstBook = books[0];

console.log(authorByIds[firstBook.author]);
// will output author with id = 1, i.e J.R.R. Tolkien

Leave a comment