Challenge 19 Megathread

I don’t think it’s absolutely necessary to build the object dynamically to pass the challenge, but the notes I saved while I was figuring it out are presented below.

I think the bigger issues you’re running into are going to relate to the first and last points. (Wrong output type, and syntax that the JS engine is likely to misinterpret)

Wish I had saved the link to where I found this.
This was in my notes from when I was figuring out how to dynamically build an object, and dynamically add new keys to it.

Note the myObj={} is the prefered option to declare a blank object, but same effect as the first line of the example code.

Every one of the myObj lines after the initial const building results in a key being added and a value being defined with that key.

Play around with it a bit and you can get that running.

const myObj = new Object(),
      str = 'myString',
      rand = Math.random(),
      obj = new Object();

myObj.type              = 'Dot syntax';
myObj['date created']   = 'String with space';
myObj[str]              = 'String value';
myObj[rand]             = 'Random Number';
myObj[obj]              = 'Object';
myObj['']               = 'Even an empty string';

// Data tests
    myObj[(receivedData[0].type)] = [receivedData[0].data]
    myObj[(receivedData[0].type)].push(receivedData[5].data)

3 Likes

Nice! Didn’t think that a Null would return false. Means my hasOwnProperty doesn’t even need to exist.

3 Likes

To add to @brokensyntax’s response, since I found the same link - the notes s/he has are from Working with objects - JavaScript | MDN ; under the Objects and Properties heading.

One thing that was stumping me as well that you may be running into… when trying to create object properties within a loop, and you’re trying to use the loop iterator, you need to use the square brackets, instead of dot notation.

e.g. :

let y = {}
for (x of array) {
y.x = "abc"}

creates a property literally called x ; whereas:

let y = {}
for (x of array) {
y.[x] = "abc"}

will use whatever you’ve defined as x in the for statement to create the object property

2 Likes

My solution came as follows:

  • Instantiated a new object.
  • Followed by using the forEach() array method on the receivedData array.
  • Used the bracket notation property accessor to populate the new object using the .push() array method.
  • Once the new object was populated, I then returned the finalized object.
6 Likes

But how did you create you object keys?


I am hopelessly lost and the help everyone is giving me isn’t helping please help make sense of this?

You’re so close, but you’re still returning an array.

The expected output should be an Object.

Note your code declaration newList = [{},{},{}]
This creates an array with multiple objects.

You need an output however that is an object with multiple arrays.
{Key1:[],Key2:[],Key3:[]}

1 Like

you hardcoded this one, I’m still working on it and have no solution yet but take a look as a reference “al.rich” solution.

for example, the last test checks your function with 2 types while you pushing 3 :


{"astro":[],"bio":[],"physics":[]}

Running test: The organizeData function should return proper object keys

{"astro":[],"bio":[],"physics":[]}

Running test: The organizeData function should return proper object values

{"astro":[],"bio":[],"physics":[]}

Running test: The organizeData function should return proper object values

{"astro":[],"bio":[],"physics":[]}

Running test: The organizeData function should return proper object values

{"bio":[],"physics":[]}```

i dont understand your next steps after that line. everything i do only adds the first element to my newObj. and i cant understand how to set the type to the newObj.key

Hi, I am a little stuck here, only passing 3 of the 5 tests. Any thoughts?

So this relies on me having already initialized a blank object
var objParsedData={}

This line checks if there’s a key matching the name of the current array object’s type.

If there is no match (the ! operator), then I would create a new key and value pair into the object.
That value is actually just a blank array initiator. []
Finally, I have an unconditional push method operation that pushes the value into that array.

That’s the whole contents of the loop.
1.Check if the key does not exist.
2.If it doesn’t exist create the key with initial value of []
3.push a value to the array.

3 Likes

Can you paste your code into a pre-formatted code block? (three backticks around the code)

Saves me a lot of time verifying my suspicions :smiley:

I suspect, the failing tests are because you have hardcoded the keys in the object, and are likely presenting an object with unexpected keys, or lacking expected keys.

In your newList, you have {} around each entry. Try adding the key - value pairs in a different manner.

An example:

var obj = {key1: value1, key2: value2};

or

obj.key3 = value3;
1 Like

Sure, many thanks, I will try here :slight_smile:

1 Like

ok i got all of that, i guess im confused as to how you created a new key(receivedData[i].type) and empty value pair in the object. not sure how to write that in

ok nevermind i figured it out. thanks for the help!

1 Like

I was about to say, I have another post above with a demonstration of multiple ways to add a new key/value pair to an object.

Finding that snippet was a big help.

I am stuck… can’t get data to push or concat more than one data per key…
Is this a recursion question?
Coz if it is, I am hopeless…

You’re correct, this is a recursion question.
result[type]=data;

Will define the entire value of the field each time it is called, rather than adding to the data in the field.

So on the first pass through astro, it will give “Saturn Data”
On the second pass over astro 5 cycles later, it will change to “Mercury is not the hottest.” rather than adding a second string.

Make sure that your value is an array type data structure, and that you aren’t overwriting your data structure each time you add to it.