Compare two arrays in VueJS with their ID - arrays

I have two arrays the first array is gulod_transaction and the second is gulod_medicine. I want to compare the two arrays with there id’s to minus the quantity from the medicine table to the quantity of transaction table. The main problem is when I add the second row first from table medicine to the transaction table. And click the Save Transaction button. In my console.log it’s minus the first row which is 40 from my first table to transaction table quantity which is equal to 10 and the result becomes 30. I want to minus both quantity if they have the same id’s.
for(let index = 0; index < this.gulod_transactions.length; index++) {
if (this.gulod_transactions[index].id === this.gulod_medicines[index].medicine_id) {
this.medicine_id = this.gulod_transactions[index].id;
this.medicine_name = this.gulod_transactions[index].name;
this.medicine_quantity = (parseInt(this.gulod_medicines[index].quantity) - parseInt(this.gulod_transactions[index].quantity));
this.medicine_price = this.gulod_transactions[index].price;
this.gulod_medicine_barangay_id = 1;
console.log(this.gulod_medicines[index].quantity);
console.log(this.gulod_transactions[index].quantity);
console.log(this.medicine_quantity);
}
}

Related

How can I return a sorted table index as a string?

I'm very unfamiliar working with LUA and LUA tables.
The example I would like to provide is that I want to know the name of the person who has the least amount of money in his/her bank account:
BankAccount = {}
BankAccount["Tom"] = 432
BankAccount["Henry"] = 55
BankAccount["Kath"] = 875
table.sort(BankAccount)
print("BankAccount Element at index 1 is ", BankAccount[1])
Goal: I would like for it to return the string "Henry".
The problem I'm having is that I'm not sure how to structure the table in such a way for it to return the string based on a value. I've also seen people do:
BankAccount = {
{"Tom", 432},
{"Henry", 55},
{"Kath", 875},
}
So I'm not exactly sure how to proceed. Thank you for your help.
You can't sort the first table, as it's a hash table that doesn't have any specific order, but you can sort the second one if you provide a sorting function:
BankAccount = {
{"Tom", 432},
{"Henry", 55},
{"Kath", 875},
}
table.sort(BankAccount, function(a, b) return a[2] < b[2] end)
print(BankAccount[1][1]) -- should print Henry
table.sort takes a function as the second parameter that will receive two values being sorted and needs to return true/false value indicating if the first element needs to go before the second (see table.sort).

IBM ODM - How Iterate over column rows (like in a given example)

How in IBM ODM to iterate through each row of decision table column and insert each value of row into method for calculations and finally check which values returned by method is the smallest.
For example:
1. Step
For every row of column Bn where A = A1 do:
SomeMethod.calculate(B1) return C1 = 10;
SomeMethod.calculate(B2) return C2 = 40;
SomeMethod.calculate(B3) return C3 = 5;
2. Step
And get minimum of Cn values.
In my example it is 5.
Thanks in advance!

how to To extract a two-by-two table for a specific event

function tableFor(event, journal) {
let table = [0, 0, 0, 0];
for (let i = 0; i < journal.length; i++) {
let entry = journal[i], index = 0;
if (entry.events.includes(event)) index += 1;
if (entry.squirrel) index += 2;
table[index] += 1;
}
return table;
}
console.log(tableFor("pizza", JOURNAL));
JOURNAL file
I'm reading eloquent javascript third edition and I find it difficult to understand this code, especially the if conditions. My question is why in the entry.events.includes(event), index should increment by 1 and in entry.squirrel should increment by 2?
I addition, if both ifs in the for loop are true, table[index] += 1 will be incremented or not?
There are 4 states to consider:
If the event in the journal does not contain pizza and is not a squirrel then increment table at index 0
If the event in the journal contains 'pizza' but not squirrel table is incremented at index 1
If the event does not contain 'pizza' but squirrel is true then the table will be incremented at index 2
If BOTH the event contains "pizza" and squirrel is true then the table will be incremented at index 3
This will loop over the JSON object in the JOURNAL file. For each "event" one of these 4 states will be chosen and the table variable will be affected in the above manner.

