My html content is as follows:
<span>{{queries['q1_name'].parameters.length}}</span> <!--Not working: Doesn't show length of parameters-->
My javascript object is as follows:
$scope.queries = {"q1_name":
{"parameters":
{"P1":"abc" }
}
}
This could be done by creating a function, but I am trying to get the length directly in html.
your object should've been: I'm assuming you will be having more than one items in queries since you were using queries[0].parameters.length. if not then you can take out the outer most square brackets.
$scope.queries = [{"parameters":
[{"P1":"abc" },{"P2":"xyz" }]
},{"parameters":
[{"P1":"bcd" },{"P2":"jfk" }]
}];
if "q1_name" is a must have then you need to use queries[0].q1_name.parameters.length. but with this method all of your objects in the array needs to have "q1_name" as property for the code to work on all element in the array.
$scope.queries = [{"q1_name":
{"parameters":
[{"P1":"abc" },{"P2":"xyz" }]
}
}]
Hope that helps.
Related
I'm trying to render a table using data from an array of objects. These have been created with a constructor which has a number of prototype methods. All the methods work correctly outside of the specific problem I'm having.
The table needs to be rendered dynamically, eventually with user input selecting which columns they want rendered and in which order. As such I'm trying to use a separate array to house the selected fields (filled in manually for now).
Below I have an example of the object, the prototype, the array, and the array of objects forEach method containing a simple console.log.
const jsonResultTotal = {
new_stats: [
{
revenue: 'US$200.00',
cost: 'US$4.09',
}]
};
jsonResultTotal.prototype = {
...jsonResultTotal.prototype,
roas: function() {
return (Number(this.revenue.replace(/[^0-9.]/g, '')) / Number(this.cost.replace(/[^0-9.]/g,''))).toFixed(
2);
},
const rowCellContent = [
'revenue',
'cost',
roas(),
'roas()'
roas
'roas'
];
jsonResultTotal.new_stats.forEach(function(e) {
for (i=0; i<=5;i++){
console.log(e[rowCellContent[i]])
}
}
The result of the above code is
'US$200.00'
'US$4.09'
undefined
Well, actually that specific rowCellContent array will result in roas() is not defined, but the point is that none of the methods will work.
If I console.log(e.roas()) then it works, so accessing the method seems to be fine. I've tried arrow functions in the rowCellContent array, as well as full functions with a return of this.roas(), I've tried to used bind on the e[rowCellContent[i]]
There doesn't seem to be any issues calling the properties through the array, or calling the prototype methods directly, I just can't get the methods via the array to work.
Any help would be appreciated.
I have made a custom filter in controller but can't get index or size of iterated json. Calling program.length gets me undefined. Any ideas?
$scope.filterFn = function(program){
if(case){
return true;
}
console.log(program.length);//undefined
return false;
};
try to use this code to get object length.
Object.keys(program).length;
try : How to display length of filtered ng-repeat data, so you can access size by $scope.filtered.length like the example.
Are there any more filters before this? Coz previous filters decide input for next filter?
The length() method is only available on an array [].
In this case you are trying to get the size of a json object {}, please note this does not have a length. If you need to see how many items exist in this object, you can try to first convert all the keys into an array by using Object.keys(program) which will give you an array.
If program originally looked like this:
{
foo: 'some value',
bar: 'another value'
}
using Object.keys(program) will give you ['foo', 'bar'] On this array you can now call the length method, so that you have Object.keys(program).length, which in this case would give you 2.
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 .
I have an object array:
user :[
{
name: String,
username: String
}
]
I want to view every change either to name or username.
I found underscore _.pluck only does the trick for one property (_.pluck(user, 'name')
Is there another way to have the list of both values?
With pluck you can only use one property, it simply isn't made to retrieve multiple. The method you would want to use is map, as suggested in this relevant question + answer: How to pluck multiple attributes from a Backbone collection?
Assuming you want the following output [['nameA','usernameA'],['nameB','usernameB'],...]], you could use map in the following manner:
var myResult = users.map(function(user) {
return [user.name, user.username];
});
NOTE: I changed the variable user to users to make more sense with your data.
Say I have:
var Certificated = {}
Sub items are added dynamically and variate. Possible outcome:
var Certificated = {
Elementary: ["foo","bar", "ball"]
MiddleSchool: ["bar", "crampapydime"]
};
I want to do the following:
Certificated.Elementary = Certificated.Elementary.join("");
Except I need it to do that on all of the objects inside.
Keep in mind I can't know for sure the titles of nor how many objects will be inside Certificated.
My question is how can I use .join("") on all elements inside Certificated, without calling each one specifically?
EDIT: I am aware .join() is for arrays and the objects inside Certificated are going to be arrays. Therefore the join method.
Does this work?
for (var key in Certificated) {
if (Certificated.hasOwnProperty(key)) {
Certificated[key] = Certificated[key].join("");
}
}
It loops through all properties of Certificated, and makes a quick safe check for the key being a real property, then uses bracket notation - [""] - to do your join.
Quick question - are you sure you want to use join? I know you just provided an example, but you can't call join on a string...it's for arrays. Just wanted to make sure you knew.
Here's a jsFiddle of my code working with arrays being used for the properties:
http://jsfiddle.net/v48dL/
Notice in the browser console, the properties' values are strings because the join combined them with "".