css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <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>

            
          
!
            
              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;
      }
    }
  }
}
            
          
!
            
              var filterState = {};

function filterComments(){
  var data = getData();
  var excludeStars = _.reduce(filterState,function(r,v,k){
    if(k.length==2&&k[0]=='s')r.push(parseInt(k[1]));
    return r;
  },[]);
  _.remove(data,function(v){return _.includes(excludeStars,v.rating)});
  var filtrate = data;
  if(!_.isEmpty(filterState)){
     
      filtrate = _.filterDeep(filtrate,function(value,key,path,depth,parent){
        if(key!=='text')
          return;
        var res = true;
        if(filterState.verified){
          res = res && parent.verified;
        }
        if(filterState.text){
          res = res && _.includes(value.toLowerCase(),filterState.text);
        }
        return res;
      },{condense:false});
    
    return _.filterDeep(data,function(value,key,path,depth,parent,parentKey,parentPath){
      return _.exists(filtrate,parentPath);
    });
  }
  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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console