ORM

In this example below you will see how to do a ORM with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by andru1989, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright andru1989 ©
  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>ORM</title>
  
  
  
  
  
</head>

<body>

  
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/andru1989/orm-OmPjed */
var posts = [
  { 
    name: 'Hanami introduction', 
    price: 2000,
    comments: [{ text: 'lorem ipsum' }, { text: 'title test' }, { text: 'title 3'}]
  },
  { 
    name: 'Ruby introduction',     
    price: 4000,
    comments: [{ text: 'lorem ipsum' }, { text: 'title test' }, { text: 'title 3' }]
  }
];

function validate(element, filter) {
  var keys = Object.keys(filter);
  var booleans = keys.filter(function(key) {
    return element[key] === filter[key];
  });
  
 return booleans.length === keys.length
}

function where(array, filter) {
  return array.filter(function(element) {
    return validate(element, filter) === true;
  });
}

function find(array, filter) {
  return array.find(function(element) {
    return validate(element, filter) === true;
  })
}

var collection = where(posts, { name: 'Ruby introduction', price: 4000 });
console.log(collection);

var object = find(posts, { name: 'Ruby introduction', price: 4000 });
console.log(object);

Comments