Reducer is adding items in the store out of nowhere - javascript

So I am trying to add a cart feauture to my React-redux site and I got stuck on a very weird occurance. So this is what I get from the payload of the action for example:
{
info: 'Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops',
id: 1,
price: 109.95,
image: 'https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg',
count: 5,
totalPrice: 549.75
}
So what Im trying to do is, when an item with the same id as this one is trying to be added, to not add it, but to increase the count of the item with the same id that already exists in the cart:
const index = state.currentCart.findIndex((x) => x.id === id);
return {
...state,
currentCart: [
...state.currentCart,
state.currentCart[index].count += 1,
(state.currentCart[index].totalPrice =
state.currentCart[index].price * state.currentCart[index].count),
],
};
The count itself is increased, but there is something really strange happening at the same time.
The total price of the product and its count are also added as elements of the currentCart array, when the only thing that should happen is to update the count of the cart item with the id from the payload,
this is what happens to the currentCart array when this action is fired:
currentCart: [
{
info: 'Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops',
id: 1,
price: 109.95,
image: 'https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg',
count: 6,
totalPrice: 659.7
},
2,
219.9,
3,
329.85,
4,
439.8,
5,
549.75,
6,
659.7
]
}
I am sure I am not mutating the state right, thank you in advance!

No, they are not coming from nowhere, you are actively adding the values to the array.
You seem to be a bit confused about how to properly handle states. You either choose an immutable approach (which I really recommend if you are using react) or you choose to mutate your references.
In javascript, when you do an assignment, that assignment also returns the value that is being assigned, so for example here:
let x = 1
let b = x+=1
// b is now 2 and x is 2
let c = b += 2
// b is now 4 and c is also 4
That is exactly what is happening on your array assignment. You are first spreading the old version of the array on the new one (making a copy) and then you mutate the reference to the current car at the same time (and this is the key part) that you are saving the return value of those assignments in the array itself.
Take a look at the values on the array, they are the results of your operations:
count (1) += 1 // 2
price (109.95) * count (2) = 219.9,
count (2) += 1 // 3
price (109.95) * count (3) = 329.85
... etc
So what you have on your array is an historic of the count and total price values.
This is a breakdown of what is happening in your code:
// Will allways be at index 0, because you add it as first element
// and then you keep copying the array below
const index = state.currentCart.findIndex((x) => x.id === id);
return {
...state,
currentCart: [
// Here you are copying the old array into the new one,
// keeping the current car at the first position
...state.currentCart,
// Here you are updating the values of the object at index 0
// and at the same time you are adding those values at
// the end of the array
state.currentCart[index].count += 1,
(state.currentCart[index].totalPrice =
state.currentCart[index].price * state.currentCart[index].count),
],
};
What you want to do is to build a new currentCart each time and. Also you want to use an object for currentCart, not an array. If you want to keep a list of items in the cart, I suggest you tu create a nested property on the cart called items, and make that be an array.
Your code example is not showing us where are you getting the action from, but I will provide you an example assuming you just have it and that the new item to add to the cart comes in the payload.
const currentCart = state.currentCart;
const newItem = action.payload
return {
...state,
currentCart: {
...currentCart,
count: currentCart.count + 1
totalPrice: (newItem.price * newItem.count) + currentCart.totalPrice,
items: [...currentCart.items, newItem]
},
};

I am not sure but this is happening
totalPrice = item's price * no of times the item is added
Other items' price is not getting included. Try this -
state.currentCart[index].totalPrice += state.currentCart[index].price * state.currentCart[index].count
(just '+=' instead of '=')

Related

Get the sum per date of nested objects in an array

