Filling columns dynamically in React based on column-headings - javascript

I have some data coming in this form :
{"L":405.0,"M":840.0,"S":2217.0,"XL":27.0,"XS":2748.0}
{"L":40.0,S":17.0,"XL":2.0,"XS":28.0}
{"L":5.0,"M":8.0,"S":17.0}
Some data may have L missing, some may have XL missing. I have to generate a table with column headings as all possible key values and for each line I have to write the corresponding values for that cell. eg here the output will be:
XS
S
M
L
XL
2748
2217
840
405
27
28
17
-
40
2
-
17
8
5
-
For the headings, I parsed through all data, and stored all the keys/column-headings in a set, now I parse each data line-by-line, how do I fill the column values?

Related

How to find the javascript code responsible changing data in an HTML element (based on JSON)?

I am scraping a website where there are clothes with javascript rendered size tables.
Sometimes the sizes are in number format like 38, 40, 50 and sometimes in character format like S, M, L etc.
Examples for these 2 types:
https://www2.hm.com/hu_hu/productpage.0872568004.html ->
Available sizes from the khaki colored one: XS, L
https://www2.hm.com/hu_hu/productpage.0881349001.html -> Available sizes from the beige colored one: 40, 42, 44
The size availability data for the first one (khaki color) comes from this json:
https://www2.hm.com/hmwebservices/service/product/hu/availability/0872568.json
In this list the first seven characters (0872568) are the base product code, the second 3 characters are the color code, and the last 3 characters are the sizes. This means we have sizes like:
002 = XS
005 = L
The size availability for the second coat comes from this json:
https://www2.hm.com/hmwebservices/service/product/hu/availability/0881349.json
based on the previously mentioned logic for this we have sizes like:
005 = 40
006 = 42
007 = 44
As you can see the same looking availability data once mean size L, then mean size 40.
I want to find the code responsible for deciding which format the user will see on the frontend based on this json data.
I went through the source code but I can not find the information I need.
What I've done is to use the recording in Chrome's Developers tool when clicking on the sizes field and looking for the product code, the json url but there is nothing there. Is there any other way I can check what function is called when I click on the size field? I guess the way it handles the json file must be hidden there.
Any guidance would be greatly appreciated.

How to make permutations satisfying conditions using google apps script?

I'm trying to get permutations of a column from one sheet automatically updated in another sheet (of the same spreadsheet) - while satisfying some conditions.
Eg. This is a dummy table with dummy values for example to illustrate my problem/goal.
Sheet 1
a 2 3
b 4 8
c 3 5
d 11 7
e 7 15
I want to have combinations of column1 such that the sum of values of their column2 is say 20 +/- 5 and column3 is 15 +/- 3. In order to achieve this, the combinations can include multiples of one element too - like 1/2 a or 3 b.
So, for example, one set of combinations in Sheet 2 would be
column1 = a + d + 1/2 e
column2 = 16.5
column3 = 17.5
I've only ever done basic coding so far and I really have no idea how to go about this, any help would be much appreciated :(

Gleaning data from tab-delimited txt file with missing values

I need to get make a json file from a whitespace-delineated txt file.
However:
1. the whitespaces are inconsistent in length and
2. some of the data of each "column" is missing.
A single row looks like this in the txt file:
5653 Phrakhtaes Phrakhtaes 34.56717 33.02724 L LCTY GB 05 0 32 Asia/Nicosia 2014-09
Ultimately, this data will go onto Redis. But without some means of creating keys for each "column", I don't see how I can work with this data.
Please, I could really use the help!
Thanks in advance!
Simply just split where there are 2 or more spaces in between your data:
var line = "5653 Phrakhtaes Phrakhtaes 34.56717 33.02724 L LCTY GB 05 0 32 Asia/Nicosia 2014-09";
console.log(line.split(/ +/));
As far as data missing, I'd recommend you just check the length of the array, and < the number of expected results, you simply discard. The only other option is to loop through, and judge which one may be missing (Based on string type, if it's in integer, uppercase, etc...) if there are a variable number of spaces in between data points.

How to Highlight Specific Column Values based on a CSS Class and Binary Digit On/Off Positions

