Match spanish words replace method - javascript

this functions searchs names or categories of this array of objects, i need to ignore spanish letters with 'acento' (for example: i need to match 'Taragüí' if i write taragui), i solve this with the string replace() method, my question: Is there a way to solve this with some unicode method? Thanks in advance.
var fromDB = [
{id: 1, name: 'Almendras', category: 'Frutos secos', price: 25, amount: 0, description: 'asd'},
{id: 2, name: 'Nueces', category: 'Frutos secos', price: 10, amount: 0, description: 'asd'},
{id: 3, name: 'Mermelada', category: 'Jam', price: 15, amount: 0, description: 'asd'},
{id: 4, name: 'Alfajor', category: 'Sweet', price: 20, amount: 0, description: 'asd'},
{id: 5, name: 'Queso', category: 'UwU', price: 45, amount: 0, description: 'asd'},
{id: 6, name: 'Arandanos', category: 'Fruta', price: 50, amount: 0, description: 'asd'},
{id: 7, name: 'Maracuya', category: 'Fruta', price: 50, amount: 0, description: 'asd'},
{id: 8, name: 'Chocolate', category: 'Sweet', price: 50, amount: 0, description: 'asd'},
{id: 9, name: 'Mascarpone', category: 'UwU', price: 50, amount: 0, description: 'asd'},
{id: 9, name: 'Taragüí', category: 'UwU', price: 50, amount: 0, description: 'asd'}
];
const input = document.querySelector('input');
input.addEventListener('input', updateValue);
function updateValue(e) {
realTimeInputValue = e.target.value.toLowerCase();
let productsMatch = fromDB.filter(x => x.name.toLowerCase().replace('á', 'a').replace('é', 'e').replace('í', 'i').replace('ó', 'o').replace('ú', 'u').replace('ü', 'u').includes(realTimeInputValue))
if(productsMatch.length){
console.log(productsMatch)
} else {
let categoriesMatch = fromDB.filter(x => x.category.toLowerCase().includes(realTimeInputValue))
console.log(categoriesMatch);
}
}
<input type="text" placeholder="Enter some text" name="name"/>

Related

Sort aray of objects by another array of objects reference

