如何根据属性过滤JSON数据



我有以下JSON数据。

{
"partners": [
{
"logo": "school-of-bookkeeping.png",
"title": "Schoolofbook<br class='d-none d-md-inline-block'/>keeping.com",
"websiteURL":"https://www.schoolofbookkeeping.com",
"getInTouchURL":"https://www.schoolofbookkeeping.com",
"location":"Santa Monical, CA, US",
"servicesCategories":["Consulting Services","Webgility Setup"],
"accountingSoftware":["QuickBooks Online","QuickBooks Enterprise","QuickBooks Point of Sale"],
"onlineStore":["Magento","Shopify","WooCommerce"],
"marketplace":["Amazon","eBay","Etsy"],
"description": "Teaching Businesses and Accounting Professionals the ins and outs of ecommerce.",
"longDescription":"<p class='fs-16 lh-1-5'>Let's face it; ecommerce is hard. From tracking inventory, sales tax requirements, shipping fulfillment, and general bookkeeping, keeping pace with all the requirements and business workflows is difficult. That's why we have partnered with Webgility to create a learning resource to provide small and medium-sized businesses a seamless and path from setup to implementation, so the accounting is done so that can focus on running your business, rather than your business, rather than running yourself ragged.</p><p class='fs-16 lh-1-5'>Our lifelong learners are here for hire so you can be set up for success and have a trusted advisor to guide you through setup to implementation.</p>",
"servicesWeProvide": "<p class='fs-16 lh-1-5'>Website creation to e-commerce implementation.</p>",
"featured": false,
"partnerType": "Certified",
"link": "school-of-bookkeeping",
"customClass": "school-of-bookkeeping"
},
{
"logo": "danwidth.png",
"title": "Danwidth",
"websiteURL":"https://www.danwidth.com",
"getInTouchURL":"https://completebusinessgroup.com/danwidth/",
"location":"Tucson, AZ, US",
"servicesCategories":["Consulting Services","Webgility Setup"],
"accountingSoftware":["QuickBooks Online","QuickBooks Enterprise","QuickBooks Point of Sale"],
"onlineStore":["Magento","Shopify","WooCommerce"],
"marketplace":["Amazon","eBay","Etsy"],
"description": "Put your ecommerce bookkeeping on cruise control.",
"longDescription":"<p class='fs-16 lh-1-5'>I don't want to be your bookkeeper. I transform businesses through technology by creating automagic workflows that allow your bookkeeping to be handled "by accident." Imagine entering data once, or not at all, and your bookkeeping is done for you. Specializing in brick and mortar retail and ecommerce, I will set up your services to integrate so that your accounting and bookkeeping is done for you as a result of you simply running your business. We work with a host of solution providers that all integrate into your accounting platform that will enable you to make informed business decisions in real-time.</p><p class='fs-16 lh-1-5'>My expertise is unparalleled across all of Intuit's Small business platforms. Online or Desktop, including Point of Sale, if it can be done in QuickBooks, I know how to make it happen. In addition, we partner with other business services to increase your revenue, decrease your expenses, and widen your online presence.</p>",
"servicesWeProvide": "<p class='fs-16 lh-1-5'>End-to-end services from website creation, digital marketing, ecommerce platform to bookkeeping, payroll service setup, and support.</p>",
"featured": false,
"partnerType": "Certified",
"link": "danwidth",
"customClass": ""
}
]
}

我如何根据服务类别、帐户软件、在线商店、市场和合作伙伴类型进行筛选?

我想用更改时选择框值的$.getJSON((方法过滤数据。

这取决于您想要如何进行过滤。有几种不同的方法可以实现这一点,这里有一个函数可以让你输入你想要过滤的密钥和一组值:

let filterBy = (list, keyToFilter, filters) => {
// Allow a string to be passed in
if(!Array.isArray(filters)) filters = [filters];
return list.filter(item => {
let values = item[keyToFilter];
// Check if the filter exists
if(!values) return false;
// For items that aren't arrays, like partnerType
//  This makes it compatible with the conditional
//  below, where the logic for check happens.
if(!Array.isArray(values)) values = [values];

// Check if array of your filters matches any in list
return filters.some(n => values.includes(n));
}
);
};

然后你可以在你的列表中这样称呼它:

let list = { "partners": [
...
]};
let accountingSoftware = filterBy(list["partners"], "accountingSoftware", ["QuickBooks Online","QuickBooks Enterprise"]);
let partnerType = filterBy(list["partners"], "partnerType", "Certified");

这里有一个例子,如果你想更多地使用它,https://repl.it/@BenjaminKnox/filter json

最新更新