Using Oracle APEX v5.1.2.
Unsure how to tackle the following but I have a table called, flag_defs with the following example data:
ID NAME
------- ------
1 A
2 B
3 C
4 D
5 E
6 F
Based on the above table, I need to display all these names with a report region but not on separate lines but in the following fashion:
A B C D
E F
where I'm only showing 4 names across.
Now based on the following binary convention, say the following:
010010
where the first 0 lines up to ID = 1 and the last 0 in this sequence lines up to ID = 6
Based on this binary sequence, which will be stored within a database field in another table, I need to apply a class called "flag-red" that I will define as color:red;font-weight:bold; to the names that have the ID position set to "1".
So in the above example binary sequence, both "B" and "E" only would receive the class of "flag-red" and would be red/bold within the report region. The others would not.
The same goes with removing the class if the digit "1" is reset back to "0" for that ID.
I would need to iterate through each digit in this field to set the correct class in my report.
I'm assuming I would firstly create a report and assign a span class to each name but unsure if this is the correct approach.
Furthermore, would JavaScript be a better option or stick to SQL
Would appreciate some assistance on how to tackle the above.
I would suggest you to make a report in the following way:
Lets say you have a query which returns some rows with rownum column. Adding a pivot to this query we can turn it into the following:
select *
from (select r, mod(rownum, 6) group_no, floor((rownum - 1)/6) row_id
from (select rownum r
from dual connect by level <= 30) t)
pivot(max(r) for group_no in (1, 2, 3, 4, 5, 0))
order by 1
Now we can join this query with the table which stores binary mask to show. Lets say initial query has columns C1, C2, ... C6with data, and joined table with mask data has columns M1, M2, ... M6. (If it is one column with a mask, we can produce 6 columns using calculating expressions) You can calculate a name of a CSS class in these columns, something like
select ...
case when <expression1> then 'flag-red' else 'flad-green' end M1,
...
Next step is to go to report properties, choose column C1 in the column list, go to Column formatting section, put #M1# in the CSS Classes field:
Also, mark M1, M2, ... M6 columns as hidden.

Text Grouping Algorithm

Given an arbitrary string of text, the task is to group the text into separate sections of a template. Each section has different min length and max length parameters. A solution can be considered optimal for a section as long as it falls within those bounds. A greedy solution might result in some sections not meeting their minimums, which means the solution as a whole is not acceptable.
I'm having trouble efficiently constructing an algorithm to do this. It seems that a dynamic programming approach might help, but thus far, I haven't been able to couch it in dynamic programming terms. Does anyone have any leads on solving this problem?
function groupText(str, template)
Inputs:
str: a string of text
template: array of JavaScript objects.
One object per section that describes the min/max amount of text allowed
Output:
array: each element corresponds to one section.
The value of the element is the text that is in the section.
As an example, let's define a string str that is equal to "This is a test." We also have a template t. t consists of several sections. Each section s has a minimum and maximum amount of characters allowed. Let's say for this example there are only two sections: s1 and s2. s1 has a minimum of 1 character and a maximum of 100. s2 has a minimum of 10 characters and a maximum of 15. We pass our string str and our template t to a function groupText. groupText must return an array, with each element i corresponding to a section. For example, element 0 will correspond to s1. The value of the element will be the text that has been assigned to the section.
In this example, a solution might be.
s1text = "This "
s2text = "is a test."
If I understood the problem correctly there's no need of any search... just subtract from the total length the sum of the minimum lengths and what remains is the amount to be distributed. Then distribute this amount to each element up to its maximum until nothing is left... in code
var minsum = 0;
for (vsr i=0; i < sections.length; i++)
minsum += sections[i].min_size;
var extra = text.length - minsum;
if (extra < 0) return null; // no solution
var solution = [];
for (var i=0; i < sections.length; i++)
{
var x = sections[i].min_size + extra;
if (x > sections[i].max_size)
x = sections[i].max_size;
solution.push(x);
extra -= x - sections[i].min_size;
}
if (extra > 0) return null; // no solution
return solution;
OK, so here's an ad-hoc, untested algorithm. If it's no good, perhaps it's good enough to goad someone else into a better answer;
Let's have some trial data. Suppose your template comprises 6 sections, which have min,max limits as:
1 - 12
13 - 25
5 - 7
6 - 7
5 - 5
10 - 25
which means that you're going to need a string of at least 40 and at most 81 characters to satisfy your constraints. And therein lies the solution. First, compute a table like this:
40 - 81
39 - 69
26 - 34
21 - 37
15 - 30
10 - 25
in which each row gives the total length of string that can still be partitioned across the 'slots' in your template. Into slot 1 you put text so that you still have between 39 and 69 characters left for the rest of the slots. Into slot 2 you put text so that you still have between 26 and 34 characters. And so on.

Categories

Resources