I need some help to sort this data out, i have an array of products and i need to sort and display by settings configuration. The output must have the same order as settings array (index) and if display is true. Thanks in advance. This is what i tryed:
var products = [
{id: 0, name: 'Chocolate', category: 'Sweet'},
{id: 1, name: 'Almendras', category: 'Fruit'},
{id: 2, name: 'Nueces', category: 'Fruit'},
{id: 3, name: 'Mermelada', category: 'Jam'},
{id: 4, name: 'Alfajor', category: 'Sweet'},
{id: 5, name: 'Queso', category: 'UwU'},
{id: 6, name: 'Arandanos', category: 'Fruit'},
{id: 7, name: 'Maracuya', category: 'Fruit'}
];
let settings = [
{
name: 'Fruit',
display: true
},
{
name: 'Jam',
display: false
},
{
name: 'Sweet',
display: true
},
{
name: 'UwU',
display: true
}
]
let group = products.reduce((r, a) => {
r[a.category] = [...r[a.category] || [], a];
return r;
}, {});
let arrangedProducts = Object.keys(group);
console.log(group);
console.log(arrangedProducts);
This is my expected output:
/*
expected result = [
[
{id: 1, name: 'Almendras', category: 'Fruit'},
{id: 2, name: 'Nueces', category: 'Fruit'},
{id: 6, name: 'Arandanos', category: 'Fruit'},
{id: 7, name: 'Maracuya', category: 'Fruit'}
],
[
{id: 0, name: 'Chocolate', category: 'Sweet'},
{id: 4, name: 'Alfajor', category: 'Sweet'}
],
[
{id: 5, name: 'Queso', category: 'UwU'}
]
]
*/
Solution
Making of groups
Apply settings and retrieve the result
const products = [
{ id: 0, name: "Chocolate", category: "Sweet" },
{ id: 1, name: "Almendras", category: "Fruit" },
{ id: 2, name: "Nueces", category: "Fruit" },
{ id: 3, name: "Mermelada", category: "Jam" },
{ id: 4, name: "Alfajor", category: "Sweet" },
{ id: 5, name: "Queso", category: "UwU" },
{ id: 6, name: "Arandanos", category: "Fruit" },
{ id: 7, name: "Maracuya", category: "Fruit" },
];
const productsGroup = products.reduce((r, a) => {
r[a.category] = [...(r[a.category] || []), a];
return r;
}, {});
function applySettings(settings) {
return settings.filter((s) => s.display).map((s) => productsGroup[s.name]);
}
console.log(
applySettings([
{
name: "Fruit",
display: true,
},
{
name: "Jam",
display: false,
},
])
);
console.log(
applySettings([
{
name: "Fruit",
display: true,
},
{
name: "Sweet",
display: true,
},
{
name: "UwU",
display: true,
},
])
);
You can filter your settings list based on the display property and then use Array.map to return a list of objects in products that match the category:
const products = [
{id: 0, name: 'Chocolate', category: 'Sweet'},
{id: 1, name: 'Almendras', category: 'Fruit'},
{id: 2, name: 'Nueces', category: 'Fruit'},
{id: 3, name: 'Mermelada', category: 'Jam'},
{id: 4, name: 'Alfajor', category: 'Sweet'},
{id: 5, name: 'Queso', category: 'UwU'},
{id: 6, name: 'Arandanos', category: 'Fruit'},
{id: 7, name: 'Maracuya', category: 'Fruit'}
];
const settings = [
{ name: 'Fruit', display: true },
{ name: 'Jam', display: false },
{ name: 'Sweet', display: true },
{ name: 'UwU', display: true }
];
const result = settings
.filter(c => c.display)
.map(c => products.filter(o => o.category == c.name));
console.log(result);
Note that this code does filter the products array for each settings value that has display:true, so may be slow for large arrays. However filter is pretty low overhead and testing with OP's sample data shows this to run 3x the speed of the reduce version; and with a larger products array (99 entries) to run 10x faster.
This should be pretty quick, because it continues on to the next iteration without executing the inner loop when display is false:
var products = [
{id: 0, name: 'Chocolate', category: 'Sweet'},
{id: 1, name: 'Almendras', category: 'Fruit'},
{id: 2, name: 'Nueces', category: 'Fruit'},
{id: 3, name: 'Mermelada', category: 'Jam'},
{id: 4, name: 'Alfajor', category: 'Sweet'},
{id: 5, name: 'Queso', category: 'UwU'},
{id: 6, name: 'Arandanos', category: 'Fruit'},
{id: 7, name: 'Maracuya', category: 'Fruit'}
];
let settings = [
{
name: 'Fruit',
display: true
},
{
name: 'Jam',
display: false
},
{
name: 'Sweet',
display: true
},
{
name: 'UwU',
display: true
}
];
function sortProducts(){
const r = [];
let i = -1;
for(let s of settings){
if(!s.display){
continue;
}
i++;
for(let o of products){
if(s.name === o.category){
if(r[i]){
r[i].push(o);
}
else{
r.push([o]);
}
}
}
}
return r;
}
console.log(sortProducts());

Count the number of items in each category within a JavaScript object

