How do we find the length of an object in Javascript? [duplicate] - javascript

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 6 years ago.
I dont see how we can find the length of an object. For arrays i can your array.length but it doesnt work for objects, any suggestions?
Thanks!

Just like that:
Object.keys(objectName).length;

Related

cannot print component in array in javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 2 months ago.
i'm just a newbie on javascript , please help me this
var arr = [{"student-name":"Harry Potter","discipline":"Magic"}] ;
how can i get "Harry Potter" in this array , thanks for helping

Change values in array to a certain pattern [duplicate]

This question already has answers here:
jQuery sort array value in sequence
(1 answer)
Sorting javascript array using function 'sort"
(3 answers)
Closed 5 years ago.
Just wondering, what is the best way to go about "sorting" an array to the following:
I have the array as [1,1,1,1,2,2,2,2], how would I sort it so it goes:
[1,2,1,2,1,2,1,2]
I cannot think of the phrase or a method to do this.
This is done in angularjs/can use jquery.

Javascript, convert a list of objects into an array? [duplicate]

This question already has answers here:
Converting JavaScript object with numeric keys into array
(17 answers)
Closed 6 years ago.
I have this:
How do I turn this into an array?
Object.keys(mounted).map((key) => mounted[key])

Object length in typescript? [duplicate]

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 6 years ago.
is there any way in typescript by which I can get length of an object:
Something like this:
say I have an object:
public customer:any={
"name":"Bhushan",
"eid":"879546",
"dept":"IT"
}
Now I am trying to get its length in typescript.
ie. when I am doing customer.length(), I should be able to get value 3 as it has 3 elements.
I tried Object.getOwnPropertyNames(customer.value) but its returning 2 whereas I have 3 elements in my object.
any inputs?
You could try the following:
Object.keys(customer).length
Object.keys(this.customer).length

How to combine 2 array to an Object? [duplicate]

This question already has answers here:
Merge keys array and values array into an object in JavaScript
(14 answers)
Closed 8 years ago.
I have 2 arrays which are the same length like this:
$scope.access_name = ['group1','group2','group3','group4']
access_value=[1,2,1,3]
How can I combine to have result like this:
object_access = {'group1:1','group2:2','group3:1','group4:3'}
thankyou very much
var object_access={};
for(int i=0;i<access_value.length;i++)
{
object_access[$scope.access_name[i]]=access_value[i];
}

Categories