Function index?

Function index?
0

#1

I can’t understand this one for the life of me, please help me out.

Write a function named index that takes an array of objects, and a property name, and returns an object where the keys are the specified property:

var array = [{ id: 1, name: 'Joe' }, { id: 2, name: 'Sue' }]

index(array, 'id') // { 1: { id: 1, name: 'Joe' }, 2: { id: 2, name: 'Sue' } }
index(array, 'name') // { Joe: { id: 1, name: 'Joe' }, Sue: { id: 2, name: 'Sue' } }
function index(arr, str){
    let obj = {};
    
    for(let i = 0; i < arr.length; i++){
        
    }
    
    
    return obj;
};

Have I got the skeleton right? Trying to work outside in.


#2

Hi, can you post the link to the challenge?
Thanks!


#3

No, it’s not part of FCC.


#4

I think your skeleton is fine, but you need to right more so we can check if there is error in the core code.


#5

Yeah, trying to figure out exactly what I have to do. Terrible instructions, but I know I need dot notation and a couple other things.


#6

try this:

function index(arr, str){
    let obj = {};
    
    for(let i = 0; i < arr.length; i++){
        obj[arr[i][str]] = arr[i];
    }
    
    
    return obj;
};

not sure if this will work


#7

Well, unfortunately it did, I just wish you didn’t give me the answer. Can you explain obj[arr[i][str]] = arr[i]; in English?


#8

So you’re not looking for explicit code? This isn’t as horrible as it sounds, I think.

You are being given an array of objects, and a property name on each of the objects in the array. Given that property name, return a NEW array consisting of each of the original array’s object’s property value for that key.
So, for example, index(array, 'id') should return [1, 2], while index(array, ‘name’)should return[“Joe”, “Sue”]`.

An easy way to do this would be to create that new array by mapping the OLD array and simply returning the member’s object property (using bracket notation, not dot notation). Then simply return the new array.


#9

Yeah, I was hoping to avoid just GIVING it to you. obj[arr[i][str]] is just UGLY, you could do the same with obj[i]arr[i][str] becomes the key for that array. Probably not exactly what you want. But meh.


#10

I just hate objects. Damnit is this shit frustrating.

EDIT:
Why arr[i]? How does this pass when I’m returning an object and need to push the array to the object? Why am I four months in and feel dumber than when I started?

obj[arr[i][str]] = arr[i]

#11

arr[i][str] is to find the specific key given in the parameter in the current array element,
obj[] makes that value into the new key in the obj object
then assign it the whole array element as the value by using =arr[i]


#12

then assign it the whole array element as the value by using =arr[i]

Why? I need back an object, what the hell is putting it back into the array gonna do?


#13

You know, you’re right. There’s a number of things wrong with that one – obj is an object, not an array.

What its doing is returning an object, and each key in the object is the property value for the nested object. Wow. that was WAY more confusing than it should have been.

You really just want to iterate over the array, copy out arr[i][str] which is the field in the current array member, and Bob’s your uncle.

Honestly, I have NO CLUE why that one passed, it truly shouldn’t have. Console log it, and you’ll see its a potentially massive object being returned, rather than an array of strings, or numbers or something.


#14

I gotta mess with it but I’m genuinely stumped.

✓ returns an empty object when passed an empty array
✓ returns an object indexed by the given property

#15

look at using a map here. You can return an array from that, the same size as the original, but only the data you want from the member objects.


#16

I don’t think I’m advanced yet. I don’t know map or any other methods like that.


#17

I thought that challenge want me to return a object instead of an array. I’m not sure and I am a noob. I learned .map() method before but I’m not very used to it yet. So I sometimes write really messy code…:frowning:


#18

Well, so leave map and other methods like that out of it for a moment. Take a look at what you know: You have an array, and you need to make an array of the same size. The information IN the array will change, but the size will remain consistent. For the function call index( [{id: 1, name: 'Joe'},{id:2, name: 'Sue'}], 'name'); you have the following:

  • Given: an array, arr = [{ id: 1, name: 'Joe' }, { id: 2, name: 'Sue' }]
  • Given: a string, str = 'name'

And from this, you will return an array [‘Joe’, ‘Sue’] - the same length array, and the filtered information from the first array, filtered by str.

So, purely as a conversation and not actually in code, what is the process of making that happen?

  1. Create a blank array. This will be returned at the end of the function.
  2. Do the for loop, much as you did. You want to loop over all the elements of the arr array, and do something with each one, so you will start at an index (we’ll call it i, for ease of reference) of zero, and go to arr.length-1 (because the index on an array starts with zero, not one, but you knew that.
  3. Within the loop, we can reference the unique member of the array by its index: arr[i]. But we have been passed an object, rather than a primitive value (number, string, boolean), so what do we do with that? We know the property name we want to use, it’s been passed to us via the str parameter of the function - so we can access that array member’s unique property by arr[i][str] (or 'in the array arr, at the index i, there is a member with a property str). We can simply push that value onto the new array created in step one, and loop onto the next.
  4. Once all the array members have been iterated through (and we know they have if we reach arr.length-1 in our index), we can simply return that new array we just populated.

One of the great sources of confusion in javascript is the nature of Objects. Actually, it’s confusing because of a couple of factors: First, they’re everywhere. Second, they change, depending on the situation. Third, they’re simple in theory, but in practice they become extremely flexible and dynamic, and that makes them intimidating.

Here’s the thing: think of an array as an ordered list. It’s a list of items, and each member is defined by its position in the list. arr[1] is defined, and remains arr[1] unless something pushes or pops or shifts or something.

An object, on the other hand, is more like an unordered list. You have no control over what property follows another, and you have no guarantee where in the object’s order a property resides. But just as with arrays, you need a mechanism to access those values, so they are given names, or keys, to access them. Just as an array member is accessible via arr[4], an object member (or property) is accessible by its name: obj.name or obj["name"] both access the same thing.

Sorry, I slip into professor mode. :slight_smile:


#19

That is a very clear explanation, thank you sir! But I wonder if they want us to return an object or an array? I might have some misunderstanding there.


#20

According to the OP’s original post (see below) an object should be returned.