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);
Related
This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
In Javascript, how do I check if an array has duplicate values?
(9 answers)
Checking for duplicate strings in JavaScript array
(13 answers)
Closed 5 months ago.
I am writing a javascript function that takes a nested array and returns the numbers that occurs more than once in that array.
I believe my function is accurate (meaning that it passes their "Correctness test" ) but i am after efficiency, how efficient is this code?
For example - Lets call the name of the function deepSort(nestedArray) where nestedArray is the nested array parameter
function deepSort(nestedArray) {
const flatArr = nestedArray.flat().sort();
let results = []
for (let i = 0; i < flatArr.length - 1; i++) {
if (flatArr[i + 1] == flatArr[i]) {
results.push(flatArr[i]);
}
}
return (results.filter((item, index) => results.indexOf(item) === index)).join()
}
const a = deepSort([[1,3,4,5], [4,7,9,1,3], [2,3,5], [1,2,3,4]]) // Returns 1,2,3,4,5
console.log(a);
const b = deepSort([[1,2,3], [4,5], [6,7,8], [2,9,0]]) // Returns 2
console.log(b);
const c = deepSort([[2,7,9], [4,3], [9,6,5], [1,4,3]]) // Returns 3,4,9
console.log(c);
Can this code be optimized any more for speed and efficiency when handling extremely large values of data?
This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 12 months ago.
How can i check if an array has element twice and log only the element that is not.
const duplicateElements = (array) => {
for(let numbers of array) {
// code here
}
}
const numbers = [1,3,2,4,1,3,2];
duplicateElements(numbers);
// Output 4
With the JS function filter you can archive this. First you have to iterate your array. then check with the filter function how many times the current value is inside your array. If equal 1 then push to an result array.
const d = [];
const arr = [1,3,2,4,1,3,2]
arr.forEach((e) => {
if (arr.filter(x => x == e).length === 1) {
d.push(e);
}
})
console.log(d);
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:
How to create an array containing 1...N
(77 answers)
Fastest way to fill an array with multiple value in JS. Can I pass a some pattern or function to method fill insted of a single value in JS? [duplicate]
(1 answer)
Closed 4 years ago.
I want to create a function that takes input from user and returns an array with all the numbers from 1 to the passed number as an argument. Example: createArray(10) should return [1,2,3,4,5,6,7,8,9,10]. I came up with this solution:
function createArray(input) {
var value = 0;
var array = [];
for (i=0;i<input;i++) {
value++;
array.push(value)
console.log(array)
}
}
createArray(12);
What is the correct and better way of doing it?
I would prefer to use Array.from:
const createArray = length => Array.from(
{ length },
// Mapper function: i is the current index in the length being iterated over:
(_, i) => i + 1
)
console.log(JSON.stringify(createArray(10)));
console.log(JSON.stringify(createArray(5)));
There is no need for the extra variable just do this:
function createArray(input) {
var array = [];
for (i = 0; i <= input; i++) {
array.push(i);
}
return array;
}
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);
});