I've been doing some web-scraping recently and ultimately fetched the data I wanted. However, there is no sense of organization since it's only a simple list in Python. It contains the meeting type (either LE/DI/SE), days, time, professor's name (edited for privacy), among some other values.
["LE", "A00", "MWF", "10:00a-10:50a", "MLK", "AUD", "Smith, John", "976539", "DI", "A01", "F", "5:00p-5:50p", "MLK", "AUD", "Smith, John", "FULL Waitlist(25)", "216", "FI", "03/17/2018", "S", "8:00a-10:59a", "TBA", "TBA", "LE", "B00", "MWF", "1:00p-1:50p", "WLH", "2005", "Smith, John", "927471", "DI", "B01", "F", "6:00p-6:50p", "MLK", "AUD", "Smith, John", "FULL Waitlist(32)", "200", "FI", "03/17/2018", "S", "8:00a-10:59a", "TBA", "TBA"]
As you can see, it's an ugly list. My goal is to make it something like this:
{
"MATH101": {
"LE": {
sectionCode: 'A00',
days: 'MFW',
times: '10:00a-10:50am',
building: 'MLK',
room: 'AUD',
instructor: 'Smith, John',
"DI": {
sectionCode: 'A01',
days: 'F',
times: '5:00-5:50pm',
building: 'MLK',
room: 'AUD',
instructor: 'Smith, John',
availableSeats: 'FULL Waitlist(25)',
capacity: '216'
}
},
"LE": {
sectionCode: 'B00',
days: 'MFW',
times: '1:00a-1:50pm',
building: 'MLK',
room: 'AUD',
instructor: 'Smith, John',
"DI": {
sectionCode: 'B01',
days: 'F',
times: '6:00-6:50pm',
building: 'MLK',
room: 'AUD',
instructor: 'Smith, John',
availableSeats: 'FULL Waitlist(32)',
capacity: '200'
}
}
}
}
There is a discussion per lecture section, thus there are two lecture times and two discussions that pertain to each lecture. I don't know if there is a better schema to store this data, but I could only come up with this.
I thought iterating through the list and saving values starting after the "LE" or "DI" strings into the ordered way that I established above, but I don't see how that would help me create the json file without each key.
I'm new to all this, and I haven't been able to find a solution. Tried converting into dictionary but it doesn't meet my needs. A lot of data is also repeated, such as the professor's name, but there won't always be one professor, nor two lectures/discussions. I plan to do this with all the courses available, so this just gets more and more complicated...
Hopefully someone is able to help out, thank you!
Related
I'm currently working with a an object and loading it into the terminal console with console.log(books)
The code for the object is this.
const books = [
{
id:1,
name: "Harry Potter and the Chamber of Secrets",
authorId: 1
},
{
id: 2,
name: "Harry Potter and the Prisoner of Azkaban",
authorId: 1
},
{
id: 3,
name: "Harry Potter and the Goblet of Fire",
authorId: 1
},
{
id: 4,
name: "The Fellowship of the Ring",
authorId: 2
},
{
id: 5,
name: "The Two Towers",
authorId: 2
},
{
id: 6,
name: "The Return of the King",
authorId: 2
},
{
id: 7,
name: "The Way of Shadows",
authorId: 3
},
{
id: 8,
name: "Beyond the Shadows",
authorId: 3
},
];
It's quite a small problem, but I've noticed that not all of the information is displayed consistently when I log to console. Is this a problem with the git bash terminal or the integration between vscode and git bash and is there anything that can be done to make the presentation more consistent? It's a bit jarring.
I've tried looking for resources on this issue and haven't found anything similar. Would be great to get ideas as even if it's a small thing, when looking through larger data sets, it would be nice to know it will be formatted consistently.
I found a great answer to this on Handy Tips on Using Console Log
You can use Stringify to structure big objects and create consistent formatting.
If I type the following it formats nicely.
console.log(JSON.stringify(books, null, 2))
I am using the xlsx module in node.js. But apparently the xlsx.utils.sheet_to_json returns JSON that is not double quoted; It's either single quoted or not quoted at all. The returned JSON looks like this:
[
{
student_Id: 1,
phone_number: 87654321,
f_name: 'Barns',
l_name: 'Illinois',
address: 'Denmark kurkum mohao street 88 bldg. 29',
},
{ student_Id: 3, f_name: 'Charlie' }
]
In order to extract the Key attributes (Student_id, phone_number...) i need to use the Object.keys method, which only works if the data is double quoted. So is there a way that my outputted data will be double quoted? like this:
[
{
"student_Id": 1,
"phone_number": 87654321,
"f_name": "Barns",
"l_name": "Illinois",
"address": "Denmark kurkum mohao street 88 bldg. 29",
}
]
Even if it nivolves using a different method/module.
Try to put your result in JSON.stringify(your result), so you will get double quoted result.
JSON.stringify({
student_Id: 1,
phone_number: 87654321,
f_name: 'Barns',
l_name: 'Illinois',
address: 'Denmark kurkum mohao street 88 bldg. 29',
})
here is my code , i want to add more than input in the same page because i am using php loop
var input = document.querySelector('input[name="input-custom-dropdown"]'),
tagify = new Tagify(input, {
whitelist: ["A# .NET", "A# (Axiom)", "A-0 System", "A+", "A++", "ABAP", "ABC", "ABC ALGOL", "ABSET", "ABSYS", "ACC", "Accent", "Ace DASL", "ACL2", "Avicsoft", "ACT-III", "Action!", "ActionScript", "Ada", "Adenine", "Agda", "Agilent VEE", "Agora", "AIMMS", "Alef", "ALF", "ALGOL 58", "ALGOL 60", "ALGOL 68", "ALGOL W", "Alice", "Alma-0", "AmbientTalk", "Amiga E", "AMOS", "AMPL", "Apex (Salesforce.com)", "APL", "AppleScript", "Arc", "ARexx", "Argus", "AspectJ", "Assembly language", "ATS", "Ateji PX", "AutoHotkey", "Autocoder", "AutoIt", "AutoLISP / Visual LISP", "Averest", "AWK", "Axum", "Active Server Pages", "ASP.NET", "B", "Babbage", "Bash", "BASIC", "bc", "BCPL", "BeanShell", "Batch (Windows/Dos)", "Bertrand", "BETA", "Bigwig", "Bistro", "BitC", "BLISS", "Blockly", "BlooP", "Blue", "Boo", "Boomerang", "Bourne shell (including bash and ksh)", "BREW", "BPEL", "B", "C--", "C++ – ISO/IEC 14882", "C# – ISO/IEC 23270", "C/AL", "Caché ObjectScript", "C Shell", "Caml", "Cayenne", "CDuce", "Cecil", "Cesil", "Céu", "Ceylon", "CFEngine", "CFML", "Cg", "Ch", "Chapel", "Charity", "Charm", "Chef", "CHILL", "CHIP-8", "chomski", "ChucK", "CICS", "Cilk", "Citrine (programming language)", "CL (IBM)", "Claire", "Clarion", "Clean", "Clipper", "CLIPS", "CLIST", "Clojure", "CLU", "CMS-2", "COBOL – ISO/IEC 1989", "CobolScript – COBOL Scripting language", "Cobra", "CODE", "CoffeeScript", "ColdFusion", "COMAL", "Combined Programming Language (CPL)", "COMIT", "Common Intermediate Language (CIL)", "Common Lisp (also known as CL)", "COMPASS", "Component Pascal", "Constraint Handling Rules (CHR)", "COMTRAN", "Converge", "Cool", "Coq", "Coral 66", "Corn", "CorVision", "COWSEL", "CPL", "CPL", "Cryptol", "csh", "Csound", "CSP", "CUDA", "Curl", "Curry", "Cybil", "Cyclone", "Cython", "Java", "Javascript", "M2001", "M4", "M#", "Machine code", "MAD (Michigan Algorithm Decoder)", "MAD/I", "Magik", "Magma", "make", "Maple", "MAPPER now part of BIS", "MARK-IV now VISION:BUILDER", "Mary", "MASM Microsoft Assembly x86", "MATH-MATIC", "Mathematica", "MATLAB", "Maxima (see also Macsyma)", "Max (Max Msp – Graphical Programming Environment)", "Maya (MEL)", "MDL", "Mercury", "Mesa", "Metafont", "Microcode", "MicroScript", "MIIS", "Milk (programming language)", "MIMIC", "Mirah", "Miranda", "MIVA Script", "ML", "Model 204", "Modelica", "Modula", "Modula-2", "Modula-3", "Mohol", "MOO", "Mortran", "Mouse", "MPD", "Mathcad", "MSIL – deprecated name for CIL", "MSL", "MUMPS", "Mystic Programming L"],
maxTags: 10,
dropdown: {
maxItems: 20,
classname: "tags-look",
enabled: 0,
closeOnSelect: false
}
})
Apply the Tagify initialization on multiple input elements:
new Tagify(input1, ...)
new Tagify(input2, ...)
new Tagify(input3, ...)
So if you have multiple <input> elements in the page, simply find them with js and pass their reference when you initialize the Tagify constructor.
you can make a forEach loop for all inputs
const allInputs = querySelectorAll('input[name="input-custom-dropdown"]');
allInputs.forEach((item) => {
// initialize Tagify on the above input node reference
new Tagify(item);
});
let data = [{
name: "Sarah",
year: ["1924", "1936", "1948", "1960", "1972", "1984", "1996", "2008", "2020", "2032"]
},
{
name: "Ann",
year: ["1925", "1937", "1949", "1961", "1973", "1985", "1997", "2009", "2021", "2033"]
}
];
we assign an array and inside the array has two objects and every object has got another array.
I want to ask user her birth year and select her name from object value from the name.
for example, if she was born in 1949 her name must be Ann or if she was born in 1960 her name must be Sarah.
I am thinking about two days but I couldn't solve this problem.
I don't want to write code if/else or case switches state. is there another code option?
I want to make a correlation between name and years.
I hope I explain well..
I think this is what you're looking for
let data = [{
name: "Sarah",
year: ["1924", "1936", "1948", "1960", "1972", "1984", "1996", "2008", "2020", "2032"]
}, {
name: "Ann",
year: ["1925", "1937", "1949", "1961", "1973", "1985", "1997", "2009", "2021", "2033"]
}];
function yearToName(year) {
return data.find(x => x.year.includes(year)).name;
}
console.log(yearToName("1936"))
console.log(yearToName("1937"))
You could accomplish this using Array.find and Array.includes: find the entry whose years include what you're looking for, then pluck the name from it. Something like this:
function getName (searchYear) {
const entry = data.find(({year}) => year.includes[searchYear]);
return entry?.name;
}
This is maybe not exactly what you wanted, but by setting up a lookup structure you can speed up and simplify the followin search steps. It will behave in a stable way, i. e. for years, where no name was defined an "undefined" will be returned.
let data = [
{name:"Sarah",
year:["1924", "1936", "1948", "1960", "1972", "1984", "1996", "2008", "2020", "2032"]},
{name:"Ann",
year:["1925", "1937", "1949", "1961", "1973", "1985", "1997", "2009", "2021", "2033"]}
];
// setup a looup structure:
data.lookup=data.reduce((a,{name,year})=>(year.forEach(y=>a[y]=name),a),{})
// run a test over many years:
for (let y=1923;y<2034;y++) data.lookup[y] && console.log(y,data.lookup[y]);
You can write a function which will get the name
With these methods
Array.find - to get the single object from the array of objects
Array.includes - to find the passed year present in the array of year
? - Optional chaining , in order getting an error, it will return undefined or null
?? - Nullish coalescing operator, to return the right side when the left side is null or undefined
let data = [{
name: "Sarah",
year: ["1924", "1936", "1948", "1960", "1972", "1984", "1996", "2008", "2020", "2032"]
}, {
name: "Ann",
year: ["1925", "1937", "1949", "1961", "1973", "1985", "1997", "2009", "2021", "2033"]
}];
const getTheName = (year) => data.find(datumn => datumn.year.includes(year))?.name ?? `Don't have a name`
console.log(getTheName('1924')) // Sarah
console.log(getTheName('1949')) // Ann
console.log(getTheName('2020')) //Don't have a name
I've got some data stored this way:
var protossUnitsArray =
[
{ name: "Zealot", health:"100", shield:"50", armor:"1", picture: "/images/protoss/zealotPortrait.png" },
{ name: "Stalker", health: "80", shield: "80", armor: "1", picture: "/images/protoss/stalkerPortrait.png" },
{ name: "Sentry", health: "40", shield: "40", armor: "1", picture: "/images/protoss/sentryPortrait.png" },
{ name: "High Templar", health: "40", shield: "40", armor: "0", picture: "/images/protoss/hightemplarPortrait.png" },
{ name: "Dark Templar", health: "40", shield: "80", armor: "1", picture: "/images/protoss/darktemplarPortrait.png" },
{ name: "Immortal", health: "200", shield: "100", armor: "1", picture: "/images/protoss/immortalPortrait.png" }
];
var protossUnitsList = new WinJS.Binding.List(protossUnitsArray);
var publicMembers =
{
itemList: protossUnitsList
};
WinJS.Namespace.define("ProtossUnitsData", publicMembers);
As you can see, the array has been made publicly available.
How do I actually reference data from this array from another javascript file?
Let's say I want to know what a Stalker's health is (which is 80). Let's assume I know Stalkers are always going to be the second item in the array, shouldn't it be something like
ProtossUnitsData.itemList[1]['health'] ?
But that's not quite right..
If you want to access 2nd item from the array protossUnitsArray, shouldn't it be:
protossUnitsArray[1].health
I don't know about Windows 8 and the UI formerly known as Metro, but in JavaScript shouldn't it just be:
ProtossUnitsData.itemList[1].health
Edit
Looking at the documentation, it seems that WinJS.Binding.List exposes methods for working with the data in the List. See here and here for more info, but it looks like you want to use:
var item = ProtossUnitsData.getItem(1),
data = item.health;
That might work...
Figured it out! It's actually:
ProtossUnitsData.itemList.getAt(1).health;