I have an array containing several hundred objects, each of which has a category. I wish to return an object that lists out the categories with a count of the number of items for each category.
const arr = [
{id: 1, name: 'ford', category: 'vehicle'},
{id: 2, name: 'pig', category: 'animal'},
{id: 3, name: 'dog', category: 'animal'},
{id: 4, name: 'chev', category: 'vehicle'},
{id: 5, name: 'cat', category: 'animal'},
{id: 6, name: 'jeep', category: 'vehicle'},
{id: 7, name: 'honda', category: 'vehicle'}
]
How would I loop through the object and create a new object that contains just the two categories and how many of each per category?
Desired output:
{vehicle: 4, animal: 3}
Code:
const arr = [
{id: 1, name: 'ford', category: 'vehicle'},
{id: 2, name: 'pig', category: 'animal'},
{id: 3, name: 'dog', category: 'animal'},
{id: 4, name: 'chev', category: 'vehicle'},
{id: 5, name: 'cat', category: 'animal'},
{id: 6, name: 'jeep', category: 'vehicle'},
{id: 7, name: 'honda', category: 'vehicle'}
]
const final = {};
arr.forEach((v) => {
const tst = v.category;
console.log(tst);
if (tst in final){
console.log('found one');
}
});
//console.log(final);
You can use reduce
const arr = [
{id: 1, name: 'ford', category: 'vehicle'},
{id: 2, name: 'pig', category: 'animal'},
{id: 3, name: 'dog', category: 'animal'},
{id: 4, name: 'chev', category: 'vehicle'},
{id: 5, name: 'cat', category: 'animal'},
{id: 6, name: 'jeep', category: 'vehicle'},
{id: 7, name: 'honda', category: 'vehicle'}
]
const categories = arr.reduce((acc, cur) => {
acc[cur.category] = (acc[cur.category] || 0) + 1
return acc;
}, {})
console.log(categories)
edit:
Now, after a year a would wrt this like that
const arr = [
{id: 1, name: 'ford', category: 'vehicle'},
{id: 2, name: 'pig', category: 'animal'},
{id: 3, name: 'dog', category: 'animal'},
{id: 4, name: 'chev', category: 'vehicle'},
{id: 5, name: 'cat', category: 'animal'},
{id: 6, name: 'jeep', category: 'vehicle'},
{id: 7, name: 'honda', category: 'vehicle'}
]
const categories = arr.reduce((acc, cur) => Object.assign(acc, {
[cur.category]: (acc[cur.category] || 0) + 1,
}), {})
console.log(categories)
It looks like the category will always exist, so you don't need to check whether it exists, but what it contains; take what it contains and increment that property on the final object:
const arr = [
{id: 1, name: 'ford', category: 'vehicle'},
{id: 2, name: 'pig', category: 'animal'},
{id: 3, name: 'dog', category: 'animal'},
{id: 4, name: 'chev', category: 'vehicle'},
{id: 5, name: 'cat', category: 'animal'},
{id: 6, name: 'jeep', category: 'vehicle'},
{id: 7, name: 'honda', category: 'vehicle'}
]
const final = {};
for (const { category } of arr) {
final[category] = (final[category] || 0) + 1;
};
console.log(final);
You have the right idea regarding looping over the array and checking if the category was already encountered. What you're missing is initializing a counter when you find a new category and incrementing it the next time that category is encountered:
const arr = [
{id: 1, name: 'ford', category: 'vehicle'},
{id: 2, name: 'pig', category: 'animal'},
{id: 3, name: 'dog', category: 'animal'},
{id: 4, name: 'chev', category: 'vehicle'},
{id: 5, name: 'cat', category: 'animal'},
{id: 6, name: 'jeep', category: 'vehicle'},
{id: 7, name: 'honda', category: 'vehicle'}
]
const final = {};
arr.forEach((v) => {
const cat = v.category;
if (cat in final) {
final[cat]++;
} else {
final[cat] = 1;
}
});
console.log(final);
const arr = [
{ id: 1, name: 'ford', category: 'vehicle' },
{ id: 2, name: 'pig', category: 'animal' },
{ id: 3, name: 'dog', category: 'animal' },
{ id: 4, name: 'chev', category: 'vehicle' },
{ id: 5, name: 'cat', category: 'animal' },
{ id: 6, name: 'jeep', category: 'vehicle' },
{ id: 7, name: 'honda', category: 'vehicle' },
]
// this will hold the results
const result = {}
for (const item of arr) {
// we have not encountered such category before
if (result[item.category] === undefined) {
// setting this category to 1
result[item.category] = 1
// we encountered such category before
} else {
// addint +1 to it
result[item.category] += 1
}
}
console.log(result)

How to duplicate an object in an array by given quantity, ES6 and above

