This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to empty an array in JavaScript
How to remove all items from jQuery array?
I have array var myArray = [];, I want to clear all items in this array on every post back.
Simplest thing to do is just
myArray = [];
again.
edit — as pointed out in the comments, and in answers to other questions, another "simplest thing" is
myArray.length = 0;
and that has the advantage of retaining the same array object.
you can remove all item in myArray using array length, it's common pattern.
try this
var myArray = [1, 2, 3];
myArray.length = 0; // remove all item
To clear the array values you can do a simple:
myarray = [];
P.s.
jQuery != javascript
There is no such thing as a jQuery array, that's just a javascript array. When a page posts back, it re-renders and all of the javascript is re-run, you don't need to clear the contents of the array.
if, during execution of the page, you wanted to clear a javascript array, just re-initialize it as a new, blank array:
myArray = []; // no var, we are just initializing not declaring
Related
This question already has answers here:
How to get next N element from array?
(6 answers)
Closed last year.
I am trying to find a way to get a range of 5 sequential indexes from an array of indeterminate length.
My array might look like this: [0,1,2,3,4,5,6,7]
I know I can use array.slice() if I need to return something like [2,3,4,5,6], but sometimes I might need to return something like [6,7,0,1,2]
Is there a clean or built-in way to do this?
If I understood right, you need something like this:
function array_indexes(arr, keys){
let res = [];
for(let k of keys){
res.push(arr[k]);
}
return res;
}
This question already has answers here:
How to get subarray from array?
(5 answers)
Closed 2 years ago.
I've searched up this question, and everywhere people seem to recommend to use array.splice(). However, splice is inplace, and, for example, in my javascript console editor.
Everywhere I seem to search, people say that splice does NOT mutate the original array, but that is clearly not the case. Now, I'm sure I will find another way to do what I want, but what is the proper way to make a copy of a piece of an array without affecting the original array?
You can use slice(), see below:
let x = [1, 2, 3, 4, 5]
console.log(x);
let sliced = x.slice(0, 2);
console.log(x);
console.log(sliced);
The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included) where begin and end represent the index of items in that array. The original array will not be modified.
Make a copy of the array using the spread operator and then you can use splice or whatever.
let arr = [1, 2, 3, 4, 5];
let newArr = [...arr];
console.log(newArr);
// newArr.splice(......)
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 3 years ago.
I have a JSON object in a JavaScript function:
[{"PMID":31206477,"MemberID":1287,"recID":6352},
{"PMID":31202264,"MemberID":1245,"recID":5974},
{"PMID":31201299,"MemberID":1184,"recID":3012},
{"PMID":31196160,"MemberID":1241,"recID":3833}]
That is saved to an Multi-dimensional Array
Is there a better way that I can make a make a new Array with only the PMID element without looping and building it. I know that this question is close to a duplicate but I'm not interested in a merge or a concat. Currently I'm doing
var newArray = [];
for (var i = 0; i < this.pmidList.length; i++) {
newArray.push(this.pmidList[i]["PMID"]);
}
This question maybe a duplicate but if the average person can't find it based upon the title then it should not be considered a duplicate. The solution is the same but the question titles are much different.
You can use the Array.map function.
const input = [{"PMID":31206477,"MemberID":1287,"recID":6352},
{"PMID":31202264,"MemberID":1245,"recID":5974},
{"PMID":31201299,"MemberID":1184,"recID":3012},
{"PMID":31196160,"MemberID":1241,"recID":3833}];
const output = input.map(item => item.PMID);
Documentation
This question already has answers here:
How can I only keep items of an array that match a certain condition?
(3 answers)
Closed 8 years ago.
I have a JS object/ associative array with some values:
var array =[
{val:1,ref:10,rule:100},
{val:1,ref:12,rule:120},
{val:2,ref:13,rule:165},
];
And I want to perform a .length, but want to be able to slice based on one of the keys (for instance val == 1). I want the length of values with val 1 rather than the length of the entire object. I have looked through the references material and could not find a satisfactory answer and I am unsure if this is feasible.
array.val==1.length = 2
Something like that...
You want to .filter the array for elements that match some predicate:
var filteredArray = array.filter(function(element) { return element.val == 1 })
filteredArray.length // = 2
filter applies the callback function to each element in the array, and the new filtered array contains all elements from original array for which the filter callback function returned true. Here, the function returns true for all elements with a val property of 1.
You need to use a .filter():
array.filter(function(v){return v.val==1}).length
This question already has answers here:
Move an array element from one array position to another
(44 answers)
Closed 8 years ago.
I'm trying to find a way to move an object to the end of the array
I have this array of objects:
[{"id":"4","name":"Boaz"},{"id":"2","name":"Shareen"},{"id":"3","name":"Simon"},{"id":"1","name":"Miriam"}]
Let's say I have an id: 3, or a position: 2.
With that I want to move the whole set {"id":"3","name":"Simon"} to the end of it all
I have tried so many things, and searched and searched but I can't make it work
You can splice and then concat the object you want to remove:
var array = [{"id":"4","name":"Boaz"},{"id":"2","name":"Shareen"},{"id":"3","name":"Simon"},{"id":"1","name":"Miriam"}];
var itemToReplace = array.splice(0, 1); // 0 is the item index, 1 is the count of items you want to remove.
// => [{"id":"4","name":"Boaz"}]
array = array.concat(itemToReplace);
or even simpler:
array = array.concat(array.splice(0, 1));
BTW: it's an array of objects, not an object of arrays.
You can use splice and concat array methods like
var arr = [{"id":"4","name":"Boaz"},{"id":"2","name":"Shareen"},{"id":"3","name":"Simon"},{"id":"1","name":"Miriam"}];
// Consider need move arr[2] to the end
var removed = arr.splice(2,1);
var new_arr = arr.concat(removed);