javascript re-populate an array - javascript

I am using:
var myArry = [];
as a global variable. I then populate the array with some data from the database.
If I then want to replace the array data with new data do I have to empty / reset the original array.
Or what is the correct method?

Here are some ways to empty an array:
myArray = [];
myArray = new Array();
myArray.length = 0;
myArray.splice(0, myArray.length);
These will all work.

myArray.length = 0; will empty the array. You can then repopulate it as needed.

If it comes from the database, it would be better to clear your array since some data may be deleted.
To do so, you can write myArray = [];

Related

how to associate key to each individual array and make them key value pair

i have array like this
[["apple","banana"],["monkey"]];
how can i associate key to them like,
[{"fruit":["apple","banana"],"wild":["monkey"]}]
is this possible?
i'm trying something like this
var arr = [["apple","banana"],["monkey"]];
var newArray = [];
for(var i=0;i<arr.length;i++){
newArray["fruit"] = arr[i] //further code i don't know
}
help me th
While you can do that, like this:
var array = [["apple","banana"],["monkey"]];
var update = [
{fruit: array[0], wild: array[1]}
];
console.log(update);
...frankly it seems unlikely that's really want you want.
Very simply you can assign a new name simply
var array = [["apple","banana"],["monkey"]];
var names = ["fruits", "wild"]
var modified = [{names[0]: array[0], names[1]: array[1]}];
If you have a lot of values just use a for loop to iterate and assign value to it.

How to get total sum of array

i'm trying to add together the contents of an array (integers).
for example:
var myArray;
var answer;
myArray[0]=2;
myArray[1]=5;
answer=myArray[0]+myArray[1];
answer should equal 7.Could you help me, please? Thank you so much.
You need to initialize your array
var myArray = [];
As you get more values into your array you might consider a loop, example:
var myArray = [];
var answer = 0;
myArray[0]=2;
myArray[1]=5;
for (var i=0;i<myArray.length;i++)
{
answer += myArray[i];
}
console.log(answer);
Take a look at this: http://www.w3schools.com/js/js_loop_for.asp
When I run your code, I get an error.
you need to intialize your variable myArray to be an array.
var myArray = [];
After I do that, answer has the expected value.
You need to declare myArray as an array, otherwise myArray[0] means a property named 0 on undefined (which will probably blow up) rather than an index access.
var myArray = [];
...

jQuery .data() remove item from stored array

I am using the .data() function in jQuery to store an array as per below:
var myArray = {};
myArray[0] = {};
myArray[0][0] = "test00";
myArray[0][1] = "test01";
myArray[1] = {};
myArray[1][0] = "test10";
myArray[1][1] = "test11";
$('#datastorage').data("testname". myArray);
I want to remove only one item (myArray[0]) from the "testname" and keep the rest.
The below does not work:
$('#datastorage').removeData("testname").removeData(0);
I believe jQuery stored the array in a form of a plain object (the test $.isPlainObject() comes back true)
I am now trying to use the function .not() to remove the element...
Since the original object is an array, what's actually stored is just a reference to the original data, so any modification you make is reflected in every reference to that array, including the one stored in .data().
So you can just remove the element from the array:
$('#datastorage').data("testname").shift();
or if you want more flexibility on which elements are removed, use .splice().
$('#datastorage').data("testname").splice(0, 1);
or if you've still got access to myArray:
myArray.shift();
There's no need to put the array back into .data() - any of the above will modify both myArray and whatever's already in .data() - they're the same array!.
The same would apply if the data was an object, but not if it's a primitive type.
You'll have to get the array out, remove from it, and then put it back.
var a = $('#datastorage').data('testname');
a.splice(0,1); // remove 1 item from position 0
$('#datastorage').data('testname', a);
try this code
var myArray = []; // myArray is an Array, not an object
myArray[0] = {};
myArray[0][0] = "test00";
myArray[0][1] = "test01";
myArray[1] = {};
myArray[1][0] = "test10";
myArray[1][1] = "test11";
$('#datastorage').data("testname", myArray);
console.log($('#datastorage').data("testname"));
$('#datastorage').data("testname", myArray.slice(1));
console.log($('#datastorage').data("testname"));
example fiddle: http://jsfiddle.net/nNg68/

Javascript multidimentional array undefined object error

I am trying to make a two dimensional array out of two one dimentional arrays with this code:
var PassAssoArr = new Array();
for(k in PassPourcentNames) {
PassAssoArr[k][0] = PassPourcentNames[k]
PassAssoArr[k][1] = PassPourcentValue[k]
}
However, I get the error message: " 'undefined' is null or not an object " and it points to the first line after the for statement.
PassPourcentNames and PassPourcentValue have the same number of elements and none of the values are null. The first one contain strings and the second one integers.
Any help is greatly apreciated.
var PassAssoArr = new Array();
for(k in PassPourcentNames) {
PassAssoArr[k] = new Array();
PassAssoArr[k][0] = PassPourcentNames[k]
PassAssoArr[k][1] = PassPourcentValue[k]
}
Also instead of new Array() you can use []
var PassAssoArr = [];
for(k in PassPourcentNames) {
PassAssoArr[k] = [];
PassAssoArr[k][0] = PassPourcentNames[k]
PassAssoArr[k][1] = PassPourcentValue[k]
}
I believe this is actually faster in most JS engines.
First define PassAssoArr[k] = []; before assigning to [0] and [1].
Javascript does not support true multi-dimensional arrays.
You're trying to use nested arrays without creating the inner arrays.
You need to put an array into each element of the outer PassAssoArr:
PassAssoArr[index] = []; //Empty array literal
You're only defining one dimension of PassAssoArr - you need to set PassAssoArr[k] = new Array();
Try just doing:
PassAssoArr[k] = new Array(PassPourcentNames[k], PassPourcentValue[k]);

Javascript unset array

How do i unset an array in javascript?
i just want to empty it - so it has nothing in it keys or anything
you can assign a new array to it:
var array = ["element1","element2","element3"];
...
array = new Array();
OR
array = [];
Assign an empty array to it
x = []
array.length = 0
should work.
What about delete?
delete array
Or to delete a single index:
delete array[x]
var array = ["elem", "item"];
...
array = []; // Empty!
FOR REFERENCE (I know its old and answered)
If you want to empty the array, that has been answered already.
If you want to unset the array, you could assign an unset variable...
var undef;
var myArr = [];
...
myArr = undef;
// typeof myArr == undefined

Categories