Replace first 4 rows with values from rows 5:8 arcpy

I am trying to replace the values in the first 4 rows of a attribute table with the row values from the next 4 rows 5:8 in an attribute table using arcpy.da.UpdateCursor. Is there a simple way to index rows 1:4 (all columns) and replace with values from rows 5:8.
Here is my code thus far:
targetFC = r"D:\ZOC\POLYPGDIS_MASTER_1.shp"
dsc = arcpy.Describe(sourceFC)
fields = dsc.fields
# List all field names except the OID field
fieldnames = [field.name for field in fields if field.name != dsc.OIDFieldName]
with arcpy.da.UpdateCursor(targetFC, fieldnames) as cursor:
for row in cursor:
row[1:4] = row[5:8]
cursor.updateRow(row)
Thanks
I think the easiest way would be to load the data into a list.
Note if you are working with a RDBMS the row order is not certain so this probably would not work unless you sorted by something.
eight_rows = []
k = 0
with arcpy.da.SearchCursor(targetFC, fieldnames) as rows:
for row in rows:
eight_rows.append(row)
k += 1
if k == 8: break
k = 5
with arcpy.da.UpdateCursor(targetFC, fieldnames) as rows:
for row in rows:
row = eight_rows[k]
k += 1
rows.updateRow(row)
if k == 8: break

Updating multiple rows with respect to a change in one row

I am using PostgreSQL and let's say I have a tasks table to keep track of task items. Tasks table is as follows;
Id Name Index
7 name A 1
5 name B 2
6 name C 3
3 name D 4
Index column in tasks table stores the sort order of the tasks. Therefore I will output the tasks with respect to index in increasing order.
So When I change Task D(id = 3)' s index into 2 the new indexes should be as below;
Id Name Index
7 name A 1
5 name B 3
6 name C 4
3 name D 2
or when I change Task A(id = 7)' s index into 4 the new indexes should be as below;
Id Name Index
7 name A 4
5 name B 2
6 name C 3
3 name D 1
What I think is updating all row's index values one by one is pretty inefficient.
So what is the most efficient way to update all index values when I change one of the indexes in my Tasks table?
Edit :
First of all sorry for the confusion. What I am asking is not a simple exchanging two row indexes. If you look at the examples when I change Task D's index in to 2 more than one rows change. So when Task D is 2, Task B becomes 3 and Task C becomes 4.
For instance;
It is like when you drag Task D and drop below Task A so that it's index becomes 2 and B and C's index increases by 1.
SQL Fiddle
What you are doing is exchanging two row's indexes. So it is necessary to store the index value of the first updated one in a temp variable and setting it temporarily to a special value to avoid a unique index collision, that is, if the index is unique. If the index is not unique that step is unnecessary.
begin;
create temp table t as
select
(
select index
from tasks
where id = 3
) as index,
(
select id
from tasks
where index = 2
) as id
;
update tasks
set index = -1
where id = (select id from t)
;
update tasks
set index = 2
where id = 3
;
update tasks
set index = (select index from t)
where id = (select id from t)
;
drop table t;
commit;
The following assumes the index column (as well as id) is unique:
with swapped as (
select n1.id as id1,
n1.name as name1,
n1.index as index1,
n2.id as id2,
n2.name as name2,
n2.index as index2
from names n1
join names n2 on n2.index = 2 -- this is the value of the "new index"
where n1.id = 3 -- this is the id of the row where the index should be changed to the new value
)
update names as n
set index = case
when n.id = s.id1 then s.index2
when n.id = s.id2 then s.index1
end
from swapped s
where n.id in (s.id1, s.id2);
The CTE first creates a single row with the ids of the two rows to be swapped and then the update just compares the ids of the target table with those from the CTE, swapping the values.
SQLFiddle example: http://sqlfiddle.com/#!15/71dc2/1

Resources