Remove object names from array of objects in js - javascript

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.

Related

Copy key:value to object elements of an array Javascript

This is a simple test of javascript knowledge that I seem to be lacking. I'm basically writing a query front end for monogdb, it's almost done apart from one little piece. Queries are built by clicking elements and most of the logic works. I'm stuck on building an $and query.
I have a flat object like this:
obj1: {
prop1: 'val1'
}
These key values are added dynamically when the user clicks on a list-item and enters a custom value. When the user clicks 2 or more items from the list-items I would like for my object to end up like this:
obj1: {
$and: [
{prop1: 'val1'},
{prop2: 'val2'},
{prop3: 'val3'}
]
}
Where all the original key value strings are wrapped in {} and become elements to an $and array.
I can get the first part working i.e. turning the key values into objects. And I can get the second part, injecting an $and array and feeding it some kind of object but I can't get the two to work together consistently.
It requires taking the value already in obj1, making an object out of it and pushing it into an array. Injecting that array into obj1, and then removing the old key value from obj1.
This is as far as I've got:
var add = []
var newElementObject= {}
for (var prop in obj1) {
newElementObject = {};
newElementObject[prop] = "custom value entered by user";
delete obj1[prop]
add.push(newElementObject);
}
obj1.$and = add;
It works, once. I get the structure I'm looking for but when I try to add more items to the $and array something craps out. The second obj updates and the first just sits there saying:
{$and : "custom value entered by user"}
I've been staring at this for too long.
Try
var newElementObject = {};
obj1.$add = obj1.$add || []; //keep the old array or make a new one
for (var prop in obj1) {
if (!obj1.hasOwnProperty(prop) || prop == '$add') { continue; }
newElementObject = {};
newElementObject[prop] = "custom value entered by user";
delete obj1[prop]
obj1.$add.push(newElementObject);
}
That way you're not going to overwrite the old array reference if you run this code twice on the same object, which is what it sounds like your problem is.

How can google DataLayer be converted into javascript object with key values for variables?

When you use Google Analytics DataLayer on your website you have it as a global variable dataLayer.
You can add your own events variables into it.
How can you convert all variables inside it to an object like
{var:value}
You access it like any other object:
var dataLayer = [{
myVar1: 'val1',
myVar2: 'val2
}]
var myVar1 = dataLayer[0].myVar1;
var myVar2 = dataLayer[0].myVar2;
I do not quite understand your heated response to nyuens answer. Firstly, since this is javascript so an array is also an object, secondly the dataLayer is an array of objects or, as we call it in JavaScript, an object (it's called JavaScript Object Notation after all). So you have an array with numeric indexes where every element is an object (which might in turn contain other arrays and objects).
I strongly disagress with nyuen's suggestion to adress elements in the dataLayer with the numeric index, though. If you push data to your dataLayer before the tag manager code dataLayer[0] will contain your custom data, if you don't it will contain the gtm.load event and related data. So this is not reliable.
However it seems want you really want is to flatten the object, i.e. remove the nested structure with numerical keys and have a one-dimensional structure. Luckily somebody has written a function that does this:
var flattenObject = function(ob) {
var toReturn = {};
for (var i in ob) {
if (!ob.hasOwnProperty(i)) continue;
if ((typeof ob[i]) == 'object') {
var flatObject = flattenObject(ob[i]);
for (var x in flatObject) {
if (!flatObject.hasOwnProperty(x)) continue;
toReturn[i + '.' + x] = flatObject[x];
}
} else {
toReturn[i] = ob[i];
}
}
return toReturn;
};
This makes a "one-dimensional" object from a nested object. Usually this would mean that keys with the same name will be overwritten by the respective last element. I.e.
dataLayer = [
{'key':value1},
{'key':value2},
]
would result in a simple
{'key':value2}
since the second "key" overwrites the first. However the function above mitigates this by storing the index as part of the key name, so you would get
{'0.key':value1, '1.key':value2}
which somewhat unfortunately means you cannot use dot syntax and have to adress the elements with angled brackets:
yourvariablename['0.key']

How to create complex javascript objects

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.

JSON object: Get all objects to capitalize their first letters

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", ...) */
}
}

