Selecting Multiple Array Elements

select multiple array elements javascript

The easiest way, if you must use JavaScript, would be to set up a simple function, to which you pass the array and the indices:

function modifyStylesOf(arr, indices, prop, newValue) {

// here we filter the array to retain only those array elements
// are present in the supplied 'indices' array:
arr.filter(function(el, index) {

// if the index of the current element is present in the
// array of indices the index will be zero or greater,
// so those elements will be retained (as the assessment
// will be true/truthy:
return indices.indexOf(index) !== -1;

// we iterate over the retained Array elements using
// Array.prototype.forEach():
}).forEach(function (el) {

// and here we update the passed-in property
// to the passed-in value:
el.style[prop] = newValue;
});
}

Then call with:

// here we use Array.from() to convert the nodeList/HTMLCollection
// into an Array:
modifyStylesOf(Array.from(c), [1,3,5,7,9], 'webkitTextFillColor', 'transparent');

function modifyStylesOf(arr, indices, prop, newValue) {

arr.filter(function(el, index) {

return indices.indexOf(index) !== -1;

}).forEach(function(el) {

el.style[prop] = newValue;

});

}

var c = document.querySelectorAll('body div');

modifyStylesOf(Array.from(c), [1, 3, 5, 7, 9], 'webkitTextFillColor', 'orange');
div {

counter-increment: divCount;

}

div::before {

content: counter(divCount, decimal-leading-zero);

}
<div></div>

<div></div>

<div></div>

<div></div>

<div></div>

<div></div>

<div></div>

<div></div>

<div></div>

<div></div>

<div></div>

<div></div>

<div></div>

<div></div>

select multiple array elements at once

No.

Iterate through your array and apply your method to each appropriate item.

You could use slice to return a range of items in an array, which might make it easier for you, depending on what you're doing.

Javascript: randomly select multiple elements from an array (Not a single value selection)

This code produces n random elements from the array, and avoids duplicates:

const originalArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];

const randomSelection = (n) => {

let newArr = [];

if (n >= originalArray.length) {

return originalArray;

}

for (let i = 0; i < n; i++) {

let newElem = originalArray[Math.floor(Math.random() * originalArray.length)];

while (newArr.includes(newElem)) {

newElem = originalArray[Math.floor(Math.random() * originalArray.length)];

}

newArr.push(newElem);

}

return newArr;

}

console.log(randomSelection(2));

console.log(randomSelection(5));
.as-console-wrapper { max-height: 100% !important; top: auto; }

Selecting multiple array elements

I.e. instead of just looping through one array element at a time, but to loop through selected pairs instead (e.g. 3 elements, and then to do something to those 3).

there are many ways to do it.

one would be

$arr = array(1,2,3,4,5,6,7,8,9);
$new = array_chunk($arr,3);
foreach ($new as $chunk) {
print_r($chunk);// 3 elements to do something with
}

How to select multiple elements in an array with C?

There's nothing built-in that does it, you need to write a loop. Don't forget that array indexes start at 0.

int all_positive = 1;
int i;
for (i = 0; i < 10; i++) {
if (myArray[i] <= 0) {
all_positive = 0;
break;
}
}
if (all_positive) {
printf("Thank you for providing positive numbers!\n");
}

How to select multiple elements into array in Linq query?

Select<TSource,TResult> returns enumerable/queryable of the type returned by selector (IEnumerabe<TResult>/IQueryable <TResult>).

If you want to achieve this with LINQ you can use Aggregate:

// note that sourceQuantity and destinationTurbineType would be lists, not arrays
var (sourceQuantity, destinationTurbineType) = sourceProposal.Quotes
.Where(x=>x.QuotationId == sourceQuoteId)
.FirstOrDefault()
.QuoteLines
.Aggregate((ints: new List<int>(), strs: new List<string>()), (aggr, curr) =>
{
aggr.ints.Add(curr.Quantity);
aggr.strs.Add(curr.WtgType);
return aggr;
});

Or just use simple for loop and copy data to destination arrays (possibly move to some extension method). Something along this lines:

var quoteLines = sourceProposal.Quotes
.Where(x=>x.QuotationId == sourceQuoteId)
.FirstOrDefault()
.QuoteLines; // assuming it is materialized collection with indexer like an array or list
int[] sourceQuantity = new int[quoteLines.Length]; // or Count
string[] destinationTurbineType = new string[quoteLines.Count()];
for(int i = 0; i < quoteLines.Length; i++)
{
var curr = quoteLines[i];
sourceQuantity[i] = curr.Quantity;
destinationTurbineType[i] = curr.WtgType;
}

How do I select multiple values in an array?

You need loop

for (int i = 0; i < numbers.Length; i++)
{
if (numbers[i] % 2 == 0)
{
Console.WriteLine("EVEN");
}
}


Related Topics



Leave a reply



Submit