I have an array with nested objects that looks like the one below.
What I'd like to do is loop through it calculate the sum of each item per date.
For example pc + screen = ?
I cannot seem to figure out how to do it properly. I have found this solution, it works great in console.log() but I cannot figure out how to output the result in a div. Should I use a map function ?
const amountPerDate = data.forEach(function (i) {
const sum = i.item.reduce(function (sum, elem) {
return sum + elem.price;
}, 0);
console.log("the total sum is " + sum);
});
The array:
The code you have posted doesn't seem quite right since forEach won't return anything, and the inner variable sum is not actually available for React to render since it is not in scope (in JavaScript, variables can not escape their containing function, which is function (i) { -- nothing outside of that function can see it).
You were roughly on the right tracks with needing map since that will return an array that represents an accumulation of the return values in the nested callback.
const amountsPerDate = data.map((i) => {
return i.item.reduce(function (sum, elem) {
return sum + elem.price;
}, 0);
});
amountsPerDate will now be an array of the sums. However, in this process, youve lost the info about which sum correlates to which date. So we need more. We can modify to return both the sum alongside the date (an array of objects, each with a sum and date inside).
const amountsPerDate = data.map((i) => {
return {
sum: i.item.reduce(function (sum, elem) {
return sum + elem.price;
}, 0),
date: i.date
});
Now, you should have something in amountsPerDate that looks like this:
[
{ date: '01/01/2022', sum: 200 },
{ date: '02/01/2022', sum: 30},
]
To display in your react component, it's just a case of rendering it, which will require you to map over this new data and return an element for each entry. You haven't posted your full component, but it will be something like this in your JSX:
<div>
{amountsPerDate.map(sum =>
<div>Date: {sum.date}. Total: {sum.sum}</div>
)}
</div>
Of course you can play with this and move it around as you see fit so it fits however you want it laid out.
It's really worth your time understanding map and the differences with foreach since it's so ubiquitous in functional programming. Foreach and map both loop over each item. But map allows you to return a value within the loop callback, and that value goes on to be part of a new array returned from map that represents that item. You can think of it as a transformation from one array to another -- both with the same length -- but with each item replaced with something of your choosing, calculated from each items original contents.

How to push item in specific index of and array of useState reactJs [duplicate]

I'm attempting to add an object at a specific point in my 'data' array which is this components state. The following isn't working, the array simply gets emptied.
addNewBulletAfterActive = () => {
const array = this.state.data;
const newBulletPoint = {
id: this.state.data.length += 1,
title: 'Click to add'
};
const newData = array.splice(this.state.activeBulletPointId, 0, newBulletPoint);
this.setState({
data: newData
});
}
The idea is that if I have a list of 10 bullet points, the user can click on the 4th bullet point and press enter to add a new bullet point directly after. I've not had any issues adding items to the end of the array but it looks like .splice is causing issues.
I believe this should do what you're after.
function addAfter(array, index, newItem) {
return [
...array.slice(0, index),
newItem,
...array.slice(index)
];
}
This function returns a new array with a new item inserted in the middle. It doesn't mutate your original array and so will play nicely with component's state and Redux.
You can then assign the output from this function to your state.
splice returns spliced items (which is empty since you splice 0 items) and mutates original array.
const newData = array.slice(0); // copy
newData.splice(this.state.activeBulletPointId, 0, newBulletPoint);
this.setState({
data: newData
});
I think this could be an easier and faster method to do this
/*Just plain JS*/
function AddAfter(array, newObject){
array.unshift(newObject);
}
/*In react If updating state*/
var _prev = this.state.your_array; //getting the current value for the state object
var newInfo = {id: 1, more: 'This is a new object'};
_prev.unshift(newInfo);

How do i traverse a children table that have relationship with multiple tables

How do i traverse the child book table when i pass the grandparent_id or parent_id to look up all the related books both in parent book and child book tables. I'm guessing it is more of a data structure problem. I'm using Activerecord to fetch the data from the database.
Tables structure
grand_parent_category
Id name
2 Math
parent_category
Id name
1 Algebra book
child_category
Id name parent_id. grandparent_id.
1. Calculus book 1 2
The normal way is to traverse the entire child books array and search the grandparent_id in the book column.
it would be the same if i choose parent_id
Example
#child_books = child_books.all()
Im passing the #child_books object to the frontend via Gon
Javascript
gon.child_books.forEach((book) => {
If (book.grandparent_id == chosen_grandparent_id) {
// do something
} else if book.parent_id == chosen_parent_id {
// do something
} else if book.parent_id == children_id
});
The result would be. If I choose grandparent_id book. Math
Grandparent = Math
Parent = Algebra, Calculus
Children = Additional Math, Discrete Math
Math
/
Algebra
/ \
Additional Math Discrete math
But this approach is really slow, if the dataset for child book category is huge let say 5000. Then in order to find the relationship I have to traverse one by one
Another approach I was thinking is to use hash
#child_books = child_books.all()
#child_books.index_by(&:id)
This will give this result
{
1: {id: 1, name: “additional mathematics”, parent_id: 1, grandparent_id: 2 }
2: {id: 2, name: “discrete mathematics”, parent_id: 1, grandparent_id: 2 }
}
But this approach can’t be done if I pass the grandparent_id to search for both parent and children books.
What approach should tackle this relationship problem.
TLDR; You problem is not likely searching through the data but in // do something part of your code.
Like #jad said it's very difficult to tell what you are trying to do. What got my attention was when you said if the dataset for child book category is huge let say 5000 5000 is tiny. It's barely even a spec in most cases.
What follows is likely not a direct solution to your problem.
// Let setup some test data, nothing too crazy
// 100K Child Books
// 10K Parent Books
// 100 Grand Parent Books
const getRandomInt = (max) => Math.floor(Math.random() * Math.floor(max))
let grand_parent_category = ''.padStart(100, ' ').split('').map((e, id) => ({id, name: `math:${id}`}))
let parent_category = ''.padStart(10000, ' ').split('').map((e, id) => ({id, name: `additional mathematics:${id}`, parent_id: getRandomInt(grand_parent_category.length)}))
let child_books = ''.padStart(100000, ' ').split('').map((e, id) => ({id, name: `child mathematics:${id}`, parent_id: getRandomInt(parent_category.length)}))
// Not counting the setup in the processing time but it's' near instant
let times = {}
// Map our data and create child buckets for easy lookup
times.startSetup = new Date()
const grandParentMap = new Map(grand_parent_category.map(ele => [ele.id, {...ele, children: []}]))
// While mapping parent add the parent to the grandParent for o(1) lookup
const parentMap = new Map(parent_category.map(ele => {
if (grandParentMap.has(ele.parent_id)) grandParentMap.get(ele.parent_id).children.push(ele.id)
return [ele.id, {...ele, children: []}]
}))
// While mapping child add the child to the parent for o(1) lookup
const childMap = new Map(child_books.map(ele => {
if (parentMap.has(ele.parent_id)) parentMap.get(ele.parent_id).children.push(ele.id)
return [ele.id, ele]
}))
// get a Grand Parent and all his children and grandchildren
const getGrandParent = (grandParentId) => {
if (!grandParentMap.has(grandParentId)) return
let grandParent = {...grandParentMap.get(grandParentId)} // shallow clone - safe based on the data given
grandParent.grandChildren = []
grandParent.children = grandParent.children.map(parentID => {
if (!parentMap.has(parentID)) return parentID
let parent = {...parentMap.get(parentID)} // shallow clone - safe based on the data given
// iterate over the children and map them into grandParent.grandChildren
parent.children.forEach(childID => grandParent.grandChildren.push(childMap.has(childID) ? {...childMap.get(childID)} : childID)) // shallow clone - safe based on the data given
return parent
})
return grandParent
}
times.finishSetup = new Date()
let grandParentId = 42
times.startSearch = new Date()
let results = getGrandParent(grandParentId)
times.finishSearch = new Date()
console.log(`grandParent ${grandParentId} has ${results.children.length} children and ${results.grandChildren.length} grandchildren!`)
console.log(`Setup took ${times.finishSetup - times.startSetup}ms`)
console.log(`Search took ${times.finishSearch - times.startSearch}ms`)
// Sample run
// grandParent 42 has 97 children and 991 grandchildren!
// Setup took 60ms
// Search took 1ms
If our data set gets bigger we may want to optimize the setup. I tried 10B children on my local system and it does take around 6 seconds. For sub 1M children this quick solution works well.

Nested recursion, find all possible piece counts

Given an example input:
[
{"id":1,"currentBlack":1,"currentWhite":0,"max":1},
{"id":2,"currentBlack":0,"currentWhite":1,"max":1},
]
Output all possible states of the input where currentBlack and currentWhite can have a value anywhere in the range from their initial value up to the maximum value.
Correct output for this example:
[
[
{"id":1,"currentBlack":1,"currentWhite":0,"max":1},
{"id":2,"currentBlack":0,"currentWhite":1,"max":1},
],
[
{"id":1,"currentBlack":1,"currentWhite":1,"max":1},
{"id":2,"currentBlack":0,"currentWhite":1,"max":1},
],
[
{"id":1,"currentBlack":1,"currentWhite":1,"max":1},
{"id":2,"currentBlack":1,"currentWhite":1,"max":1},
],
[
{"id":1,"currentBlack":1,"currentWhite":0,"max":1},
{"id":2,"currentBlack":1,"currentWhite":1,"max":1},
]
]
The real input will have max anywhere between 1 and 8 and there will be far more objects within the input array. My attempt is below (heavily commented):
function allPossibleCounts(pieceCounts) {//pieceCounts is the input
var collection = []; //used to collect all possible values
recursiveCalls(pieceCounts); //runs recursive function
return collection; //returns result
function recursiveCalls(pieceCounts) {
//if pieceCounts is already in collection then return, not yet implemented so duplicates are currently possible
collection.push(pieceCounts);//inputs a potential value
console.log(JSON.stringify(pieceCounts));//this is successfully logs the correct values
console.log(JSON.stringify(collection));//collection isn't correct, all values at the top of the array are copies of each other
for (let n in pieceCounts) {//pieceCounts should be the same at the start of each loop within each scope, aka pieceCounts should be the same at the end of this loop as it is at the start
subBlackCall(pieceCounts);
function subBlackCall(pieceCounts) {
if (pieceCounts[n].currentBlack < pieceCounts[n].max) {
pieceCounts[n].currentBlack++;//increment
recursiveCalls(pieceCounts);
subBlackCall(pieceCounts);//essentially you're either adding +1 or +2 or +3 ect all the way up to max and calling recursiveCalls() off of each of those incremented values
pieceCounts[n].currentBlack--;//decrement to return pieceCounts to how it was at the start of this function
}
}
subWhiteCall(pieceCounts);
function subWhiteCall(pieceCounts) {
if (pieceCounts[n].currentWhite < pieceCounts[n].max) {
pieceCounts[n].currentWhite++;
recursiveCalls(pieceCounts);
subWhiteCall(pieceCounts);
pieceCounts[n].currentWhite--;
}
}
}
}
}
But currently my attempt outputs as this ungodly mess of copied arrays
[[{"id":1,"currentBlack":1,"currentWhite":1,"max":1},{"id":2,"currentBlack":1,"currentWhite":1,"max":1}],[{"id":1,"currentBlack":1,"currentWhite":1,"max":1},{"id":2,"currentBlack":1,"currentWhite":1,"max":1}],[{"id":1,"currentBlack":1,"currentWhite":1,"max":1},{"id":2,"currentBlack":1,"currentWhite":1,"max":1}],[{"id":1,"currentBlack":1,"currentWhite":1,"max":1},{"id":2,"currentBlack":1,"currentWhite":1,"max":1}],[{"id":1,"currentBlack":1,"currentWhite":1,"max":1},{"id":2,"currentBlack":1,"currentWhite":1,"max":1}]]
Edit: working code: https://pastebin.com/qqFTppsY
The pieceCounts[n] always reference to the one object. You should recreate the pieceCount for saving in to the collection as different object. For example, you can add
pieceCounts = JSON.parse(JSON.stringify(pieceCounts)); // just clone
at the start of recursiveCalls function.
To avoid conversion to JSON and back, I would suggest using Object.assign to perform a deeper copy in combination with map on the array:
function allPossibleCounts(pieceCounts) {
var result = [],
current = deeperCopy(pieceCounts);
function deeperCopy(arr) {
return arr.map( row => Object.assign({}, row) );
}
function recurse(depth) {
// depth: indication of which value will be incremented. Each "row" has
// 2 items (black/white), so when depth is even, it refers to black, when
// odd to white. Divide by two for getting the "row" in which the increment
// should happen.
var idx = depth >> 1, // divide by 2 for getting row index
prop = depth % 2 ? 'currentWhite' : 'currentBlack', // odd/even
row = pieceCounts[idx];
if (!row) { // at the end of the array
// Take a copy of this variation and add it to the results
result.push(deeperCopy(current));
return; // backtrack for other variations
}
for (var value = row[prop]; value <= row.max; value++) {
// Set the value of this property
current[idx][prop] = value;
// Collect all variations that can be made by varying any of
// the property values that follow after this one
recurse(depth+1);
// Repeat for all higher values this property can get.
}
}
recurse(0); // Start the process
return result;
}
// Sample input
var pieceCounts = [
{"id":1,"currentBlack":1,"currentWhite":0,"max":1},
{"id":2,"currentBlack":0,"currentWhite":1,"max":1},
];
// Get results
var result = allPossibleCounts(pieceCounts);
// Output
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
The idea is to use recursion: imagine the problem can be solved for all variations that can be made for all properties, except the first one. Produce those, and then change the first property value to the next possible value. Repeat again the production of all variations, etc. The combination of all those results together will be the solution for when the first property value should also be varied.
This is an ideal situation for recursion. The recursion stops when there are no more property values remaining: in that case there is only one solution; the one with all the values set as they are. It can be added to the list of results.
The properties can be enumerated like this:
row currentBlack currentWhite
---------------------------------
0 0 1
1 2 3
2 4 5
3 6 7
...
n 2n-2 2n-1
We could call that number depth, and increase it at every step of deeper recursion. Given a depth, the property to vary is defined by:
depth is even => currentBlack
depth is odd => currentWhite
row number = depth / 2 (ignoring the remainder)

Changing local value in function, modifies global state in react

I have react component with a array of a shoppinglist in state.
The shoppinglist have multiple arrays and object to reflect which ingredients matches each day and meal.
This functions loops through each ingredient push it to an empty array which I later maps over and returns the value. When I find an ingredient which already exists in the ingredient-array, then it modifies the value instead of pushing a new similar ingredient. (the reason for this is that a breakfast on monday and tuesday can both have the ingredient "apple", and then I just want to show "apple" in the list once, but have the amount of values to reflect both breakfasts.
The problem:
The value of the ingredient I modify is changed on state-level. Which means when I run the function again (I do because the user can filter days) the value increases.
Scenario
Breakfast in both monday and tuesday has the ingredient Apple. Apple has a value of 2, since the recipe needs two apple. This means my loop should return 4 apples. And it does the first time. But when I change som filters so the loops run again to reflect the new filter, it now shows 6 apples.
Can someone tell me how this is possible? How can this code modify my global state?
loopIngredients (items) {
const ingredients = []
const newArr = items.slice()
newArr.map((data, key) => {
data.map((data, index) => {
const valid = ingredients.find((item) => item.ingredient.id === data.ingredient.id)
if (valid) {
const parentValue = parseInt(ingredients[index].value)
const localValue = parseInt(data.value)
ingredients[index].value = (parentValue + localValue).toString()
} else {
ingredients.push(data)
}
})
})
} // END loopIngredients
Found the solution:
The ingredients.push(data) pushed the reference from the state.
I thought I removed the reference by doing .slice(), but apparently not.
By doing ingredients.push(Object.create(data)) it worked as intended.

Categories