[英]How to create complex javascript objects
I'm trying to create a javascript object to send as a parameter. 我正在尝试创建一个JavaScript对象作为参数发送。
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
而且我从库中收到一条错误消息,提示我的对象缺少Delete
内的关键Objects
When I console.log
params
I get 当我console.log
params
我得到
{ 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 正如Alex在下面指出的那样,JavaScript中的map
并不是我想的那样的数组类型,因此
Delete:[{
I should have done Delete:{
Delete:[{
我应该完成Delete:{
You've made Delete
an array containing an object that has Objects
as a key. 您已经完成了Delete
一个包含以Objects
为键的对象的数组的工作 。 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). 还要注意,我从Objects
的[]
内部(第四行)中删除了{Key: ""}
。 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. 如果您确实打算将带有空键的对象作为数组中的第一个条目,请将其添加回去,但我怀疑您没有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.