I'm trying to convert an array of objects where i return duplicated objects if the object properties quantity is greater than 1.
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
// desired return
[
{ id: 1, name: "Scissor", price: 2 }
{ id: 1, name: "Scissor", price: 2 }
{ id: 1, name: "Scissor", price: 2 }
{ id: 2, name: "Hat", price: 6.5}
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
]
My code:
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
let newObjects= [];
Object.entries(objects).forEach(([key, value]) => {
for (let i=0; i < value.quantity; i++){
newObjects.push({ id: value.id, name: value.name, price: value.price})
}
});
console.log(newObjects);
So my code above does work, does return what i wanted, however i feel like there is a better/smoother and more of ES6 and beyond method. Could anyone please suggest a better way?
You could use .fill() and .flatMap().
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
let newObjects = objects.flatMap(e=>
Array(e.quantity).fill({id: e.id, name: e.name, price: e.price})
);
console.log(newObjects);
You can use an array reduce along with an array fill.
The map is required only if you want to have unique references otherwise you can fill using the same object.
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
const output = objects.reduce((a, c) => {
return a.concat(Array(c.quantity).fill({}).map(x=>({
id: c.id,
name: c.name,
price: c.price
})))
}, []);
console.log(output)

What is the good way to loop through a javascript array? [duplicate]

This question already has answers here:
Split array into chunks
(73 answers)
Closed 5 years ago.
Consider the following JavaScript Array:
const allRows = [
{id: 1, name: Name 1},
{id: 2, name: Name 1},
{id: 3, name: Name 1},
{id: 4, name: Name 1},
{id: 5, name: Name 1},
{id: 6, name: Name 1},
{id: 7, name: Name 1},
{id: 8, name: Name 1},
{id: 9, name: Name 1},
{id: 10, name: Name 1},
{id: 11, name: Name 1},
{id: 12, name: Name 1},
{id: 13, name: Name 1},
{id: 14, name: Name 1},
{id: 15, name: Name 1},
{id: 16, name: Name 1},
{id: 17, name: Name 1},
{id: 18, name: Name 1},
{id: 19, name: Name 1},
{id: 20, name: Name 1},
{id: 21, name: Name 1},
{id: 22, name: Name 1},
{id: 23, name: Name 1},
{id: 24, name: Name 1},
{id: 25, name: Name 1},
{id: 26, name: Name 1},
{id: 27, name: Name 1},
{id: 28, name: Name 1},
{id: 29, name: Name 1},
{id: 30, name: Name 1},
];
// want to loop through the array and convert it to the array as written below
let rowsPaginated = [
[
{id: 1, name: Name 1},
{id: 2, name: Name 1},
{id: 3, name: Name 1},
{id: 4, name: Name 1},
{id: 5, name: Name 1},
{id: 6, name: Name 1},
{id: 7, name: Name 1},
{id: 8, name: Name 1},
{id: 9, name: Name 1},
{id: 10, name: Name 1}
],
[
{id: 11, name: Name 1},
{id: 12, name: Name 1},
{id: 13, name: Name 1},
{id: 14, name: Name 1},
{id: 15, name: Name 1},
{id: 16, name: Name 1},
{id: 17, name: Name 1},
{id: 18, name: Name 1},
{id: 19, name: Name 1},
{id: 20, name: Name 1}
],
[
{id: 21, name: Name 1},
{id: 22, name: Name 1},
{id: 23, name: Name 1},
{id: 24, name: Name 1},
{id: 25, name: Name 1},
{id: 26, name: Name 1},
{id: 27, name: Name 1},
{id: 28, name: Name 1},
{id: 29, name: Name 1},
{id: 30, name: Name 1}
]
];
I am thinking to convert this way -
for (let i = 0; i < allRows.length; i++) {
console.log(allRows[i]);
}
But I don't think it will be a good way to do it.
What is the best way to loop through the allRows array and convert as rowsPaginated array?
Use array's slice() with the size you want to cut from the array. Try the following:
const allRows = [
{id: 1, name: 'Name 1'},
{id: 2, name: 'Name 1'},
{id: 3, name: 'Name 1'},
{id: 4, name: 'Name 1'},
{id: 5, name: 'Name 1'},
{id: 6, name: 'Name 1'},
{id: 7, name: 'Name 1'},
{id: 8, name: 'Name 1'},
{id: 9, name: 'Name 1'},
{id: 10, name: 'Name 1'},
{id: 11, name: 'Name 1'},
{id: 12, name: 'Name 1'},
{id: 13, name: 'Name 1'},
{id: 14, name: 'Name 1'},
{id: 15, name: 'Name 1'},
{id: 16, name: 'Name 1'},
{id: 17, name: 'Name 1'},
{id: 18, name: 'Name 1'},
{id: 19, name: 'Name 1'},
{id: 20, name: 'Name 1'},
{id: 21, name: 'Name 1'},
{id: 22, name: 'Name 1'},
{id: 23, name: 'Name 1'},
{id: 24, name: 'Name 1'},
{id: 25, name: 'Name 1'},
{id: 26, name: 'Name 1'},
{id: 27, name: 'Name 1'},
{id: 28, name: 'Name 1'},
{id: 29, name: 'Name 1'},
{id: 30, name: 'Name 1'},
];
let rowsPaginated = [];
var i, j, size = 10;
for (i = 0, j = allRows.length; i < j; i += size) {
rowsPaginated.push(allRows.slice(i, i+size));
}
console.log(rowsPaginated);
Try following code, I corrected your array was few mistakes:
const allRows = [
{id: 1, name: "Name1"},
{id: 2, name: "Name1"},
{id: 3, name: "Name1"},
{id: 4, name: "Name1"},
{id: 5, name: "Name1"},
{id: 6, name: "Name1"},
{id: 7, name: "Name1"},
{id: 8, name: "Name1"},
{id: 9, name: "Name1"},
{id: 10, name: "Name1"},
{id: 11, name: "Name1"},
{id: 12, name: "Name1"},
{id: 13, name: "Name1"},
{id: 14, name: "Name1"},
{id: 15, name: "Name1"},
{id: 16, name: "Name1"},
{id: 17, name: "Name1"},
{id: 18, name: "Name1"},
{id: 19, name: "Name1"},
{id: 20, name: "Name1"},
{id: 21, name: "Name1"},
{id: 22, name: "Name1"},
{id: 23, name: "Name1"},
{id: 24, name: "Name1"},
{id: 25, name: "Name1"},
{id: 26, name: "Name1"},
{id: 27, name: "Name1"},
{id: 28, name: "Name1"},
{id: 29, name: "Name1"},
{id: 30, name: "Name1"}
];
let rowsPaginated = [];
let j = 0;
for (let i = 0, j = allRows.length; i < j; i += 10) {
rowsPaginated.push(allRows.slice(i, i+10));
}
console.log(rowsPaginated);

