Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="post">
<h2>Deep filter js object or array with Lodash extension</h2>
<p>Lodash method "_.filter" iterates over the top level only. If you want to filter nested data - "_.filterDeep" from the <a href="https://github.com/YuriGor/deepdash/" rel="noreferrer noopener">Deepdash</a> will do the job.</p>
<hr />
<p><strong>Deepdash</strong> is an extension for the Lodash library. It mixes several traversal methods right into <strong>Lodash</strong> object and tries to follow API style of this great library. So if you are familiar with Lodash, you will feel ok with Deepdash too. </p>
<hr />
<p>This little tutorial will let you see, how to process tree-like structures easily.</p>

<a href="http://yurigor.com/deep-filter-js-object-or-array-with-lodash" target="_blank">Read more..</a>
</div>

<div id="filters"><input type="text" placeholder="enter keyword"></div>
<div id="mycomments"></div>

<div id="templates" style="display:none;">
  <div id="comment" class="comment">
    <div class="fa author"></div>
    <div class="fa rating"></div>
    <div class="clear"></div>
    <div class="text"></div>  
    <div class="replies"></div>    
  </div>
  <label id="filter"><input type="checkbox"><i class="title fa"></i></label>
</div>
</div>

              
            
!

CSS

              
                html{
  background-color:#0c2233;
  color:#FFF;
}
a {
  color:#08A;
}
.clear{
  clear:both;
}
.post {
  padding:0px 5px;
  margin-bottom:20px;
}
#comments {
  padding:15px;
}
.comment {
  background-color:rgba(#000,0.3);
  border: 1px solid rgba(#FFF,0.2);
  padding:15px;
  margin:5px;
  float:left;
  clear:both;
  min-width:400px;
}
.replies .comment {
  border:none;
  //border-left: 1px dashed rgba(#FFF,0.5);
  float:right;
  min-width:200px;
  .author {
    font-size:14px;
  }
  .rating{
    display:none;
  }
  .text{
    border:none;
    margin:0;
    padding:0;
  }
  .author+.rating+.clear {
    clear:none;
  }
  .author:after{
    content:":  ";
    white-space: pre;
  }
  .author,.text {
    display:inline;
  }
}
.author {
  font-weight:bold;
  float:left;
  &:before{
    color:#833;
    content:"\f21b  ";
    font-size:14px;
    white-space: pre;
  }
  &.verified:before{
    color:#383;
    content:"\f007  ";
  }
}
.rating{
  float:right;
  color:gold;
  width:74px;
  overflow:hidden;
  &:before {  
    position:relative;
    left:-75px;
  content:"\f005\f005\f005\f005\f005\f006\f006\f006\f006\f006";
  }
  @for $s from 0 through 5 {
    &.stars-#{$s}:before {
      left:-75px + $s*15px;
    }    
  }
}
.text {
  font-size:14px;
  font-style:italic;
  margin-top:10px;
  border-top: 1px dashed rgba(#FFF,0.3);
  padding-top:10px;
  b {
    color:gold;
  }
}
#filters{
  padding:5px;
  input[type=text]{
    margin-right:3px;
    padding:0 2px;
    width:120px;
  }
  input[type=checkbox]{
    display:none;
    &+i{
      user-select: none;
      opacity:0.5;
      border: 1px solid rgba(#FFF,0.5);
      border-radius:3px;
      padding:5px;
      &:before{
        content:"\f096";
        margin-right:3px;
      }
      &:hover{
        opacity:1;
      }
    }
    &:checked+i{
      opacity:1;
      &:before{
        content:"\f046";
        margin-right:1px;
        color:#383;
      }
    }
  }
  label{
    padding:0;
    cursor:pointer;
    font-size:12px;
    margin-right:4px;
    &.star{
      input+i:before{
        content:"\f005";
        margin-right:0;
        color:gold;
      }
      input:checked+i:before{
        content:"\f006";
        margin-right:0;
      }
    }
  }
}
              
            
!

JS

              
                var filterState = {};

function filterComments(){
  var data = getData();
  var filtrate = data;
  if(!_.isEmpty(filterState)){     
     //will do filtering here
  }  
  
  return data;
} 


var comments = document.getElementById('mycomments');
var tpl = document.getElementById('comment');
var filters = document.getElementById('filters');
var textField = filters.children[0];
var flt = document.getElementById('filter');

renderComments(filterComments(),comments);
addFilter('verified','Verified','Show verified users only');
addFilter('s1','','Exclude comments with rating 1','star');
addFilter('s2','','Exclude comments with rating 2','star');
addFilter('s3','','Exclude comments with rating 3','star');
addFilter('s4','','Exclude comments with rating 4','star');
addFilter('s5','','Exclude comments with rating 5','star');

function renderComments(data,p){
  p.innerHTML = "";
  data.forEach(function(d){
    var comment = tpl.cloneNode(true);
    comment.id="";
    var author = comment.getElementsByClassName("author")[0];
    author.innerHTML = d.name;
    if(d.verified)
      author.className += " verified";
    if(d.rating)
      comment.getElementsByClassName("rating")[0].className += " stars-"+d.rating;
    var html = d.text;
    if(filterState.text){      
      html = _.replace(html,new RegExp(_.escapeRegExp(filterState.text), 'gi'),'<b>$&</b>');
    }
    comment.getElementsByClassName("text")[0].innerHTML = html;
    if(d.replies&&d.replies.length)
      renderComments(d.replies,comment.getElementsByClassName("replies")[0]);
    p.appendChild(comment);
  });
}
 
function addFilter(id,label,title,cls){
  var filter = flt.cloneNode(true);
  filter.id="";
  if(cls)
    filter.className += ' '+cls;
  filter.getElementsByClassName("title")[0].innerHTML = label;
  filter.setAttribute('title',title);
  filter.addEventListener('change', function(event) {
    if(event.target.checked)
      filterState[id] = true;
    else
      delete filterState[id];
    renderComments(filterComments(),comments);
  });
  filters.appendChild(filter);
}

textField.addEventListener('keyup', function(event) {
  if(this.value!=''){
    filterState.text = this.value.toLowerCase();
  }else{
    delete filterState.text;
  }
  renderComments(filterComments(),comments);
});



// --------- Data
function getData(){
  return [
  {
    name:'Bob',
    text:'Perfect!',
    rating:5,
    verified:true,
    replies:[
      {
        name:'Alice',
        text:'Agree',
        verified:false
      },
      {
        name:'admin',
        text:'Thank you!',
        verified:true,
        replies:[
          {
            name:'Bob',
            text:'Write more!',
            verified:true,
            replies:[
              {
                name:'admin',
                text:'Ok :)',
                verified:true,
              }
            ]
          }
        ]
      },
      {
        name:"Augusta",
        text:"like a brillaint!11",
        verified:true
      }
    ]
  },
  {
    name:'mr.John',
    text:'Well done..',
    rating:4,
    verified:false,
    replies:[
      {
        name:'admin',
        text:'Can it be better?',
        verified:true,
        replies:[{
          name:'mr.John',
          text:'May be last three lines can be shorter..',
          verified:false,
          replies:[,
                {
                  name:'Bob',
                  verified:true,
                  text:"Don't listen to him, it will be unreadable!"
                }]
      }]
      }
    ]
  },
    {
      name:'Mark',
      rating:5,
      text:'Any way to donate?',
      verified:false,
      replies:[
        {
          name:'Bill',
          text:'+1',
          verified:false,
        },
        {
          name:'Larry',
          text:'+1',
          verified:true
        },
      ] 
    },
    {
      name:'Regina',
      rating:2,
      text:'Not really like it',
      verified:true,
      replies:[
        {
          name:'admin',
          text:':(',
          verified:true,
        }
      ]
    }
];
};
              
            
!
999px

Console