1

To construct an object like this:

params (Object) (defaults to: {}) —
    Delete — required — (map)
        Objects — required — (Array<map>)
            Key — required — (String) Key name of the object to delete.

I'm doing this:

var params = {
    Bucket : data.bucket,
    Delete: [{
    Objects:[{ Key:""}] }]  
};

for(i=0;i<data.keys.length;i++){
    params.Delete[0].Objects[i].Key = data.keys[i];
}

It breaks on the Key with

params.Delete[0].Objects[i].Key = data.keys[i];
                                ^

TypeError: Cannot set property 'Key' of undefined

What am I doing wrong?


It turns out there is only one Key inside of Objects. I'd like to put as many keys as I want inside of Objects so as to properly construct the object. How can I do this?

2
  • 1
    because Objects[1] is undefined... Commented Jan 2, 2014 at 17:40
  • Even if the rightside value of assignment is wrong, you would get the same error. Javascript fails the complete line and complains about leftside value. I thought the statement must be params.Delete[0].Objects[i].Key = data[i];. Because you are looping through data.keys.length which was not known to us, We can not judge the number of looping iterations. Commented Jan 2, 2014 at 18:10

2 Answers 2

1

How many object literals are in the Objects array? From your initialization it only looks like 1. But you are using a for loop, implying you expect more than one.

That error means Objects[i] is undefined for whatever value of i you reach when it occurs.

It seems like you need to do something like

 for(i=0;i<data.keys.length;i++){
    params.Delete[0].Objects.push({Key: data.keys[i]})
 }

to get new object literals into your Objects array as you process your data (change your definition of params to just have an empty array for Objects initially).

Sign up to request clarification or add additional context in comments.

3 Comments

I want there to be as many Keys as there are in data.keys. I thought that the array of Objects would be dynamic
"How many object literals with a Key property are in the Objects array?" Or indeed, without a Key property...
@Houseman: It is, but when i is 1, Objects[i] is undefined. You're trying to treat it like an object. You probably meant .Objects[i] = { Key: data.keys[i] };
0

Your params.Delete[0].Objects here has only one object in it and it happens when the i is 1. you are counting a for loop based on the other loop with 2 different length.

data.keys

is different from:

params.Delete[0].Objects

this one has only one object, whereas the first one has more than one object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.