This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
var oldMatches = Meteor.users.find(userId, {
fields: {
_id: false,
matches: true
}
}).fetch()
This returns an object with an empty matches array, as it should. How would I access this array to find the length of it?
Thanks.
You would find the length of the array with
oldMatches.length
So:
if(oldMatches.length) {
// is not empty
} else {
// is empty
}
N.B. length only works on a returned array from fetch, not a Meteor cursor
Related
This question already has answers here:
Does JavaScript guarantee object property order?
(13 answers)
Does ES6 introduce a well-defined order of enumeration for object properties?
(3 answers)
Closed 1 year ago.
I'm trying to sort an object from db
MyOjbect : {
key: {
val1:"value",
val2:"",
val3:time,
val4:...
...}
}
and I sorted the object by val3
const sorted = Object.entries(MyObject)
.sort((x,y) => {x[1].val3 - y[1].val3} );
and got an array type.
0:(2) ["key" , {val1:...val2:...}]
1:(2) ["key" , {val1:...val2:...}]
...
I tried reduce() to make it back to the object type, but it changes to be in unsorted order
How can I keep the order sorted and change the type to the object?
You can't. An object doesn't keep the sort order as an Array does it. If you want to have the keys sorted you need to use an Array.
This question already has answers here:
How to efficiently count the number of keys/properties of an object in JavaScript
(19 answers)
Closed 3 years ago.
I have an data object in a state, like so:
this.state = {
data: {
newsData: [],
eventData: [],
},
}
How can I get the number of properties defined in the state data? data.length doesn't work since it's not an array.
You can use Object.values(data).length
This question already has answers here:
Iterate through object properties
(31 answers)
How to parse JSON data when the property name is not known in advance?
(2 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
How can I get the value "success" if the value that stands in place "Everlasting hair" is changing?
{
'Everlasting hair' :
{ succes : true,
volume:'12'
}
}
You can discover the property names in the object via for-in or Object.keys. Object.keys, for instance, will give you an array of the object's own, enumerable properties. If you know for sure there will only be one, then:
var success = yourObject[Object.keys(yourObject)[0]].succes;
// Another s here? It's missing in the question -----------^
On cutting-edge JavaScript engines with Object.values (new in ES2017, but polyfillable), as Keith points out if you don't need the name you can use Object.values instead:
var success = Object.values(yourObject)[0].succes;
// Another s here? It's missing in the question -^
This question already has answers here:
How to check if a string array contains one string in JavaScript? [duplicate]
(5 answers)
Closed 7 years ago.
I want to be able to see if "megan" is a part of the array people. Though, i can only do this by using people[2].
Here is my code:
var people = new Array("jack","ian");
document.write(people[0]);
people.push("megan");
document.write("<br />");
Use the indexOf method of the array:
if(people.indexOf("megan") > -1) {
//do stuff
} else {
//not in array
}
If the string is in the array, 0 is returned. if not, -1 is returned.
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
i have to get the value of wave1 and wave2 from array by using underscore.js
array =[{"id":1,"name":"Monoprix", "pdv":16,"graph":[{"wave1":22,"wave2":11}]} ;
i try
$scope.wave1 =array.graph.wave1 ;
console.log($scope.wave1) ;
console log = Unidentified
can you help me !
your code is wrong, you miss ] after element list.
your array :
var array =[{"id":1,"name":"Monoprix", "pdv":16,"graph":[{"wave1":22,"wave2":11}]}];
and to get some data you must first select array index, like this:
array[0].graph