Programming

The mapping would be something like this:

const arr = [{ name: ‘a’, d: ‘a’}, { name: ‘b’, d: ‘b’ }];
const hash = arr.reduce((acc, i) => { acc[i.name] = i.d; return acc; }, {});

console.log(hash);
console.log(hash[‘a’]);

There’s probably a better way to write the reducer but hopefully it gets the idea across. You have to build an object where you assign properties while looping over the array of objects. Libraries like lodash probably have standard functions for doing this.

1 Like

Right, but whether or not I’d worry about that depends a lot on how long the list is. I’m assuming it’s usually not long enough to care too much unless you need to do a lot of finds, in which case yeah, either change the API or map it once and store the result so you can do the hash lookup.

For my board game site there was a ~75% drop in CPU usage when I switched from a list with finds to a hashmap FWIW. The lists weren’t all that long, maybe 50 tops, but there were hundreds of events per minutes.

1 Like

Hashmap can degrade to O(n)

Just nitting sorry :)

Cool! Don’t get me wrong, I’d almost always change the API here because I like the hash lookup more anyway. I think “hundreds of events per minute” satisfies the “unless you need to do a lot of finds” condition I mentioned though. I’ve been conditioned to always consider the possibility of premature optimization :stuck_out_tongue:

1 Like

Y’all probably will just lol or say “ok, boomer” or “wat?” or something, but anyone have any experience and/or thoughts on Drupal?

I’d rather work at Arby’s

2 Likes

Eh, this is probably worse, but it’s a new site and I think I’m going to do it in Wordpress. I already have two sites in Wordpress going and this will be a different type of site and lame as it is I think there’s enough demand for Wordpress stuff that it could be a valuable skill as an independent contractor.

Wordpress >>>> drupal for most things

That’s pretty nice. It took me a minute to realize what you were saying about the LookupName enum, but that’s handy.

You can use markdown for your code blocks to make sure it displays the way you want. It looks like you’re just relying on the software to interpret tabs, but it is annoyingly fiddly about white space.

1 Like

Right. We use typescript and RxJS, so I got the gist. I was about to suggest you needed to check that lookupName existed and then it clicked that it was an enum and that you had specified the type of your object such that it was assumed those properties always exist. Because I’m slow :slight_smile:

You know what, you’re right. I was just confusing myself. It wouldn’t throw a compiler error. I was just looking at the code and thinking “what happens if lookupName doesn’t exist?” But then realizing it was an enum made me understand that it’s just part of the API contract, rather than being something more run-time dependent (like the name of a person in a person lookup, or whatever).

We don’t use strict null checks at the moment.

Hi all,

Taking a big long break from politics for the sake of my health. But that leaves me with like 4+ hours a day where my first instinct is to open the guardian or post on here or what have you. I need displacement activity so I’ve decided to embrace the hellworld, swim or die, neo liberal, always be gaining skills to avoid becoming obsolete lifestyle and learn business programming.

I can write beginner/intermediate SQL code but that’s it. But my brain doesn’t work remembering weird details eg I have to look up the code for the first of last month every single time even though I used it basically once a day for the past 2 years. So I’m look for language recommendations and super beginner tutorials geared towards business use. I tried a first steps python thing online but that got me printing hello world, counting to 10 and then abruptly stopped. Free resources preferred but if you had an amazing time with something paid I’ll go for it. PLz halp.

1 Like

Python is the way to go imo although it’s easier to figure out something to do where you can see the results and show them off in JavaScript on the web. Do you have a project in mind?

Check out Humble Bundle and see if they have any Python bundles at the moment… oh, they don’t, but they do have this:

Which has some beginner material for Python for cheap. There are also 1001 YouTube videos on it as well to help.

If doing this in the abstract is too boring or doesn’t stick you may want to think of an application you can use to do something for you at work, or something you used to do, write up what you want it to do and then start finding out how to do the different bits of it. My last go round at playing with Python I started to design a file parser; something that could read a delimited file and give some statistical read outs on the various columns. Things like min/max or avg, or most frequency distributions. The idea being it would be for checking the quality of user data and addresses. A task I used to do by visual inspection a few jobs ago. Lots of breaking that down and writing little test programs to do the individual bits of it.

No unfortunately. Just need a time sink and figured it may as well be productive.

Find by value is pretty much back to using some method that will iterate over a collection (in JS plain objects can also be treated as a type of collection, e.g. an array of key=>value pairs) and checking each item to see if it matches. So if you want to find the entry where field1 === 'baz' then you have to do something like this (there are a few different ways to loop over the hash entries, or values, depending on what you are looking for):

const obj = {
  ABC: {
    field1: 'foo',
    field2: 'bar'
  },
  
  XYZ: {
    field1: 'baz',
    field2: 'blat'
  }
};

const key = Object.keys(obj).find((k) => {
  return obj[k].field1 === 'baz';
});

console.log(key);
console.log(obj[key]);

If you don’t care about the ‘ABC’ key, then Object.values(obj).find(...) may be more direct.

This is too general to make specific language recommendations. Also this forum is almost entirely Javascript programmers so take those recommendations with a grain of salt. Without more info python is almost definitely a better choice.

C/bash programmer here ayooo