issues accessing an array in an object jquery - javascript

I am trying to access some data from an object as follows:
var summaryChanges = {
dataToAdd:[
{name:[]},
{events:[]},
{emails:[]}
],
dataToRemove:[
{name:[]},
{events:[]},
{emails:[]}
]
}
i am trying to log the contents of the name property of data to add as follows:
console.log($(summaryChanges.dataToAdd.name)[0]);
however the console only logs undefined.

dataToAdd is an arrary not an object , so access it like
console.log(summaryChanges.dataToAdd[0].name[0])

You need to realize some things
$(summaryChanges.dataToAdd.name) you are creating a jQuery Object.
summaryChanges it's an object so you can do sumaryChanges.dataToAdd
dataToAdd it's an array, so for get a value you access it like this dataToAdd[index]
At the end you access it like this
console.log(summaryChanges.dataToAdd[index].name[index])

Related

Add object to variable defined for form

I have a form that uses $scope.booking variable composed of several fields and array, all loaded from HTML.
I need to add an array of object from javascript, adding one object per time.
I tryed
$scope.booking.newExternalUsers[$scope.count]= $scope.user.newExternalUser;
and
$scope.booking.newExternalUsers.push=$scope.user.newExternalUser;
but I receive Cannot set property '0' of undefined and Cannot set property of undefined, it is correct because I have instantiated only $scope.booking={}
Maybe is a stupid question, but I am almost new in angularjs, how can I add the $scope.user.newExternalUser one pertime (for each button event)?.
Thanks
You should define the array first before set values to it.
like this:
$scope.booking = {};
$scope.booking.newExternalUsers = [];
or
$scope.booking = {
newExternalUsers: []
};
Then
You can add items to it as you want, like this:
$scope.booking.newExternalUsers[$scope.count]= $scope.user.newExternalUser;
or using Array.prototype.push()
$scope.booking.newExternalUsers.push($scope.user.newExternalUser);
First define the property newExternalUsers in booking array first
$scope.booking={
'newExternalUsers' : []
}
or
$scope.booking.newExternalUsers=[]
Then push the item to an booking array
$scope.booking.newExternalUsers.push($scope.user.newExternalUser);
You have to instantiate $scope.booking.newExternalUsers=[]
I'll assume you have a controller. In that controller you can have this:
$onInit() {
this.booking = { newExternaUsers: [] };
}
Then it's initiated once when the controller starts.

How can I manually access a third-dimension javascript Array

I've been trying to access a third level node in an array using the indexes in it, but I can't access it, I tried a lot of ways that I found here on SO but I don't want to iterate through it, I want to get it manually.
var data = [
{code:1,
label:'John Doe',
tasks:[{
code:1,
label: 'AnyProject',
starts:'2016/1/25',
ends:'2016/2/25'}]
}];
What I want to do (theoretically):
data[0].tasks.code
data[0].tasks[0].code
tasks is an Array so you need to access it like an array.
data[0].tasks[0].code
Inside data array you have tasks and inside task array you have property code.
[] is an array you can use index to look inside.
{} is an object you can access using .

Read a HashMap in App Script

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

Get Value of Child Object with JavaScript

I have a JSON collection produced from an object graph. Shown below is an example value. I am having trouble accessing the nested 'Type' object to retrieve any of it's values.
[{"Id":1,"Name":"My Name","Type":{"Id":1,"Name":"my Value"}}]
I am using a JS component that has a property that can be assigned a value similar to below.
myProperty: Type.Name, //Not working
Can someone recommend how I set this value?
What you have is a JavaScript array, not an object, and certainly not JSON. So if you have
var arr = [{"Id":1,"Name":"My Name","Type":{"Id":1,"Name":"my Value"}}]
you'd need to index it, and grab the Type object off of that.
var typeName = arr[0].Type.Name;

Use value from array as property

So I have a dictionary-like object and an array:
var colors = {"b":color(0, 0, 0)};
var ar=[["b","0","0"],["b","b","0"],["b","b","b"]];
Now, I would like to get the value from the dictionary using the array like so:
colors.ar[0][0]
Which should give me the color black. However, this gives me an error:
Cannot read property '0' of undefined
I believe this is because it's trying to access colors."b" instead of colors.b .
So how can I get the property from the dictionary by using a value from the array?
Thanks!
I get it. What you want is this:
colors[ar[0][0]]
Since ar[0][0] resolves to "b", colors[ar[0][0]] resolves to colors["b"].
If you use dot notation it will try to access colors.ar which is undefined.

Categories

Resources