Object length in typescript? [duplicate] - javascript

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

Related

How to use filter to compare to arrays to find missing object? [duplicate]

This question already has answers here:
Array.includes() to find object in array [duplicate]
(8 answers)
Object comparison in JavaScript [duplicate]
(10 answers)
How to determine equality for two JavaScript objects?
(82 answers)
Closed 9 months ago.
I'm trying to use arr.filter() to find the missing element between two arrays, this has been answered plenty of times and i've seen threads like this one Finding missing element in two array for javascript that explain it actually very well. For some reason thought, i cant seem to get this to work.
This is my code
var x = [{Number:1},{Number:2},{Number:3}]
var y = [{Number:1},{Number:2}]
function getDifference(x,y){
x.filter(function(object,index,arr){
console.log(object,index,arr)
if(!y.includes(object)){
// print object
console.log(object) // <-- Prints all 3, should just print the missing one.
}
})
}
getDifference(x,y)
Basically, it just needs to print out the missing object. Which in this case, is {Number:3}
Instead, it prints every object.
I've tried with the code in the thread that i linked above, and still was having trouble. Not sure what i'm not understanding with it.

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.

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

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;

Get all data from JSON array [duplicate]

This question already has answers here:
Loop (for each) over an array in JavaScript
(40 answers)
Closed 7 years ago.
I've a little problem with a simple thing.I believe.
this is my code...
javascript code
I'm able to grab the first object element but I need all the data object, I guess I've to change something in this code line...
value[0]['firstName'];
You need to replace
value[0]['firstName']; // $.each()
With
value[index]['firstName']; // $.each()
And
$.each(oggprova.PIANIFI,function(){...});
with
$.each(oggprova.PIANIFI.gennio,function(){....});

How to get the value of a property in javascript which has a dot in it's name? [duplicate]

This question already has answers here:
How to get objects value if its name contains dots?
(4 answers)
How can I access object properties containing special characters?
(2 answers)
Closed 9 years ago.
I have a property with name 'pp.phaseName' in an object 'Config'
Whenever I try to access it like Config.pp.phaseName, it's throwing error.
I've tried using Config.(pp.phaseName), Config."pp.phaseName" etc. but none was working.
Please help me on how to do this.
You have to use the square bracket notation.
Config["pp.phaseName"]

Categories