-3

I want to build an object from two arrays, and that object should have a unique key name for each distinct value of the first array, and a randomly chosen value from the second which is also unique.

var keyNames = ['a','b','a','c','a']
var valuesToPickFrom = ['foo', 'bar', 'baz', 'qux', 'quux', 'waldo', 'fred']

Ie, the desired end result is:

{ 'a':'foo', 'b':bar', 'c':'baz' } // each value is unique

But not:

{ 'a':'foo', 'b':bar', 'c':'bar' } // repeated value, no good

How can this be done?

drenl
  • 1,321
  • 4
  • 18
  • 32
  • 3
    `So I'd like a function` Have you made any attempt at all yourself yet? Post the code you're trying if you've hit a wall – CertainPerformance Jun 26 '18 at 04:17
  • 1
    StackOverflow is not a site for "solutions" (especially not if by solutions you mean "code ready for copy/paste"). It's not for "thoughts" either. SO is for [specific questions about programming issues](https://stackoverflow.com/help/on-topic). – melpomene Jun 26 '18 at 04:39
  • I am sure he has tried it himself, that's why he is asking here. I find it "interesting" how the question always get so popular with down votes when it is a bad question. – leogoesger Jun 26 '18 at 05:40
  • @melpomene From the page you linked: "if your question generally covers… a specific programming problem, or a software algorithm, or software tools commonly used by programmers; and is a practical, answerable problem that is unique to software development … then you’re in the right place to ask your question!" My question meets all these criteria. I've removed the coloquial language. – drenl Jun 26 '18 at 11:48
  • "*I am looking for a function*" is not a specific question, it's just "write my code for me" in disguise. Your question shows no attempt to come up with a solution yourself. – melpomene Jun 26 '18 at 11:53
  • @melpomene I am asking this question in the general case because it is a worthwhile question, and clarifying procedures for this supports the general knowledge base. If you are convinced this question should not be asked here, please link to official SO documentation explaining why. Again, I have edited out the more colloquial language. – drenl Jun 26 '18 at 12:01
  • 1
    I ... That's what I just did. I'm confused.... You're not asking about a "general case", you're asking about a fairly specific problem. But that's besides the point because the subject of your question is not the problem. The problem is that your question ("how do I do this?") is effectively asking people to write code for you. It is the *question* that is unspecific, not the subject. – melpomene Jun 26 '18 at 12:09
  • @melpomene Consider the post below, which also asks a "how do I do this" question... 61 Upvotes, protected status: https://stackoverflow.com/questions/2380019/generate-unique-random-numbers-between-1-and-100 – drenl Jun 26 '18 at 20:53

1 Answers1

2

Question is too vague. So I will only describe what your function should do:

  1. Eliminate duplicates from both arrays. Eliminate array duplicates
  2. Randomize both arrays.
  3. Initiate a result object. var result={}
  4. A while loop until there is no more keys or values.
  5. array.pop a key and value from arrays. Add it to the object. result[key]=value;
  6. Return result object.
CarlosH.
  • 711
  • 7
  • 12
  • Don't use `sort` to shuffle an array. It doesn't work that way. – melpomene Jun 26 '18 at 05:15
  • I know, this is a naive approach, I will not code the full solution. I will remove that sample from the answer. There are tons of ways to do it. The old method should look like this: `function shuffle(o){for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);return o;};`. If you want you to be fancy, you could even apply cryptographic randomization. – CarlosH. Jun 26 '18 at 05:37
  • `parseInt` on a number? That's still crazy-talk. – melpomene Jun 26 '18 at 05:38
  • Yeah, somebody need to do the full homework. That is a old code. It needs to get an integer. It could use `Math.floor()` instead. Personally I will not use it on production, good enough for proof of concept. I will use something that uses `crypto.getRandomValues()`. – CarlosH. Jun 26 '18 at 05:51