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:{
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.
Related
We're building a dynamic JSON payload according to a simple template format that instructs how many levels our payload should produce, e.g. "level1.level2" > will produce:
{
"level1": [
{
"level2": [
{
"someData": "willGoHere",
},
{
"someOtherData": "willAlsoGoHere"
}
]
}
]
}
Obviously we're working with a different naming structure, e.g: "client.ipcEvent.level3" and in some cases we're testing 4 levels. We're doing something wrong ~ we're able to build our javascript object but unable to use stringify() to create a full result because for some strange reason stringify() against our object only returns the 1st level's data:
{ "level1": [] }
.
I've tried changing the way in which our object is loaded with values but it all comes back to stringify() 'dropping' off array values that don't have accompanying attribute/property values at the same level.
The issue is that your client is an array, but you've put properties on it that aren't array entries (which you can do because arrays are objects):
JSON.stringify only includes the array entries of an array, not its other properties:
const a = [];
a.nonEntryProperty = "foo";
a[0] = "bar";
console.log(JSON.stringify(a)); // ["bar"]
To ensure data is serialized to JSON correctly, include object-like properties in objects, and array entries in arrays:
const structure = {
info: {
nonEntryProperty: "foo"
},
array: ["bar"]
};
console.log(JSON.stringify(structure, null, 4));
OK, solved; we were creating arrays when it should have been objects, and vice-versa; sorry for the vague question but thank you TJ and Sebastian. Based on the specification of "level1.level2" the solution was to create object {} for "level1" and array [] for the last item "level2"...
I am trying to understand jquery extend method , As per the API documentation
The merge performed by $.extend() is not recursive by default; if a
property of the first object is itself an object or array, it will be
completely overwritten by a property with the same key in the second
or subsequent object. The values are not merged.
It says array or object property will not merged and will be replaced . In below example parameter is JSON array and it is getting merged instead of getting replaced .
var a = {
external : true,
parameter : [{name:'ip1'},{name:'ip2'},{name:'ip3'}]
};
var b = {
data:'Sumeet',
parameter : [{name:'ip1'},{name:'ip2'}]
};
$.extend(true,a,b);
console.log(a);
Output :
{
data: "Sumeet",
external: true,
parameter: [{
name: "ip1"
}, {
name: "ip2"
}, {
name: "ip3"
}]
}
The parameter should have only ip1 and ip2
Note that your citation says "by default". The documentation later also says:
However, by passing true for the first function argument, objects will be recursively merged.
Full paragraph:
The merge performed by $.extend() is not recursive by default; if a property of the first object is itself an object or array, it will be completely overwritten by a property with the same key in the second or subsequent object. The values are not merged. This can be seen in the example below by examining the value of banana. However, by passing true for the first function argument, objects will be recursively merged.
I have data in a .json file, in the form:
{
"ObjectA": {},
"ObjectB": {},
"ObjectC": {}
}
I want to remove the names (ObjectA, B etc.) so I end up with 'anonymous' objects:
{
{},
{},
{}
}
Delete only deletes properties of an object, so that won't work. What will do the trick?
Might not be the cleanest method, but gets the job done. (I added values so your console log is easier to see that the data is coming across.
var obj = {
ObjectA: {fu:'bar'},
ObjectB: {iheart:'pizza'},
ObjectC: {something:'value'}
};
var obj2 = {};
var i = 0;
for (var key in obj) {
obj2[i] = obj[key];
i++;
}
obj = obj2; // don't really need this - could just use obj2
console.log(obj);
I'm sure this could be cleaned up and written in less code, but you get the idea of at least one way you can do that.
As far as I know, data can only be represent in one form i.e: Key->value. In every case, whether you talk about JSON data, MYSQL data or Object alloc in heap.
For making this easy we have generalised data storing in 2 methods.
Data in Dictionary
Data in Array
Dictionary holds each data with a unique "key". Like you already have in json "ObjectA" is a key. And Array also holds data with a unique key user don't have to specify that key while pushing data into array. we call that key "index".
So technically array is also a dictionary with auto-incremented "key" for each data.
Now, lets get into the point. You want to save your data in json with out any key. It means you have to save data in an ARRAY.
like:
{
objects : { [{}, {}, {} ]}
}
but remember you need to save that array adjacent to any "key". You can't get rid from key->value rule.
I want to be able to save an array (if possible), so that I can get to an entry within it quickly and easily with a unique identifier, something like:
array structure:
[
1001:{loads of info},
1002:{loads more info}
]
and to get values like:
var info_i_want = array.1001;
I have the 'loads of info' part already in a json object, just need to built this new array?
I ask because at the moment I have to loop through each object in the array to check if its the one I want before I can do anything
If you want you can use numeric keys with an object literal (not the array notation you used above):
var obj = {
1001: { /* data */},
1002: { /* data */}
};
With numeric keys, you must use bracket notation to dereference; i.e:
obj[1001]; // *not* dot reference `obj.1001` which will not work
Hope this helps :)
EDIT
For reference read the Object section of Javascript Garden, specifically Accessing Properties
you can assume like this.
var arr = [
{1001: "loads of info"},
{1002: "loads more info"}
]
var info_i_want = arr[0].1001 // returns loads of info
I have a JSON object that looks something like this (result from an AJAX call):
json{
code: 0,
resultVal: Object {
data:
[
Object{
generatedName: name1,
generatedValue: value1
},
Object{
generatedName1: name2,
generatedValue1: value2
}....
],
anotherItem: true,
...
}
}
To clarify resultVal is an object and data is an array of objects, and each object in that array will have two values who's names I will not know in advance.
I am having a problem because I need generatedName and generatedValue to be GenerateName and GeneratedValue. These names and values are usually not the as each other. I know can access each Object through json.resultVal.data[#], but that's as far as I have gotten. json.resultVal.data[0].name returns undefined.
Once I can get those values isolated I can make the fixes I need.
NOTE I am running these calls through Chrome's debugger. The thinking is once I am able to isolate the value I can write the code to fix it using that call. It takes some time to get to this point in the application.
Any suggestions?
If I understood it right, you need to iterate over all keys for all objects in "json.resultVal.data". Try using a for/in loop to iterate over the "data" object, as in:
for( var i in json.resultVal.data ) {
for( var k in json.resultVal.data[i] ) {
/* here "k" will be key string ("generatedName", "generatedValue", ...) */
}
}