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',
}];
Related
I recognize that I can access the value of an object within an array by passing in the key name, like so:
const batchNumValue = batchNumber[0]['MAX(batch_number) + 1'];
In my case I know the array will always contain exactly one object, with a single key/value pair, like so:
[{ 'MAX(batch_number) + 1' : 234 }]
That being the case, my question is, is there a way I can pass in a variable representing whatever that key name happens to be? Or does one have to always explicitly pass the key name, even in a situation like this?
You could get the values from the object and take the first item.
const
data = [{ 'MAX(batch_number) + 1' : 234 }],
value = Object.values(data[0])[0];
console.log(value);
I'm trying to reference a value which stores a model's property from the session.
var value = '${foo.property}';
Now using chartjs I try to reference the value as one of the data for the graph
myChart = document.getElementById('myChart').getContext('2d');
massPopChart = new Chart(myChart, {
type: 'bar' ,
data: {
labels:['Sample Column' , '2nd Low Value Column'],
datasets:[{
label: 'Sample Value Column',
data:[
//------REFERENCE OVER HERE
value,
3000
],
backgroundColor:[
'rgba(255,99,132,0.6)',
'rgba(54,162,235,0.6)'
]
}]
},
options:{}
});
From inspecting elements the value I get is not the value of the model property but the word value itself
I've read up on javascript tutorials none of their lessons about variables help me with this issue
I learned that referencing variables in javascript is different so can someone enlighten me on this
EDIT: I've also tried directly referencing '${foo.property}' in the data segment and I get the value but it returns as a String, I have tried using parseInt and Number to try to convert but to no avail
Thanks to a comment from a certain user(who deleted it for some reason), I found that removing the quotes and directly referencing the model property in the data segment will do just what I need
data:[
${foo.property} , //NOT '${foo.property}'
3000
],
referencing a variable into the data segment still has that issue where the name of variable is what's used rather than the assigned value
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)
Before I was able to do req.user.property to get data, but all of a sudden that failed and I have to do req.user[0].property to get data.
Essentially req.user is a list. It looks like this...
user:
[ { _id: '934715373258035',
active: true,
date: 'July 1st 2015, 1:44:49 am',
email: 'username#emailprovider.com',
genre: '53d8fcd1ea70ad64d6655fa8',
location: '53d8ff38ea70ad64d6655fbb',
name: 'First Last',
pass: 'QAt9tSGDpft7iSxwoa5gsTO63ONXshREQmkE8F6MKqRA6IIn2Eo49Z5vZFqKushX' } ],
Why is this and what is causing it? Is this normal? It didn't seem to be in my research.
As I can see, currently you have array of entities (users), so that's why you can't access your properties just be req.user.propertie.
In order to access some prop. of array, you need to select specific item for this array and then access needed property.
Possible the issue is related to the fact that previously you were receiving jason object and now it's array of json objects.
Example
Array: var objects = [obj1][obj2][obj3];
To select specific item you need to access it by index: objects[0] //obj1
And then you can access properties of 'obj1': objects[0].someProp
It sounds like req.user.property has changed from an object literal to an array. In an object literal, you'd be able to access the property by name.
var objectLiteral = {name: 'foo'};
objectLiteral.name === 'foo' // true
In an array, you have to access the members by index.
var arrayOfObjectLiterals = [{name: 'foo'}, {name: 'bar'}]
arrayOfObjectLiterals[0].name === 'foo' // true
In order to force req.user to be an object (instead of an object within a list) I changed my passport.deserializeUser function to run the command db.users.findOne() instead of db.users.find().
This returns only the first result and allows req.user to be equal to an object instead of a list that contained the object.
Still not sure what originally caused it to turn in to a list since it should still only return one result...
Given a data structure that contains an array of JavaScript objects, how can I bind a certain entry from that array to an input field using Angular?
The data structure looks like this:
$scope.data = {
name: 'Foo Bar',
fields: [
{field: "F1", value: "1F"},
{field: "F2", value: "2F"},
{field: "F3", value: "3F"}
]
};
The fields array contains several instances of the given structure, with each entry having both a field attribute and a value attribute.
How can I bind an input control to the value field attribute of the array entry with the field F1?
<input ng-model="???"/>
I know that I could bind all fields using an ng-repeat, but that's not what I want. The above data is just an example from a much larger list of fields, where I only want to bind a pre-defined subset of fields to controls on the screen. The subset is not based on the attributes in the array entries, but is known at design time of the page.
So for the above example, I would try to bind F1 to one input on the page, and F2 to another one. F3 would not be bound to a control.
I've seen examples where a function was used in the ng-model, but it doesn't seem to work with Angular 1.1.0.
Is there another clever way to bind the input field to a specific array entry?
Here's a fiddle that has an example, but does not work since it's trying to use function in the ng-model attribute: http://jsfiddle.net/nwinkler/cbnAU/4/
Update
Based on the recommendation below, this is what it should look like: http://jsfiddle.net/nwinkler/cbnAU/7/
I personally would reorganize the array in a way that field property of an entry of the array become the identifier of the object. Mhhh that sentence may sound strange. What I mean is the following:
$scope.data = {
name: 'F1',
fields: {
F1: {
value: "1F"
},
F2: {
value: "2F"
}
}
};
If you want to bind a the value dynamically and it's an easy and quick way to achieve it.
Here is your fiddle modified so that it words. http://jsfiddle.net/RZFm6/
I hope that helps
You can use an array of objects, just not an array of strings.
HTML:
<div ng-repeat="field in data.fields">
<input ng-model="field.val"/>
</div>
JS:
$scope.data = {
name: 'F1',
fields: [
{ val: "v1" },
{ val: "v2" }
]
};
I've updated #Flek's fiddle here: http://jsfiddle.net/RZFm6/6/
Edit: Sorry just read your question properly, you can still use an array with:
<label>Bound to F1:</label>
<input ng-model="data.fields[0].value"/>
though maybe stop and think. Is there going to be variable number of fields ? or are you making a predetermined number of fields ? Use an array in the former and an object for the latter.
One way to do it is to simply add the necessary references to the scope, like this:
$scope.fieldF1 = fieldValue('F1');
$scope.fieldF2 = fieldValue('F2');
And then use those references:
<input ng-model="fieldF1.value"/>
<input ng-model="fieldF2.value"/>
Fiddle: http://jsfiddle.net/cbnAU/5/
Note: I'm assuming that $scope.data is static, but if it happens to be dynamic you can always watch for changes on it and recalculate the references...