How to get first N number of elements from an array
I believe what you're looking for is:
// ...inside the render() function
var size = 3;
var items = list.slice(0, size).map(i => {
return <myview item={i} key={i.id} />
});
return (
<div>
{items}
</div>
)
Ruby on Rails - Get first n number of elements from array with condition
When you only want to show the first 3 comments that match a certain condition then I would do this:
<% matching_comments = @comments.select { |comment| comment["postId"] == post["id"] } %>
<% matching_comments.first(3).each do |comment| %>
<td><%= comment["body"] %></td>
<% end %>
<td><%= matching_comments.size %></td>
You might load the @comments with the condition in the controller to make the view easier to read.
Javascript Array - get first 10 items
This will do the job. Replace [startIndex]
and [endIndex]
with 0 and 10 to get the first 10.
data.slice([startIndex], [endIndex]).map((item, i) => {
placeIDs.push(item.place_id);
});
Get the first N elements of an array?
Use array_slice()
This is an example from the PHP manual: array_slice
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"
There is only a small issue
If the array indices are meaningful to you, remember that array_slice
will reset and reorder the numeric array indices. You need the preserve_keys
flag set to true
to avoid this. (4th parameter, available since 5.0.2).
Example:
$output = array_slice($input, 2, 3, true);
Output:
array([3]=>'c', [4]=>'d', [5]=>'e');
jq: get first n elements of an array field
If you are looking to print only those specific fields on the first 3 results, use the slice operator. See jqplay for demo
.identifier[:3] | map({system, value})
return the first n elements of a k sized array in O(1) time
If you must work with raw arrays and not ArrayList
then Arrays
has what you need. If you look at the source code, these are the absolutely best ways to get a copy of an array. They do have a good bit of defensive programming because the System.arraycopy()
method throws lots of unchecked exceptions if you feed it illogical parameters.
You can use either Arrays.copyOf()
which will copy from the first to Nth
element to the new shorter array.
public static <T> T[] copyOf(T[] original, int newLength)
Copies the specified array, truncating or padding with nulls (if
necessary) so the copy has the specified length. For all indices that
are valid in both the original array and the copy, the two arrays will
contain identical values. For any indices that are valid in the copy
but not the original, the copy will contain null. Such indices will
exist if and only if the specified length is greater than that of the
original array. The resulting array is of exactly the same class as
the original array.
2770
2771 public static <T,U> T[] More ...copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
2772 T[] copy = ((Object)newType == (Object)Object[].class)
2773 ? (T[]) new Object[newLength]
2774 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
2775 System.arraycopy(original, 0, copy, 0,
2776 Math.min(original.length, newLength));
2777 return copy;
2778 }
or Arrays.copyOfRange()
will also do the trick:
public static <T> T[] copyOfRange(T[] original, int from, int to)
Copies the specified range of the specified array into a new array.
The initial index of the range (from) must lie between zero and
original.length, inclusive. The value at original[from] is placed into
the initial element of the copy (unless from == original.length or
from == to). Values from subsequent elements in the original array are
placed into subsequent elements in the copy. The final index of the
range (to), which must be greater than or equal to from, may be
greater than original.length, in which case null is placed in all
elements of the copy whose index is greater than or equal to
original.length - from. The length of the returned array will be to -
from. The resulting array is of exactly the same class as the original
array.
3035 public static <T,U> T[] More ...copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
3036 int newLength = to - from;
3037 if (newLength < 0)
3038 throw new IllegalArgumentException(from + " > " + to);
3039 T[] copy = ((Object)newType == (Object)Object[].class)
3040 ? (T[]) new Object[newLength]
3041 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
3042 System.arraycopy(original, from, copy, 0,
3043 Math.min(original.length - from, newLength));
3044 return copy;
3045 }
As you can see, both of these are just wrapper functions over System.arraycopy
with defensive logic that what you are trying to do is valid.
System.arraycopy
is the absolute fastest way to copy arrays.
How to find the number of the elements of an array that are in a specific range of values
can you try this one.
in this way it do not sort, but it will display values, that are between the numbers the user enters
#include <stdio.h>
#define SIZE 5
int main(void){
float grades[SIZE];
float g;
int i;
for (i=0;i<SIZE;i++){
printf("Enter a grade: ");
scanf("%f", &g);
grades[i]= g;
}
float a,b;
printf("Enter a grade range: \n");
scanf("%f %f", &a,&b);
for (i=0;i<SIZE;i++){
if(a>b){
if(grades[i]>=b && grades[i]<=a){
printf("%f ", grades[i]);
}
}else{
if(grades[i]>=a && grades[i]<=b){
printf("%f ", grades[i]);
}
}
}
return 0;
}
Related Topics
Is It Any Limit for Post Data Size in Ajax
Convert Image from Url to Base64
Checking for Null Is Not Working for Ajax Json Data
How to Add a New Object (Key-Value Pair) to an Array in JavaScript
Open Html5 Date Picker on Icon Click
Ngx-Datatable Set Column Width Dynamically
How to Clear Input After Onclick With Reactjs
Filter Json Object Array on Multiple Values or Arguments JavaScript
How to Add Active Class/State for Li Element in Reactjs
Detect If User Is Using Webview for Android/Ios or a Regular Browser
How to Clear the Focus in JavaScript
How to Extract the User Name from an Email Address Using JavaScript
Can We Map Over Two Arrays At Same Time in Js
Most Efficient Method to Groupby on an Array of Objects
Regular Expression Using JavaScript for Removing the Special Characters
How to Convert All Elements in an Array to Integer in JavaScript
How to Bind on Click Event on Dynamically Created Button in Angular 6