I have an issue of re counting the numbers in strings. I am trying to re calculate title numbers based on it's kindof type. e.g. When I delete Oranges 2
from [Oranges 1, Oranges 2, Oranges 3], it should become [Oranges 1, Oranges 2] i.e. I wanted to re count the numbers irrespective of it's previous number. One can delete any i.e. 1 or 5 or last e.g. say 10
const req = [
{id: 'z1', title:"Oranges 1"},
{id: 'y1', title:"Apples 1"},
// Oranges 2 deleted
{id: 'a1', title: "Oranges 3"},
{id: 'b1', title: "Apples 2"},
// Apples 3 deleted
{id: 'a3', title: "Apples 4"},
{id: 'b2', title: "Oranges 4"},
{id: 'b6', title: "Apples 5"},
{id: 'c3', title: "Oranges 5"},
{id: 'x1', title: "Apples 6"},
];
const titlesWithNoDigits = req.map(tab => {
return { ...tab, title: (tab?.title || '').replace(/[0-9]/g, '').trim() };
});
const res = titlesWithNoDigits.reduce((obj, next) => {
const updatedTitle = `${label} ${(titlesWithNoDigits[label] || 0) 1}`;
return {
...obj,
[next.id]: updatedTitle,
};
}, {});
The response I am looking for is:
{
z1: 'Oranges 1',
y1: 'Apples 1',
// Oranges 2 deleted
a1: 'Oranges 2',
b1: 'Apples 2',
// Apples 3 deleted
a3: 'Apples 3',
b2: 'Oranges 3',
b6: 'Apples 4',
c3: 'Oranges 4',
x1: 'Apples 5',
}
I am trying javascript reduce
function. Can anyone give an idea, please ?
CodePudding user response:
You can store the count in an object mapping it with the title
const req = [
{ id: "z1", title: "Oranges 1" },
{ id: "y1", title: "Apples 1" },
// Oranges 2 deleted
{ id: "a1", title: "Oranges 3" },
{ id: "b1", title: "Apples 2" },
// Apples 3 deleted
{ id: "a3", title: "Apples 4" },
{ id: "b2", title: "Oranges 4" },
{ id: "b6", title: "Apples 5" },
{ id: "c3", title: "Oranges 5" },
{ id: "x1", title: "Apples 6" },
];
const labels = {};
const reg = /[a-zA-z] /;
const response = req.reduce((obj, next) => {
let title = "";
const label = next.title;
title = label.match(reg)?.[0] || "";
if (title) {
if (labels[title]) {
labels[title] = labels[title] 1;
} else {
labels[title] = 1;
}
}
return {
...obj,
[next.id]: `${title} ${labels[title]}`,
};
}, {});
console.log(response)
CodePudding user response:
You need to keep a counter for each fruit when updating the titles. First declare a Map
(or object) to keep the count for each fruit. Then we use map
on the req
object to generate each new object in the array while updating the counter for each fruit.
const req = [
{id: 'z1', title:"Oranges 1"},
{id: 'y1', title:"Apples 1"},
// Oranges 2 deleted
{id: 'a1', title: "Oranges 3"},
{id: 'b1', title: "Apples 2"},
// Apples 3 deleted
{id: 'a3', title: "Apples 4"},
{id: 'b2', title: "Oranges 4"},
{id: 'b6', title: "Apples 5"},
{id: 'c3', title: "Oranges 5"},
{id: 'x1', title: "Apples 6"},
];
// maps fruits to the number of occurrences
let fruitCounters = new Map();
const res = req.map(tab => {
// remove number from title
let bareTitle = tab.title.split(' ')[0];
// update counter (initialize with 0 if key missing)
fruitCounters[bareTitle] = (fruitCounters[bareTitle] ?? 0) 1;
// put together the updated title string
const updatedTitle = `${bareTitle} ${fruitCounters[bareTitle]}`;
return {
id: tab.id,
title: updatedTitle,
}
});
console.log(res)