Javascript dynamic filter over js with array of criterions

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const arr = [
{label : 'lbl1', text: 'txt1'},
{label : 'lbl2', text: 'txt2'},
{label : 'lbl3', text: 'txt3'},
{label : 'lbl4', text: 'txt4'},
// much more items
];
const filterBy = [
{label: 'lbl1',text: 'txt1'},
{label : 'lbl4', text: 'txt4'}
//may have 0 or more items
];
</code>
<code>const arr = [ {label : 'lbl1', text: 'txt1'}, {label : 'lbl2', text: 'txt2'}, {label : 'lbl3', text: 'txt3'}, {label : 'lbl4', text: 'txt4'}, // much more items ]; const filterBy = [ {label: 'lbl1',text: 'txt1'}, {label : 'lbl4', text: 'txt4'} //may have 0 or more items ]; </code>
const arr = [
   {label : 'lbl1', text: 'txt1'},
   {label : 'lbl2', text: 'txt2'},
   {label : 'lbl3', text: 'txt3'},
   {label : 'lbl4', text: 'txt4'},
   // much more items
];

const filterBy = [
    {label: 'lbl1',text: 'txt1'},
    {label : 'lbl4', text: 'txt4'}
    //may have 0 or more items
];

I want to dynamically filter arr by filterBy. filterBy is coming from the UI… here its just a representation of what the user can pass…

in a static way I would do:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>arr.filter(x => (x.text == filterBy[0].text && x.label == filterBy[0].label) ||
. (x.text == filterBy[1].text && x.label == filterBy[1].label)
);
</code>
<code>arr.filter(x => (x.text == filterBy[0].text && x.label == filterBy[0].label) || . (x.text == filterBy[1].text && x.label == filterBy[1].label) ); </code>
arr.filter(x => (x.text == filterBy[0].text && x.label == filterBy[0].label) ||
   .            (x.text == filterBy[1].text && x.label == filterBy[1].label)
);

Is it possible to chain dynamic criterions in JS?
Thanks

6

You can easily achieve this result using filter and some

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const arr = [
{ label: "lbl1", text: "txt1" },
{ label: "lbl2", text: "txt2" },
{ label: "lbl3", text: "txt3" },
{ label: "lbl4", text: "txt4" },
];
const filterBy = [
{ label: "lbl1", text: "txt1" },
{ label: "lbl4", text: "txt4" },
];
const result = arr.filter(({ label, text }) =>
filterBy.some((o) => o.label === label && o.text === text)
);
console.log(result);</code>
<code>const arr = [ { label: "lbl1", text: "txt1" }, { label: "lbl2", text: "txt2" }, { label: "lbl3", text: "txt3" }, { label: "lbl4", text: "txt4" }, ]; const filterBy = [ { label: "lbl1", text: "txt1" }, { label: "lbl4", text: "txt4" }, ]; const result = arr.filter(({ label, text }) => filterBy.some((o) => o.label === label && o.text === text) ); console.log(result);</code>
const arr = [
  { label: "lbl1", text: "txt1" },
  { label: "lbl2", text: "txt2" },
  { label: "lbl3", text: "txt3" },
  { label: "lbl4", text: "txt4" },
];

const filterBy = [
  { label: "lbl1", text: "txt1" },
  { label: "lbl4", text: "txt4" },
];

const result = arr.filter(({ label, text }) =>
  filterBy.some((o) => o.label === label && o.text === text)
);

console.log(result);

0

You can use reduce and inside the callback use filter to get the items from arr

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const arr = [{
label: 'lbl1',
text: 'txt1'
},
{
label: 'lbl2',
text: 'txt2'
},
{
label: 'lbl3',
text: 'txt3'
},
{
label: 'lbl4',
text: 'txt4'
},
//.... much more items
];
const filterBy = [{
label: 'lbl1',
text: 'txt1'
},
{
label: 'lbl4',
text: 'txt4'
}
];
const res = filterBy.reduce((acc, curr) => {
acc.push(...arr.filter(item => {
return item.label === curr.label && item.text === curr.text
}))
return acc;
}, []);
console.log(res)</code>
<code>const arr = [{ label: 'lbl1', text: 'txt1' }, { label: 'lbl2', text: 'txt2' }, { label: 'lbl3', text: 'txt3' }, { label: 'lbl4', text: 'txt4' }, //.... much more items ]; const filterBy = [{ label: 'lbl1', text: 'txt1' }, { label: 'lbl4', text: 'txt4' } ]; const res = filterBy.reduce((acc, curr) => { acc.push(...arr.filter(item => { return item.label === curr.label && item.text === curr.text })) return acc; }, []); console.log(res)</code>
const arr = [{
    label: 'lbl1',
    text: 'txt1'
  },
  {
    label: 'lbl2',
    text: 'txt2'
  },
  {
    label: 'lbl3',
    text: 'txt3'
  },
  {
    label: 'lbl4',
    text: 'txt4'
  },
  //.... much more items
];

const filterBy = [{
    label: 'lbl1',
    text: 'txt1'
  },
  {
    label: 'lbl4',
    text: 'txt4'
  }
];


const res = filterBy.reduce((acc, curr) => {

  acc.push(...arr.filter(item => {
    return item.label === curr.label && item.text === curr.text
  }))

  return acc;
}, []);

console.log(res)

use an obj instead of an array

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
const arr = [
{label : 'lbl1', text: 'txt1'},
{label : 'lbl2', text: 'txt2'},
{label : 'lbl3', text: 'txt3'},
{label : 'lbl4', text: 'txt4'},
];
const filterBy = {
criteria1:"lbl1",
criteria2:"txt1",
criteria3:"lbl4",
criteria4:"txt2",
}
const filtered = arr.filter(item => filterBy["criteria1"] === item.label ||filterBy["criteria2"] === item.text)||filterBy["criteria3"] === item.label || filterBy["criteria4"] === item.text);
</code>
<code> const arr = [ {label : 'lbl1', text: 'txt1'}, {label : 'lbl2', text: 'txt2'}, {label : 'lbl3', text: 'txt3'}, {label : 'lbl4', text: 'txt4'}, ]; const filterBy = { criteria1:"lbl1", criteria2:"txt1", criteria3:"lbl4", criteria4:"txt2", } const filtered = arr.filter(item => filterBy["criteria1"] === item.label ||filterBy["criteria2"] === item.text)||filterBy["criteria3"] === item.label || filterBy["criteria4"] === item.text); </code>

