I'm a new programmer , I know how to use dictionary/JSON to get the value of whatever you want like for example var x = {"name":jack,"age":20};
then x.name = jack
but what if I've a table that has been imported through an unknown EXCEL file and I just want to know the title of each column , how can i do that ?
for example
Var table = [{"id":0 , "name":jack,"age":25,"profession":student},{"id":1 , "name":nora,"age":22,"profession":student}]
i want to make a javascript function that can inform me with titles of each columns , the number of the columns
how can i do that ?
First of all, an object in JavaScript doesn't care if it was constructed out of JSON or not. So your object in JavaScript syntax will look like this:
const x = { name: 'jack', age: 20 };
If you now do Object.keys(x), you will get this:
[ 'name', 'age' ]
And Object.keys(table[0]) should be exactly what you want: the column names of your 'table'
[ 'name', 'age' ]
You can get a list of the names of the properties of an object in javascript with this :
Object.keys();
Documentation here :
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
Response of a similar question here :
Getting the object's property name
Object.keys(table[0])
will give you the keys of the first row; presumably the other rows will behave and follow the pattern.
Just iterate over object keys:
for (var key in table[0]) {console.log(key);}
or (will work in most modern browsers):
Object.keys(table[0]).forEach(key => console.log(key));
Don't forget to check if "table" has elements
Also see other possible options
var test = Object.keys(objectname)
console.log(test)
Related
I have a confusion of what this array can hold. Also, I want to know how it assigns the values to the variable set.
Can someone give me an example of data['sax'] please and explain me the loop below?
for(var x = 0; x < data['sax'].length; x++){
var set = data['sax'][x];
Then what does this mean ?
id : set.saxID,
name : set.symbol
What you have here is an array that is being looped through. data['sax'] will be something along the lines of the following:
var data = {
sax: [
{
saxID: 1,
symbol: 1
},
{
saxID: 2,
symbol: 2
}
]
}
As you can see in the example above, sax is an array which contains multiple objects. What happens when you loop over it, is that it accesses one of the objects inside the array. So data['sax'][0] will give you the object with saxID: 1.
Using the variable set to temporarily store the data in; you can access the data of data['sax'][0] as set. So data['sax'][0].saxID becomes set.saxID. It is somewhat of a shorthand version of accessing the data.
What happens with id: set.saxID, is that the values get assigned to a new object. Which will be something like the following.
var newSax = {
id: set.saxID
}
You are basically transferring data from one object to another.
Explaining the code
var set = data['sax'][x];
Here you are creating a variable called set and assigning it a value from data['sax'][x].
data['sax'] this might look like a array But Its Not, to access value of a array we use index but here its a string. This is another way of accessig a property value of a object. So data is a object with one of its property being sax. That means
data = {sax: somevalue};
Now we have,
data['sax'][x] So as you know data['sax'] is a object and then this [x] , here x is not a string its a variable and it holds the Number value (0,1,2,3...) , it means your data['sax'] value is a array since we are accessing from index.
So from the above analysis your data['sax'] will be in this format.
Array
data = { sax : ["someValue","someValue"]}
So variable set will be
var set = "someValue"; // if x = 0, as in the first loop
Now coming to the code
id : set.saxID,
name : set.symbol
set.saxID this is used if the set is an object. In Jquery to retrieve the value of a property in the object you use the . operator with property name (or by the property name as seen above). So the code means set is a object with two properties saxID and symbol. So your set object will be like
set = { saxID: 123, symbol = "TEST"}
That also means that your data value be
data = { sax : [{saxID: 123, symbol = "TEST"},{saxID: 123, symbol = "TEST"}]}
Let me know if I was clear
Update note: I believe this is different from the linked "duplicate" answer because I'm not asking how to use a variable name as the "name" (since I've shown I know how to do that), but rather, how to achieve the intended result below.
I currently have the custom data layer variable as an array that is populated in the following notation:
myArray.push({"name":"value"});
That said, the expected output should be like so:
[{"name1":"value1", "name2":"value2", "name3":"value3"}]
Now my current dilemma is that I need to add another item in the array, however, my "name" is stored in a variable.
Using the variable name leads to the variable name being used as the "name", so that won't work.
myArray.push({varName:"value"});
Results in:
[{"varName":"value"}]
I've also tried creating a new object, and inserting that in, but that just adds the object into the array without the correct "name".
var myObject = {};
myObject[varName] = "value";
myArray.push(myObject);
Results in:
[{"Message": {"varName":"value"}}]
Now, I'm out of ideas on how to go about with this, so any help is much appreciated!
TIA
If you can use ECMAScript 2015 you can do with computed property names:
myArray.push({[varName]:"value"});
I couldn't understand your question if you wanted to delete the variable "name" inside myArray or update it?
For adding new variables inside myArray just do another .push for name
myArray.push = [{
'name': 'value',
'name1': 'value',
'name2': 'value',
'name3': 'value',
}];
For removing the variable name this code would do it for more information about flushing variable read this Simo Ahava's blog http://www.simoahava.com/gtm-tips/remember-to-flush-unused-data-layer-variables/
myArray.push = [{
'name': undefined,
'name1': 'value',
'name2': 'value',
'name3': 'value',
}];
Yes I already know about
json.newvalue = 'newvalue'
But I'm not looking for that...
I have an empty JSON
plugins = {};
Now I need to add 5 items to it for example:
{"name":"test"}
{"name":"test2"}
I am looking for something like this: plugins.add({"name":"test"})
First, json is a string representation of an object, what you have in your code is an object. However, it seems that you don't want a JavaScript object, but an array.
var plugins = [];
plugins.push({"name":"test"});
plugins.push({"name":"test2"});
plugins will now be
[
{"name", "test"},
{"name", "test2"}
]
I try to read my data on a Hash Table but I search in the internet but i don't find a solution.
KPIs.push( {name: [data[0][j]], unite :[data[1][j]], order: [data[2][j]], column:[j] , area:[getArea(data[0][j])] } ) ;
I try :
KPIs.value["name"] // doesn't work
KPIs.length // work
How can I read this HashTable ?
Thanks for your help.
Based on your code it appears you are pushing an Object onto an Array, but you attempt to access the object properties directly on the Array, rather than on the element in the Array.
You'll first need to access the correct Array element, before attempting to access your object properties:
KPIs[0].name
or, to loop over them:
for(var i in KPIs){
var name = KPIs[i].name;
Logger.log(name);
}
See details on Arrays here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
My question is the following, in an array:
var array = new Array();
array['abc'] = 'value1';
array['def'] = 'value2';
How do I get the associative key of an array if I have its index number? Let's say I want associative key of arr[0]'s associative key ( => 'abc'), or associative key of arr[1] '=> 'def'). How is this possible in jQuery?
Let's be clear, I am not looking for the value and I do not need to use $.each(). I just need to link 0 to 'abc' and 1 => 'def' etc... Unfortunately something like arr[0].assoc_key() doesn't seem to exist T_T
Thanks a bunch.
All right so the solution is pretty simple, you need to create an object which associates indeces with keys as well as keys with values. Here is a JSBin that works. Please note that to add an element, you need a custom function (addElement in this case) to be able to have both indeces and keys associated at the right places. This is a rough draft to give you an idea of how it can be done!
JSBin
If you have any question or if that wasn't exactly what you expected, simply edit your question and I'll have another glance at it. It HAS to be a custom made object if you want the behavior you asked for.
Javascript doesn't have a native Dictionary type, you would have to write it. – T McKeown
It isn't possible and jQuery doesn't come into the picture at all. If you use an array as a dictionary like that, you are doing something wrong. – Jon
rethink the way you are doing it. Maybe try array[0] = {key: 'abc', value: 'value1'} – Geezer68
#Geezer68, objects do not support multidimentional data, the array I'm working on is 3 levels deep (I know I didn't says so in my original post, but I didn't think it was relevant).
Anyway, thank you guys, it answers the question! I will rethink it then ;-)
EDIT: I guess I'll just add a level:
var array = new Array();
array[] = 'abc';
array[0] = 'value1'
I don't know an other than using a for ... in. So here how i do it and hope you get a better answer (because i want to know aswell!).
var array = new Array();
array['abc'] = 'value1';
array['def'] = 'value2';
var listKeys = [];
for(x in array) listKeys.push(x);
console.log(listKeys); // ['abc', 'def']
but using [string] on an array object is adding property to the object, not the array. So it may be better to initialise it like that :
var array = {};
You might learn more information on this technique in this question and some restriction on why you should not rely on that.