I want to exclude out some category names but the if checks in switch and in a traditional if statement aren't working. I also want to translate anything Arabic with English which doesn't seem to be applying- any help would be appreciated!
Switch statement:
// function customBuildFunction(cats,qty) {
// var valid_cats = [];
// for (var i=0; i < cats.length; i++) {
// switch(true) {
// case (cats[i].name != 'NA'):
// case (cats[i].name != 'New In'):
// case (cats[i].name != 'Sale'):
// case (cats[i].name != 'Card Holders'):
// case (cats[i].name != 'Bags & Accessories'):
// case (cats[i].name != 'Heels'):
// case (cats[i].name != 'Sneakers'):
// case (cats[i].name != 'Flats'):
// case (cats[i].name != 'Sandals'):
// case (cats[i].name != 'Boots'):
// case (cats[i].name != 'Pumps'):
// case (cats[i].name != 'Shoes'):
// case (cats[i].name != 'Bags'):
// case (cats[i].name != 'Loafers & Slippers'):
// case (cats[i].name != 'Mules'):
// case (cats[i].name != 'Men'):
// case (cats[i].name != 'Women'):
// case (cats[i].name != 'Kids'):
// case (cats[i].name != 'Baby'):
// valid_cats.push(cats[i].name)
// break;
// }
// }
// // console.log('valid cats', valid_cats)
// var uniqueBrands = [...new Set(valid_cats)]
// var uniqueValidCats = uniqueBrands.filter(e => e)
// console.log('unique', uniqueValidCats);
// var innerHTML = ''
// // console.log(qty);
// for (var j=0; j<qty; j++) {
// innerHTML += '<div class="container"> ' + '<div class="brands">' + uniqueValidCats[j] + '</div>' + '</div>'
// }
// return innerHTML
// }
Traditional for loop where I attempted to store translates Arabic strings:
function customBuildFunction(cats,qty,template,title) {
var valid_cats = [];
var str = [];
var regex = /^[\p{Arabic}\s\p{N}]+$/gm
console.log('cats', cats)
for (var i=0; i < cats.length; i++) {
if (cats[i].name != 'NA' || cats[i].name != 'New In' || cats[i].name != 'Sale' || cats[i].name != 'Card Holders' || cats[i].name != 'Bags & Accessories' || cats[i].name != 'Heels' || cats[i].name != 'Sneakers' || cats[i].name != 'Flats' || cats[i].name != 'Sandals' || cats[i].name != 'Boots' || cats[i].name != 'Pumps' || cats[i].name != 'Shoes' || cats[i].name != 'Bags' || cats[i].name != 'Loafers & Slippers' || cats[i].name != 'Mules' || cats[i].name != 'Men' || cats[i].name != 'Women' || cats[i].name != 'Kids' || cats[i].name != 'Baby') {
str.push(cats[i].name.replace(regex))
valid_cats.push(cats[i].name)
}
}
var uniqueBrands = [...new Set(valid_cats)]
var uniqueValidCats = uniqueBrands.filter(e => e)
// console.log('unique', uniqueValidCats);
console.log('str', str);
var innerHTML = ''
// console.log(qty);
for (var j=0; j<qty; j++) {
innerHTML += '<div class="container"> ' + '<div class="brands">' + uniqueValidCats[j] + '</div>' + '</div>'
}
return innerHTML
}
This is the response from cats that i'm iterating through:
cats
(25) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {rectype: 'CAT', name: 'Sandals'}
1: {rectype: 'CAT', name: 'Shoes'}
2: {rectype: 'CAT', name: 'Mules'}
3: {rectype: 'CAT', name: 'BALENCIAGA'}
4: {rectype: 'CAT', name: 'جديدنا'}
5: {rectype: 'CAT', name: 'Boots'}
6: {rectype: 'CAT', name: 'JIMMY CHOO'}
7: {rectype: 'CAT', name: 'Men'}
8: {rectype: 'CAT', name: 'New In'}
9: {rectype: 'CAT', name: 'Pumps'}
10: {rectype: 'CAT', name: 'ADIDAS'}
11: {rectype: 'CAT', name: 'جيمي تشو'}
12: {rectype: 'CAT', name: 'Sneakers'}
13: {rectype: 'CAT', name: 'Bags & Accessories'}
14: {rectype: 'CAT', name: 'Baby'}
15: {rectype: 'CAT', name: 'NA'}
16: {rectype: 'CAT', name: 'Women'}
17: {rectype: 'CAT', name: 'NIKE'}
18: {rectype: 'CAT', name: 'Kids'}
19: {rectype: 'CAT', name: 'Flats'}
20: {rectype: 'CAT', name: 'Heels'}
21: {rectype: 'CAT', name: 'Loafers & Slippers'}
22: {rectype: 'CAT', name: 'CHANEL'}
23: {rectype: 'CAT', name: 'Card Holders'}
24: {rectype: 'CAT', name: 'Bags'}
length: 25
[[Prototype]]: Array(0)
From the above comment ...
"Even though unicode escape is supported in JS, the language specific letter escaping like
\p{Arabic}
is not. But if it is about filtering all items with theirname
only featuring non latin word characters sequences then the OP can filter the last shown array data just by e.g. ...sampleData.filter(({ name }) => (/^\W+$/).test(name))
."
const sampleData = [
{ rectype: 'CAT', name: 'Sandals'},
{ rectype: 'CAT', name: 'Shoes'},
{ rectype: 'CAT', name: 'Mules'},
{ rectype: 'CAT', name: 'BALENCIAGA'},
{ rectype: 'CAT', name: 'جديدنا'},
{ rectype: 'CAT', name: 'Boots' },
{ rectype: 'CAT', name: 'JIMMY CHOO' },
{ rectype: 'CAT', name: 'Men' },
{ rectype: 'CAT', name: 'New In' },
{ rectype: 'CAT', name: 'Pumps' },
{ rectype: 'CAT', name: 'ADIDAS' },
{ rectype: 'CAT', name: 'جيمي تشو' },
{ rectype: 'CAT', name: 'Sneakers' },
{ rectype: 'CAT', name: 'Bags & Accessories' },
{ rectype: 'CAT', name: 'Baby' },
{ rectype: 'CAT', name: 'NA' },
{ rectype: 'CAT', name: 'Women' },
{ rectype: 'CAT', name: 'NIKE' },
{ rectype: 'CAT', name: 'Kids' },
{ rectype: 'CAT', name: 'Flats' },
{ rectype: 'CAT', name: 'Heels' },
{ rectype: 'CAT', name: 'Loafers & Slippers' },
{ rectype: 'CAT', name: 'CHANEL' },
{ rectype: 'CAT', name: 'Card Holders' },
{ rectype: 'CAT', name: 'Bags' },
];
console.log(
sampleData.filter(({ name }) => (/^\W+$/).test(name))
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
If one in addition also wants to match (language independent) all names which consists of uppercase letters and whitespace only then one changes the filter slightly to ...
const sampleData = [
{ rectype: 'CAT', name: 'Sandals'},
{ rectype: 'CAT', name: 'Shoes'},
{ rectype: 'CAT', name: 'Mules'},
{ rectype: 'CAT', name: 'BALENCIAGA'},
{ rectype: 'CAT', name: 'جديدنا'},
{ rectype: 'CAT', name: 'Boots' },
{ rectype: 'CAT', name: 'JIMMY CHOO' },
{ rectype: 'CAT', name: 'Men' },
{ rectype: 'CAT', name: 'New In' },
{ rectype: 'CAT', name: 'Pumps' },
{ rectype: 'CAT', name: 'ADIDAS' },
{ rectype: 'CAT', name: 'جيمي تشو' },
{ rectype: 'CAT', name: 'Sneakers' },
{ rectype: 'CAT', name: 'Bags & Accessories' },
{ rectype: 'CAT', name: 'Baby' },
{ rectype: 'CAT', name: 'NA' },
{ rectype: 'CAT', name: 'Women' },
{ rectype: 'CAT', name: 'NIKE' },
{ rectype: 'CAT', name: 'Kids' },
{ rectype: 'CAT', name: 'Flats' },
{ rectype: 'CAT', name: 'Heels' },
{ rectype: 'CAT', name: 'Loafers & Slippers' },
{ rectype: 'CAT', name: 'CHANEL' },
{ rectype: 'CAT', name: 'Card Holders' },
{ rectype: 'CAT', name: 'Bags' },
];
console.log(
sampleData.filter(({ name }) =>
(/^\W+$/).test(name) ||
(/^[\p{Lu}\p{Z}]+$/u).test(name)
)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
Any other filter condition can be covered by a blacklist based approach (where one checks the current values against a list of not allowed values) which one combines with the former uppercase testing ...
const sampleData = [
{ rectype: 'CAT', name: 'Sandals'},
{ rectype: 'CAT', name: 'Shoes'},
{ rectype: 'CAT', name: 'Mules'},
{ rectype: 'CAT', name: 'BALENCIAGA'},
{ rectype: 'CAT', name: 'جديدنا'},
{ rectype: 'CAT', name: 'Boots' },
{ rectype: 'CAT', name: 'JIMMY CHOO' },
{ rectype: 'CAT', name: 'Men' },
{ rectype: 'CAT', name: 'New In' },
{ rectype: 'CAT', name: 'Pumps' },
{ rectype: 'CAT', name: 'ADIDAS' },
{ rectype: 'CAT', name: 'جيمي تشو' },
{ rectype: 'CAT', name: 'Sneakers' },
{ rectype: 'CAT', name: 'Bags & Accessories' },
{ rectype: 'CAT', name: 'Baby' },
{ rectype: 'CAT', name: 'NA' },
{ rectype: 'CAT', name: 'Women' },
{ rectype: 'CAT', name: 'NIKE' },
{ rectype: 'CAT', name: 'Kids' },
{ rectype: 'CAT', name: 'Flats' },
{ rectype: 'CAT', name: 'Heels' },
{ rectype: 'CAT', name: 'Loafers & Slippers' },
{ rectype: 'CAT', name: 'CHANEL' },
{ rectype: 'CAT', name: 'Card Holders' },
{ rectype: 'CAT', name: 'Bags' },
];
console.log(
sampleData.filter(({ name }) =>
(/^\W+$/).test(name)
|| (
(/^[\p{Lu}\p{Z}]+$/u).test(name)
&& !['NA'].includes(name)
)
)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }