Razor: adding dynamic values together - razor

I have a set of values that are generated dynamically with a foreach loop, how can I add these into one value?
For instance, say I have a site where each node has a number associated with it. How can I add all these numbers together? So far I've figured it'd be something similar to the following, where the value of 'node.aNumberValue' is added to the next one, and so on:
#foreach (var x in nodes){
var total = node.aNumberValue + node.aNumberValue (etc...);
<p>#total</p>
}

This is what you want, I think:
int total = 0;
#foreach (var x in nodes)
{
total += x.aNumberValue;
}
<p>#total</p>
Or even better, just:
<p>#nodes.Sum(x => x.aNumberValue)</p>

Related

How to render more than X elements in react? (Performance)

I have a huge component that can receive a start and end date, plus some floors numbers, and based on these numbers, it generates a table, something like the below image.
Example of this table:
The problem is, how to make this table generate faster?
To generate the floors I use a for a loop.
JavaScript code:
for (let i = 0; i < section.floorQuantity; i++) {
floors.push(i + 1)
}
and for each day, I generate a column with the number of floors as cells (divs).
you can use "useMemo" to cache the same computation and it's result change when input data changes
const computeExpensiveValue=(inputs)=>{
//do something
return result
}
const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);

Angular: Posting Data to a Table with a loop and giving a problem when posting the i of the loop into the table

Good day,
I am trying to get data into a table, with the tour_id and every single media_id (the station_id i am getting from somewhere else), the ordernumber is what is giving me a headache:
I am trying to get every station one number for every media i am posting.
For example:
station 1 has 2 medias
and station 2 has 3
then the odernumbers should be like this: 0, 0, 1, 1, 1
I am using the following Code at this moment:
for(var i = 0; i < this.currentStations.length; i++){
this.http.get("http://localhost:3000/mediasforstation/" + this.currentStations[i].id).subscribe((res) => {
medias = res;
for (var j = 0; j < medias.length; j++){
this.http.post("http://localhost:3000/posttourstations",
{"tour_id": id, "media_id": medias[j].id, "ordernumber": i}).subscribe(function(res) {
console.log(res);
}.bind(this));
}
});
}
Everything but the ordernumber works, however, the ordernumber always takes the number of stations involved, in our example above it would be 2.
How do I fix this?
Thank you very much for your help.
As I understand, you need to keep the index value. The type of variable i is var which is function scoped. Within outer loop, you are calling an API that returns some response, meanwhile the value of i is updated and for next index/counter, the API call has been sent. When you get response from API calls, you get the value of i where the outer loop has been called of.
In other words, you need to understand the difference between var and let. Your problem can be solved by replacing
for(var i=0;...)
with
for(let i=0;...)
Here's providing you the sample code.
//block scoped - retains value of i
for (let i=0;i<10;i++){
this.http.get('https://jsonplaceholder.typicode.com/users').subscribe(res=>{
for(var j=0;j<5;j++){
console.log(`i=>${i}`)
}
})
}
//function scoped - gets updated value of i
for (var i=0;i<10;i++){
http.get('https://jsonplaceholder.typicode.com/users').subscribe(res=>{
for(var j=0;j<5;j++){
console.log(`i=>${i}`)
}
})
}

Using splice() to remove items that matches condition from array

Im trying to via a for-loop remove all the items that matches a condition in the state array. But it seems to only be removing the last items in the array and not the ones that matches. Am I using the .splice() incorrectly? Thanks in advance. Code is:
rmTravel() {
for(var i = 0; i < this.cards.length; i++){
if(this.cards[i].sg_categories.includes("travel")){
this.cards.splice(i, 1);
console.log('Removed following card:', this.cards[i].slug)
}
}
console.log('Cards in cards state: ', this.cards)
}
This is a bit of a classic problem; you're iterating forward and shrinking the array at the same time so you're going to end up skipping over records.
I suggest using Array.prototype.filter() instead
this.cards = this.cards.filter(({ sg_categories }) =>
!sg_categories.includes('travel'))
This will reduce the array to entries who's sg_categories property does not include "travel".

Webmatrix Razor Summing numbers in foreach loop

Inside a foreach loop i have a statement
#(var = var1 + var2 ) ;
the math occurs but the results var gets displayed on the page after each execution. I don't want that to happen just do the math so I can display results at the bottom of the table.
thanks
You need to remove the # symbol as when you're in your for each loop, you're already in a code block.
Your code should look a little like this:
#foreach (var i in a)
{
var x = i + i;
}

Compare two different arrays in Actionscript 3 using For Loop within a For Loop

basically I need a loop within a loop to compare two different arrays in my actionscript3 lottery game.
I have attempted the loop but I cannot seem to get it to work ...
check_win.addEventListener(MouseEvent.CLICK, f_check_win);
function f_check_win(event:Event):void{
for(index = 0; index < matches[index]; index++ ){
trace(index);
for(index2 = 0; index2 < input_array.length; index2++){
if (match[index2] == input_array[index2]){
choose_change = choose_change+1;
}
}
}
So basically within this code check_win is a button. Once the button is clicked it runs the loop. It is meant to take an instance of matches which contains 6 properties and loop until index is greater than matches. According to my output this is happening but the second loop doesn't appear to do anything. Any help is greatly appreciated.
Based on the code you've provided, it looks like your inner loop should look more like this:
for(index2 = 0; index2 < input_array.length; index2++){
if (matches[index] == input_array[index2]){
choose_change = choose_change+1;
}
}