2

I'm trying to create a javascript object to send as a parameter.

I need to send it in this format:

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

How can I construct this object and fill it out?

I'm doing this:

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

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

And I get an error message from the library saying that my object is missing the key Objects inside of Delete

When I console.log params I get

{ Bucket: 'ct.presentations',
Delete: [ { Objects: [Object] } ] }

What's the best way for creating objects like this?


As Alex pointed out below, map in Javascript, isn't a type of array like I thought it was, so instead of

Delete:[{ I should have done Delete:{

3
  • try params.Delete[0].push({Key: data.keys[i]} Commented Jan 2, 2014 at 18:05
  • @Dan that gave me Object #<Object> has no method 'push' Commented Jan 2, 2014 at 18:07
  • Wow is that indentation hard to read. :-) Commented Jan 2, 2014 at 18:09

1 Answer 1

3

You've made Delete an array containing an object that has Objects as a key. Sounds like it shouldn't be an array:

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

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

Also note that I removed the {Key: ""} from inside the [] in Objects (on the fourth line). If you really meant to have an object with an empty key as the first entry in the array, add that back, but I suspect you didn't.

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

4 Comments

map in other languages typically translates to a plain object {} in JavaScript. So this sounds right to me.
@AlexWayne Oh. That explains it. I thought map was a type of list.
That worked, except the Delete[0] should be changed to Delete seeing as it is no longer an array. It gave me errors the other way around
@Houseman It's called a map because it "maps" keys to values. Other names are "hash" or "dictionary". Most languages have a construct for this, and in JavaScript that construct is "object".