Filter json array using another json array
Create Date: April 16, 2019 at 09:57 AM         | Tag: JAVASCRIPT         | Author Name: Sun, Charles |
how to filter json array using another json array? (it is not working without id)
You could filter the items in the result array where the item matches every parameter in filterParams
. If you only want to check if at least one match exists replace .filter
with .some
e.g.
var matches = results.filter(item =>
filterParams.every(paramItem =>
item[paramItem.param] === paramItem.value));
I've limited it to equals comparison but you can expand the comparison using a switch
based on the other comparison types you have.
How to filter an json object by another object - Javascript
You can use every
and includes
to make a boolean value that will work with filter()
.
For example:
var jsondata = [{"firstName": "Sam","lastName": "Jones","age": "10"},{"firstName": "Sam1","lastName": "Jones1","age": "10"},{"firstName": "Sam2","lastName": "Jones2","age": "12"},{"firstName": "Sam3","lastName": "Jones3","age": "13"},{"firstName": "Sam4","lastName": "Jones4","age": "14"},{"firstName": "Sam5","lastName": "Jones5","age": "15"},{"firstName": "Sam","lastName": "Jones11","age": "16"},{"firstName": "Sam6","lastName": "Jones6","age": "17"},{"firstName": "Sam7","lastName": "Jones7","age": "18"},{"firstName": "Sam8","lastName": "Jones8","age": "19"},{"firstName": "Sam9","lastName": "Jones9","age": "20"},{"firstName": "Sam10","lastName": "Jones10","age": "21"},{"firstName": "Sam11","lastName": "Jones11","age": "22"},{"firstName": "Sam12","lastName": "Jones12","age": "23"}]
var filterArray = [{"id":"firstName","value":["Sam"]},{"id":"lastName","value":["Jones"]}]
let filtered = jsondata.filter(item => // filter jsondata
filterArray.every( f => // so every member of filter array
f.value.includes(item[f.id])) ) // has a corresponding item[id] in value
console.log(filtered)
The above filter says that for every
item in filterArray
you want the value
array to include the item keyed to id
. This will make it so the item in jsondata
needs to match all the criteria in filterArray
. If you add Sam1
and Jones1
to the arrays, you'll get two items in the filter.
var jsondata = [{"firstName": "Sam","lastName": "Jones","age": "10"},{"firstName": "Sam1","lastName": "Jones1","age": "10"},{"firstName": "Sam2","lastName": "Jones2","age": "12"},{"firstName": "Sam3","lastName": "Jones3","age": "13"},{"firstName": "Sam4","lastName": "Jones4","age": "14"},{"firstName": "Sam5","lastName": "Jones5","age": "15"},{"firstName": "Sam","lastName": "Jones11","age": "16"},{"firstName": "Sam6","lastName": "Jones6","age": "17"},{"firstName": "Sam7","lastName": "Jones7","age": "18"},{"firstName": "Sam8","lastName": "Jones8","age": "19"},{"firstName": "Sam9","lastName": "Jones9","age": "20"},{"firstName": "Sam10","lastName": "Jones10","age": "21"},{"firstName": "Sam11","lastName": "Jones11","age": "22"},{"firstName": "Sam12","lastName": "Jones12","age": "23"}]
var filterArray = [{"id":"firstName","value":["Sam", "Sam1"]},{"id":"lastName","value":["Jones", "Jones1"]}]
let filtered = jsondata.filter(item => // filter jsondata
filterArray.every( f => // so every member of filter array
f.value.includes(item[f.id])) ) // has a corresponding item[id] in value
console.log(filtered)
New Comment
Review and Install the Sample Application
Create Date: April 15, 2019 at 10:48 PM         | Tag: ANGULAR         | Author Name: Sun, Charles |
Create New Angular Project
Open a command prompt.
Navigate to a hard drive where you place projects. Make a new Angular project using the Angular CLI.
ng new PTC --prefix ptc
Create new Web API project
vmkdir PtcApi
cd PtcApi
dotnet new webapi
Load the current project in Code
code .
Create a Workspace
You have just loaded the Web API project. Add the Angular project to Code by clicking on the File | Add Folder to Workspace... menus.
Locate the PTC\PTC folder and add that folder to Code.
Select File | Save Workspace As... from the menu. Give it a name such as ptc-workspace.
Add Bootstrap & jQuery
npm install bootstrap@3 jquery --save
generate service without create a folder underneath:
ng g s security/security --flat -m app.module
//--flat means on underneath folder generated
//-m app.module means registering this new service with the app.module
create a derective:
ng g d security/hasClaim --flat
The schematic or collection:schematic to generate.
This option can take one of the following sub-commands:
appShell
application
class
component
directive
enum
guard
interceptor
interface
library
module
pipe
service
serviceWorker
webWorker
New Comment
A Very Simple Popup Box – HTML, CSS, JavaScript
Create Date: April 10, 2019 at 03:18 PM         | Tag: JAVASCRIPT         | Author Name: Sun, Charles |
A Very Simple Popup Box – HTML, CSS, JavaScript
Popup boxes are the most useful way of showing a warning or any other important information to the website visitors in many HTML5 templates. In this article I’m going to walk you through the creation of a very simple popup box with shadow overlay and close button. We’re going to implement this using HTML, CSS and jQuery in less than 100 lines (not compressed code).
The box and the shadow is hidden when the page loads, we have to trigger an event, like a link click to show it.
Use the iframe below to test the live demo:
The HTML Code
Add a link that triggers the box and a div that behaves like the box shadow. This wraps the actual box with the close button. The helper span is used to center the box vertically.
<a class="trigger_popup_fricc">Click here to show the popup</a> <div class="hover_bkgr_fricc"> <span class="helper"></span> <div> <div class="popupCloseButton">X</div> <p>Add any HTML content<br />inside the popup box!</p> </div> </div>
The CSS Code
/* Popup box BEGIN */ .hover_bkgr_fricc{ background:rgba(0,0,0,.4); cursor:pointer; display:none; height:100%; position:fixed; text-align:center; top:0; width:100%; z-index:10000; } .hover_bkgr_fricc .helper{ display:inline-block; height:100%; vertical-align:middle; } .hover_bkgr_fricc > div { background-color: #fff; box-shadow: 10px 10px 60px #555; display: inline-block; height: auto; max-width: 551px; min-height: 100px; vertical-align: middle; width: 60%; position: relative; border-radius: 8px; padding: 15px 5%; } .popupCloseButton { background-color: #fff; border: 3px solid #999; border-radius: 50px; cursor: pointer; display: inline-block; font-family: arial; font-weight: bold; position: absolute; top: -20px; right: -20px; font-size: 25px; line-height: 30px; width: 30px; height: 30px; text-align: center; } .popupCloseButton:hover { background-color: #ccc; } .trigger_popup_fricc { cursor: pointer; font-size: 20px; margin: 20px; display: inline-block; font-weight: bold; } /* Popup box BEGIN */
The Script
$(window).load(function () { $(".trigger_popup_fricc").click(function(){ $('.hover_bkgr_fricc').show(); }); $('.hover_bkgr_fricc').click(function(){ $('.hover_bkgr_fricc').hide(); }); $('.popupCloseButton').click(function(){ $('.hover_bkgr_fricc').hide(); }); });New Comment