Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm looking for a way to sort an array with words and numbers on each array by highest number
E.g.
'jimmy=40'
'bobby=100'
'cattie=30'
'marrie=50'
'jonny=60'
turns into
'bobby=100'
'jonny=60'
'marrie=50'
'jimmy=40'
'cattie=30'
Is there any solutions for this problem ?
You can split the array items with = and compare the number part inside sort() event handler function:
var arr = ['jimmy=40', 'bobby=100', 'cattie=30', 'marrie=50', 'jonny=60'];
function sortData(data){
return data.sort(function(a, b){
return b.split('=')[1] - a.split('=')[1];
});
}
console.log(sortData(arr));
You can do it in three steps.
First split the array items by equal sign.
Sort the array based on number.
Again join array items by equal sign.
let data = ["jimmy=40", "bobby=100", "cattie=30", "marrie=50", "jonny=60"];
let ret = data
.map((x) => (x = x.split("=")))
.sort((x, y) => y[1] - x[1])
.map((x) => x.join("="));
console.log(ret);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
Improve this question
I have an array: a=[10,11,12,13]. How can check elements from a[0] to a[2] if they >0 in the shortest way and don't use for loop? Does anyone have any ideas? Thank you.
You can slice the array to get the items within the specified indexes, then use filter to filter out the items that don't match the condition and check whether the length of the resulting array is the length of the range.
const a = [10, 11, 12, 13]
const isValid = a.slice(0, 3).filter(e => e > 0).length == 3
//last index in slice is exclusive
console.log(isValid)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I've been looking for a way to return elements in an array based on a dynamic number. This dynamic number will be the number of required index positions for each array element, if each element can satisfy the required number of index positions (its own position counts towards the requirement) then that element should be returned.
const arr = [1,2,3,4,5,6]
const requiredIndexes = 3
So with the variables listed above I should return [1,2,3,4] because 5 and 6 will not be able to return three index positions.
The Array.prototype.slice might give you some help. Please refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
const arr = [1,2,3,4,5,6]
const requiredIndexes = 3
console.log(arr.slice(0, arr.length - requiredIndexes + 1))
I am not an expert but I think you would want to iterate up to the position. Are you having the dynamic number be a random number? You may want to also clone your array and use the clone as the worker array. Not sure exactly what you are wanting to do with the array, but you can then slice from the array and the parent array stays unefected.
function pickposition() {
var randIndex = Math.floor(Math.random() * clone.length);
rand = clone[randIndex];
clone.splice(randIndex,1);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
It may be silly but i need to write an logic to get integer to split to array based on size.
For example if integer is 13 i need to convert to array as [5,5,3] if my size is 5.
if size is 10 then array should be [10,3].
Can any one help me writing the logic in javascript?.
You may build up an array of size N/n if there's no remainder from division (N%n), or N/n+1 if there is one.
Then you may simply Array.prototype.map() through that array and populate the result with n or use remainder as the last element if one exists:
const num = 13,
size = 5,
splitNumber = (N, n) =>
[...Array(0|N/n+(N%n ? 1 : 0))]
.map((_,i,s) =>
N%n && i == s.length-1 ? N%n : n)
console.log(splitNumber(13,5))
console.log(splitNumber(13,10))
.as-console-wrapper{min-height:100%;}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to compare two arrays
array1=["123',"456"];
array2=[{id":"001",name:"prashant"},{id:"123",name:"jhh"},{id:"123444",name"baak"},{id:"456",name"sxs"}];
my objective is to extract the objects from array2 whose ids match the values in array1.
Can someone help me with the optimal solution?
First of all your second array (array2) is syntactically invalid.
You can try with Array.prototype.filter() and Array.prototype.includes()
var array1=["123","456"];
var array2 = [{id:"001",name:"prashant"},{id:"123",name:"jhh"},{id:"123444",name:"baak"},{id:"456",name:"sxs"}];
var res = array2.filter(i => array1.includes(i.id));
console.log(res);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Var randomLetters = "ljhgfdza";
Var randomString = "";
Now I'll have to add elements in the first variable to the second Randomly
Using the while loop, and Math.floor(Math.random() * random letters.length)
I am having a problem at my
"While (condition)" what should it be?
To flat out answer your question, you can use:
while(randomLetters.length > 0){
Then when you use a letter from randomLetters, you delete the letter and the length is now 1 less.
This will be enough for you:
const randomLetters = "ljhgfdza";
const returnRandom = (randomString) => {
const arrString = [...randomString].sort((a, b) =>{
return 0.5 - Math.random()
}).join("");
console.log(typeof arrString,arrString);
}
returnRandom(randomLetters);
but... in this case sort method is not that random as you think. This link will tell you why. I would do this with reduce() or map(), both are described in link above.