Get all data from JSON array [duplicate] - javascript

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(){....});

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;

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

Set dynamic value to Array in JS like PHP [duplicate]

This question already has answers here:
How to insert an item into an array at a specific index (JavaScript)
(28 answers)
Closed 7 years ago.
My question is how I will set a value an array in JS like PHP?
for example:
in php I have an array named Arr, I set a new value in n position using
Arr[] = value
but I try to do same in JS but display an error
Use the push command
Arr.push(value)

Categories