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

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)

Related

how to create an array of values from an existing array in Javascript [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 5 months ago.
I have an array which has many objects, I want to make an array of tag values so I can display the non repeated tags in the frontend.
This is the array, and from which I want to create the new array which will include all the tag values.
Can you please share with me a solution or maybe a way to solve this ? Thanks
You can use Array#map to get all the tag property values, and then create a Set from that to get the unique ones.
const tags = [...new Set(yourObj.nodes.map(x => x.tag))];

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

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

Underscore.js get value of key object in array of object [duplicate]

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

Append variable value to form Input [duplicate]

This question already has answers here:
How to use JavaScript variables in jQuery selectors?
(6 answers)
Closed 8 years ago.
I need to append numeric value stored in one variable to form input fields.
Let's Say
var b= 5;
var text=$("#dropdown_id").val();
I want to append value of variable b in dropdown_id .
My expected result is #dropdown_id5
Use string concatenation:
$("#dropdown_id" + b).val()

Categories