I need to create an array in Javascript with only one (or more) given element at a given position.
See the following snippet:
params = []; params[5] = "my value" params[14] = "my other value"; console.log(params);
All the elements that I don't initialize are (as expected) undefined. Can I delete these undefined elements, before or after the array is created?
Also, what happens if I set the array as POST data for a JQuery $.ajax() call?
Why don't you just use an object then?
params = {};
params[5] = "my value"
params[14] = "my other value";
console.log(params);
You can use Associative Arrays as also
var person = [];
person[0] = "John";
person[10] = "Doe";
person[20] = 46;
console.log(person);
array=(
'key1'=>''value1,
'key2'=>''value2
);
if you want to use in loop
result=array()
for(i=10;i<5;i++){
array1=(
'key1'=>''value1,
'key2'=>''value2
);
result=array1;
}
console.log(result)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.