This question already has answers here:
How to sort DOM elements while selecting in jQuery?
(4 answers)
Closed 8 years ago.
I'm trying to sort an array of DOM elements based on their ID. The array is populated by getting all elements with a given class:
var rowsList = document.getElementsByClassName("employee_grid_rows");
rowsList.sort(); //??
How do I get the sorting done by ID?
You have to sort the HTMLCollection
var rowsList = document.getElementsByClassName("employee_grid_rows");
console.log(rowsList);
var arr = Array.prototype.slice.call( rowsList );
rowsList = arr.sort(function(a, b) {
//Comparing for strings instead of numbers
return a.id.localeCompare(b.id);
});
console.log(rowsList);
rowList = rowList.sort(function(a, b) {
return a.id.localeCompare(b.id);
});
Related
This question already has answers here:
How to sum elements at the same index in array of arrays into a single array?
(7 answers)
Closed 3 years ago.
I am working with a multi-dimensional array.
the following is my array:
let arr = [[1,2,3],[1,2,3],[1,2,3]];
the length of the inner arrays will always be the same.
I want create a function to add all the arrays elements together with their respective elements and create a new array with the result.
so my desired output is
result =[3,6,9];
You can use nested for loop for that.
let arr = [[1,2,3],[1,2,3],[1,2,3]];
let res = Array(arr[0].length).fill(0);
for(let i = 0;i<arr[0].length;i++){
for(let j = 0;j<arr.length;j++){
res[i] += arr[j][i]
}
}
console.log(res)
This question already has answers here:
Filter strings in Array based on content (filter search value)
(6 answers)
Closed 4 years ago.
I have an array of strings (names) such as:
name =["John Doe","Lutfur Kabir", "Moshiur Imtiaz Rahman", "Clark Kent","Jenny Doe"]
I want to get the index/es of the name that has Doe in it. How do I go about doing it using JavaScript.
You can use Array.prototype.includes:
Find the previous answer here already
var categoriesPresent = ['word', 'word', 'specialword', 'word'];
var categoriesNotPresent = ['word', 'word', 'word'];
var foundPresent = categoriesPresent.includes('specialword');
var foundNotPresent = categoriesNotPresent.includes('specialword');
console.log(foundPresent, foundNotPresent); // true false
You can use .reduce() to create an array having indexes of strings containing the desired string:
let data =["John Doe","Lutfur Kabir", "Moshiur Imtiaz Rahman", "Clark Kent","Jenny Doe"];
let result = data.reduce((r, c, i) => {
if(c.includes('Doe')) { r.push(i); }
return r;
}, []);
console.log(result);
This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 6 years ago.
I have this array of objects:
var frequencies = [{id:124,name:'qqq'},
{id:589,name:'www'},
{id:45,name:'eee'},
{id:567,name:'rrr'}];
I need to get an object from the array above by the id value.
For example I need to get the object with id = 45.
I tried this:
var t = frequencies.map(function (obj) { return obj.id==45; });
But I didn't get the desired object.
How can I implement it using JavaScript prototype functions?
If your id's are unique you can use find()
var frequencies = [{"id":124,"name":"qqq"},{"id":589,"name":"www"},{"id":45,"name":"eee"},{"id":567,"name":"rrr"}];
var result = frequencies.find(function(e) {
return e.id == 45;
});
console.log(result)
You need filter() not map()
var t = frequencies.filter(function (obj) {
return obj.id==45;
})[0];
This question already has answers here:
Sort an array based on another array of integers
(7 answers)
Closed 7 years ago.
I have an array or numbers and objects (same length):
var a = [2,0,1], b = [obj1,obj2,obj3];
I would like to reposition items in array 'b' to the position of numbers in array 'a'.
Could be done with jquery as well.
How can I do that most easily?
Thanks
To do this there is no an auto function but you can use array map to get this result.
var orderByArray = function(order, data) {
return order.map(function(pos) {
return data[pos];
});
};
var a = [2,0,1];
var b = ['obj1', 'obj2', 'obj3'];
var result = orderByArray(a, b);
console.log('result', result);
This question already has answers here:
Getting a list of associative array keys
(6 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
I need to group the rows out of a table that has a matching order number, and then iterate over the groupings.
I have this code working which is creating the perfect array, data-wise:
var multItems = [];
// Combine items under orders,
$('tr.order').each(function(){
var orderNum = $(this).find('.ordernumber').val();
if ( ($('tr.order .ordernumber[value="' + orderNum + '"]').length > 1 ) && !(orderNum in multItems) ){
$('tr.order .ordernumber[value="' + orderNum + '"]').each(function(){
if (!(orderNum in multItems)){
multItems[orderNum] = [];
}
multItems[orderNum].push(this);
});
}
});
// Create new tr with order totals (of each item)
for (var i = multItems.length - 1; i >= 0; i--) {
// Code
};
But it creates an array with a length of 0, apparently, where multItems = [], but multItems[orderNumber] is defined... just with no way to access it if I don't know the order numbers.
I could make an array of the order numbers separately, but that feels like it must be the long way round. If I just create a numbered array, how do I know which number to pop the items from the orders into?
With your current code you have
var orderNum = $(this).find('.ordernumber').val();
where val() returns a string and not a number. So when you are doing multItems[orderNum] it is a string.
For the current code to work, you want to use a for in loop.
for (var prop in multItems) {
if( multItems.hasOwnProperty( prop ) ) {
console.log(multItems[prop]);
}
}
FYI: Order is not guaranteed. Also you should be using an object {} and not an array here.
Now the other thing you can do is to use parseInt to change the string into a number and than magically your for loop would start working. [This is assuming that ordernumber is a numeric value]
var orderNum = parseInt($(this).find('.ordernumber').val(), 10);