Get object from array of objects in Javascript

I Have this array:
var arrayExample = [
{productId: 1, quantity: 2, name: example, description: example},
{productId: 1, quantity: 2, name: example, description: example},
{productId: 1, quantity: 2, name: example, description: example},
{productId: 1, quantity: 2, name: example, description: example}];
My question is
How do I get all the items of the array but taking in every object only the productId and quantity?
Thus having an array that contains all the objects but only with the two values?
The number of the objects of the array is variable
Result:
var arrayExampleNew = [
{productId: 1, quantity: 2},
{productId: 1, quantity: 2},
{productId: 1, quantity: 2},
{productId: 1, quantity: 2}];
sorry for my English
You could just map it
var arrayExample = [{
productId: 1,
quantity: 2,
name: 'example',
description: 'example'
}, {
productId: 1,
quantity: 2,
name: 'example',
description: 'example'
}, {
productId: 1,
quantity: 2,
name: 'example',
description: 'example'
}, {
productId: 1,
quantity: 2,
name: 'example',
description: 'example'
}];
var arr = arrayExample.map(function(item) {
return {productId : item.productId, quantity : item.quantity }
});
console.log(arr)
ES2015:
const arrayExampleNew = arrayExample.map(({productId, quantity}) => ({productId, quantity}));

Categories

Resources