How to merge these arrays/json objects?

I am a bit confused at this point on what is an object, what is an array, and what is a JSON. Can someone explain the differences in syntax between the two? and how to add items to each, how to merge each type, and such? I am trying to get this function to take the new information from a JSON object (I think) and merge it with some new information. This information will then be passed to a PHP script to be processed.
Here is the console output:
{"public":{"0":["el29t7","3bmGDy"]}}
{"public":"[object Object][object Object]"}
Here is the JS I am using:
/* Helper function to clean up any current data we have stored */
function insertSerializedData(ids, type) {
// Get anything in the current field
current_data = $('#changes').val();
if (!current_data) {
var data = {};
data[index++] = ids;
var final_data = {};
final_data[type] = data;
$('#changes').val(JSON.stringify(final_data));
} else {
current_data = JSON.parse(current_data);
var data = {};
data[index++] = ids;
// Does the index exist?
if (type in current_data) {
var temp_data = current_data[type];
current_data[type] = temp_data + data;
} else {
current_data[type] = data;
}
//var extra_data = {};
//extra_data[type] = data;
//$.merge(current_data, extra_data);
$('#changes').val(JSON.stringify(current_data));
}
console.log($('#changes').val());
}
The idea is if the key (public, or whatever other ones) doesn't exist yet, then to make it point to an array of arrays. If it does exist though, then that of array of arrays need to be merged with a new array. For instance:
If I have
{"public":{"0":["el29t7","3bmGDy"]}}
and I want to merge it with
["aj19vA", "jO71Ba"]
then final result would be:
{"public":{"0":["el29t7","3bmGDy"], "1":["aj19vA", "jO71Ba"]}}
How can i go about doing this? Thanks
Excellent two-part question. Overall, the second question is non-trivial because of the complexity of the first.
Question 1:
what is an object, what is an array, and what is a JSON. Can someone
explain the differences in syntax between the two?
Question 2:
and how to add items to each,
Question 3:
how to merge each type, and such?
Answer 1:
This is a common stumbling point because, JavaScript is more flexible than one might initially expect. Here is the curve.
In JavaScript everything is an object.
So here is the code for each:
//What is an object?
var obj = { };
var obj2 = { member:"value", myFunction:function(){} }
Above is an empty object. Then another object with a variable and a function.
They are called object-literals.
//What is an array
var array1 = [ ] ;
var array2 = [0,1,2,3,4];
Above is an empty array. Then another array with five Integers.
Here is the curve that causes confusion.
//Get elements from each of the prior examples.
var x = obj2["member"];
var y = array2[1];
What??? Both Object and Array are accessing values with a bracket?
This is because both are objects. This turns out to be a nice flexibility for writing advanced code. Arrays are objects.
//What is JSON?
JSON stands for JavaScript Object Notiation. As you might have guessed. Everything is an object... It is also an { }; But it is different because - it is used to transfer data to - and - from JavaScript, not actually used (commonly) in JavaScript. It is a file transfer format.
var JSONObject = {"member":"value"};
The only difference to the prior example is quotes. Essentially we are wrapping the object literal as a string so that it can be transferred to a server, or back, and it can be reinterpreted, very easily. Better than XML - because it does not have to be custom-parsed. Just call, stringify() or ParseJSON(). Google it. The point is... JSON can be converted into an object-literal JS object, and JS object-literals can be converted into JSON, for transfer to a server or a CouchDB database, for example.
Sorry for the tangent.
Answer 2:
How to add an item to each? Here is where the curve stops being a nuisance, and starts being awesome! Because everything is an object, it is all just about the same.
//Add to an object
var obj {member1:"stringvalue"}
obj.member2 = "addme"; //That is it!
//Add to an array
var array1 [1,2,3,4,5];
array1[0] = "addme";
array[6] = null;
//We shouldn't mix strings, integers, and nulls in arrays, but this isn't a best-practice tutorial.
Remember the JS object syntax and you may start to see a whole new flexible world of objects open up. But it may take a bit.
Answer 3: Ah, yeah... how to merge.
There are seriously (very many) ways to merge two arrays. It depends on exactly what you need. Sorted, Duplicated, Concatenated... there are a few.
Here is the answer!
UPDATE: How to make a beautiful multiple dimensional array.
//Multiple Dimension Array
var array1 = [1,2,3];
var array2 = [3,4];
var arraysinArray = [array1,array2]; //That is it!
Here is the curve again, this could be in an object:
var obj{
array1:[1,2,3],
array2:[3,4]
}
JavaScript is powerful stuff, stick with it; it gets good. : )
Hope that helps,
All the best!
Nash
In this case, think of a JavaScript's object literal {} as being like PHP's associative array.
Given that, an "array of arrays" actually looks like this (using your above desired output):
{public: [["el29t7","3bmGDy"], ["aj19vA", "jO71Ba"]]}
So here we have an object literal with a single property named "public" whose value is a 2-dimensional array.
If we assign the above to a variable we can then push another array onto "public" like this:
var current_data = {public: [["el29t7","3bmGDy"], ["aj19vA", "jO71Ba"]]};
// Using direct property access
current_data.public.push(["t9t9t9", "r4r4r4"]);
// Or using bracket notation
current_data["public"].push(["w2w2w2", "e0e0e0"]);
current_data's value is now:
{public: [
["el29t7","3bmGDy"],
["aj19vA", "jO71Ba"],
["t9t9t9", "r4r4r4"],
["w2w2w2", "e0e0e0"]
]}
So now "public" is an array whose length is 4.
current_data.public[0]; // ["el29t7","3bmGDy"]
current_data.public[1]; // ["aj19vA", "jO71Ba"]
current_data.public[2]; // ["t9t9t9", "r4r4r4"]
current_data.public[3]; // ["w2w2w2", "e0e0e0"]
MDN has very good documentation on Array for insight on other functions you might need.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array
First is an object, that contains array, second is an array.
DEMO showing display output http://jsfiddle.net/GjQCV/
var object={"public":{"0":["el29t7","3bmGDy"]}};
var arr=["aj19vA", "jO71Ba"] ;
/* use object notation to add new property and value which is the array*/
object.public[1]=arr;
It'd be much more natural if {"0": ...} were a true array rather than an object, but anyway:
function maxKey(b) {
var max;
for( var key in b )
var max = key;
return max;
}
function merge(a,b) {
for( var key in a ) {
b[key] = b[key] ? (b[key][maxKey(b)+1]=a[key], b[key]) : a[key];
}
return b;
}
Note that this assumes you would insert at the next integer index
Arrays are a particular kind of Javascript object
JSON is a way of representing Javascript objects (and as such can represent arrays and more)
Objects are much more general, and can be simple objects that can be represented as JSON, or can contain functions and prototypes.
So, this is not an array of arrays (you would access items using JSON notation like myobj["0"]):
{"0":["el29t7","3bmGDy"], "1":["aj19vA", "jO71Ba"]}
This is an array of arrays, which means you can use the push method to add an item, and access items using array notation like myobj[0]:
[ ["el29t7","3bmGDy"], ["aj19vA", "jO71Ba"] ]
It seems like the structure you want is something like this:
var myobj = { "public": [ ["key", "value"], ["key", "value"] ] }
Then if you want to add/merge new items, you'd write this:
if (myobj["public"] != null) {
myobj["public"].push(["newkey", "newval"]);
} else {
myobj["public"] = ["newkey", "newval"];
}

Categories