Javascript: check multiple elements in array [closed] - javascript

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)

Related

javascript : Sort array with number and alphabetical [closed]

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);

Split integer to array based on size using javascript [closed]

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%;}

How do I get the equivalent index of another array? [closed]

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
Here, I have two arrays.
let x = [0, 1, 2]
let y = [3, 4, 5]
What is the code so that when I input an index of array x, I get array y's equivalent? So like, I want to output 3 when I input x[0].
You can index y array with x elements (i is equals to x[i] in your case, is it wanted?) y[x[i]] or y[i]
If you insert a new item in array x using push operation ie. x.push(val), then corresponding element in array y will be:
y[x.length-1];
I have an array: y[x[i]]
but check for index range for array Y else you will get undefined.

Comparing a string array with object array [closed]

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);

Get the minimum value from the array [closed]

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 5 years ago.
Improve this question
I am trying to get the minimum value from an array without changing the way the the value are aligned.
function removeSmallest(numbers){
var min=Math.min.apply(null,numbers);
var indexvalue=numbers.indexOf(min);
delete numbers[indexvalue];
return numbers;
}
Two issues:
Math.min.apply(null, numbers) should be Math.min.apply(Math, numbers) (note Math instead of null)
Normally, delete is not the right tool for arrays (see this question's answers for details; use splice to remove array entries if you're going to modify the array in place:
numbers.splice(indexvalue, 1);
You might also consider creating and returning a new array instead of mutating and returning the same array you received.

Categories