const arr = [
   {label : 'lbl1', text: 'txt1'},
   {label : 'lbl2', text: 'txt2'},
   {label : 'lbl3', text: 'txt3'},
   {label : 'lbl4', text: 'txt4'},
];

const filterBy = {
  criteria1:"lbl1",
  criteria2:"txt1",
  criteria3:"lbl4",
  criteria4:"txt2",
}

const filtered = arr.filter(item => filterBy["criteria1"] === item.label ||filterBy["criteria2"] === item.text)||filterBy["criteria3"] === item.label || filterBy["criteria4"] === item.text);

the other “solutions” have to make two or plus loops. while this one only do one.

while more criterias you have, the uglier it is going to look like

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const arr = [
{label : 'lbl1', text: 'txt1'},
{label : 'lbl2', text: 'txt2'},
{label : 'lbl3', text: 'txt3'},
{label : 'lbl4', text: 'txt4'},
// much more items
];
const filterBy = [
{label: 'lbl1',text: 'txt1'},
{label : 'lbl4', text: 'txt4'}
//may have 0 or more items
];
// If you want `||` between the filters.
console.log(arr.filter(x => filterBy.some(f => f.label === x.label && f.text === x.text)));
// If you want `&&` between the filters.
console.log(arr.filter(x => filterBy.every(f => f.label === x.label && f.text === x.text)));</code>
<code>const arr = [ {label : 'lbl1', text: 'txt1'}, {label : 'lbl2', text: 'txt2'}, {label : 'lbl3', text: 'txt3'}, {label : 'lbl4', text: 'txt4'}, // much more items ]; const filterBy = [ {label: 'lbl1',text: 'txt1'}, {label : 'lbl4', text: 'txt4'} //may have 0 or more items ]; // If you want `||` between the filters. console.log(arr.filter(x => filterBy.some(f => f.label === x.label && f.text === x.text))); // If you want `&&` between the filters. console.log(arr.filter(x => filterBy.every(f => f.label === x.label && f.text === x.text)));</code>
const arr = [
   {label : 'lbl1', text: 'txt1'},
   {label : 'lbl2', text: 'txt2'},
   {label : 'lbl3', text: 'txt3'},
   {label : 'lbl4', text: 'txt4'},
   // much more items
];

const filterBy = [
    {label: 'lbl1',text: 'txt1'},
    {label : 'lbl4', text: 'txt4'}
    //may have 0 or more items
];

// If you want `||` between the filters.
console.log(arr.filter(x => filterBy.some(f => f.label === x.label && f.text === x.text)));

// If you want `&&` between the filters.
console.log(arr.filter(x => filterBy.every(f => f.label === x.label && f.text === x.text)));

I think this is what you wants.. dynamic criterias.. using Object.keys and reduce you can get this.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const arr = [
{label : 'lbl1', text: 'txt1'},
{label : 'lbl2', text: 'txt2'},
{label : 'lbl3', text: 'txt3'},
{label : 'lbl4', text: 'txt4'},
// much more items
];
const filterBy = [
{label: 'lbl1',text: 'txt1'},
{label : 'lbl4', text: 'txt4'}
];
const filtered = arr.filter(item => {
return filterBy.some(fItem => {
const keys = Object.keys(fItem);
return keys.length > 0 && keys.reduce((acc, k) => {
return acc && item[k] === fItem[k];
}, true)
});
});
console.log(filtered)</code>
<code>const arr = [ {label : 'lbl1', text: 'txt1'}, {label : 'lbl2', text: 'txt2'}, {label : 'lbl3', text: 'txt3'}, {label : 'lbl4', text: 'txt4'}, // much more items ]; const filterBy = [ {label: 'lbl1',text: 'txt1'}, {label : 'lbl4', text: 'txt4'} ]; const filtered = arr.filter(item => { return filterBy.some(fItem => { const keys = Object.keys(fItem); return keys.length > 0 && keys.reduce((acc, k) => { return acc && item[k] === fItem[k]; }, true) }); }); console.log(filtered)</code>
const arr = [
   {label : 'lbl1', text: 'txt1'},
   {label : 'lbl2', text: 'txt2'},
   {label : 'lbl3', text: 'txt3'},
   {label : 'lbl4', text: 'txt4'},
   // much more items
];

const filterBy = [
    {label: 'lbl1',text: 'txt1'},
    {label : 'lbl4', text: 'txt4'}
];

const filtered = arr.filter(item => {
    return filterBy.some(fItem => {
           const keys = Object.keys(fItem);
           return keys.length > 0 && keys.reduce((acc, k) => {
            return acc && item[k] === fItem[k];
           }, true)
    });
});

console.log(filtered)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật