I have 3 arrays of same length. I'd like to subtract the values in the first two arrays,without clicking a button, and if the answer is negative, i get the index of that array number, and display a hidden input field(in html) from the 3rd array, whose array index corresponds to the array index of the negative number.
For example:
//assuming this is the first array
array('10','2','3','4','5');
//and assuming this is the second array
array('9','1','4','3','7');
i'd like to subtract the values of the second from the first array, such that i have results, for example:
array('1','1','-1','1','-2');
the above results determines which input fields(from the 3rd array) are to be displayed. For example the input fields to be displayed are those whose array indexes are 2 and 4
here's my code for the 3rd array of input fields:
<?php while($roww = mysql_fetch_array($result)) { ?>
<input type='text' name="reason[]" class="explanation">
<?php } ?>
is there any way i can make the above possible?
So far, this what i've tried, though i haven't applied arrays..just a slight update on a sample i found on the internet...
<html>
<head>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".explanation").hide();
var input1_ = document.getElementById('num1');
var input_ = document.getElementById('num2');
function sub(){
var answer = (parseInt(input1_.value) - parseInt(input2_.value));
if(answer < 0){
$(".explanation").show();
}else $(".explanation").hide();
}
document.getElementById('num1').onkeyup = function () {
sub();
};
document.getElementById('num2').onkeyup = function () {
sub();
};
});
</script>
</head>
<body>
<input type='number' id='num1' value="0">
<input type='number' id='num2' value="0">
<input type='text' class='explanation' value="0">
</body>
</html>
I have 3 arrays of same length. I'd like to subtract the values in the first two arrays,without clicking a button, and if the answer is negative, i get the index of that array number, and display a hidden input field(in html) from the 3rd array, whose array index corresponds to the array index of the negative number.
For example:
//assuming this is the first array
array('10','2','3','4','5');
//and assuming this is the second array
array('9','1','4','3','7');
i'd like to subtract the values of the second from the first array, such that i have results, for example:
array('1','1','-1','1','-2');
the above results determines which input fields(from the 3rd array) are to be displayed. For example the input fields to be displayed are those whose array indexes are 2 and 4
here's my code for the 3rd array of input fields:
<?php while($roww = mysql_fetch_array($result)) { ?>
<input type='text' name="reason[]" class="explanation">
<?php } ?>
is there any way i can make the above possible?
So far, this what i've tried, though i haven't applied arrays..just a slight update on a sample i found on the internet...
<html>
<head>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".explanation").hide();
var input1_ = document.getElementById('num1');
var input_ = document.getElementById('num2');
function sub(){
var answer = (parseInt(input1_.value) - parseInt(input2_.value));
if(answer < 0){
$(".explanation").show();
}else $(".explanation").hide();
}
document.getElementById('num1').onkeyup = function () {
sub();
};
document.getElementById('num2').onkeyup = function () {
sub();
};
});
</script>
</head>
<body>
<input type='number' id='num1' value="0">
<input type='number' id='num2' value="0">
<input type='text' class='explanation' value="0">
</body>
</html>
Share
Improve this question
edited Sep 30, 2016 at 13:26
Kenn
asked Sep 30, 2016 at 13:03
KennKenn
211 gold badge2 silver badges5 bronze badges
2
- 2 Yes, it is possible. What have you tried? – VLAZ Commented Sep 30, 2016 at 13:04
- i've updated the question, to show what i've done so far.. – Kenn Commented Sep 30, 2016 at 13:29
6 Answers
Reset to default 2 var a = Array('10','2','3','4','5');
var b = Array('9','1','4','3','7');
var c = a.map(function(v,i) { return (v - b[i]); });
You can use a loop:
var result = [];
for (let i=0; i<arr1.length; i++) {
result[i] = arr1[i]-arr2[i];
}
You may use map:
creates a new array with the results of calling a provided function on every element in this array.
The snippet:
var firstArr = ['10','2','3','4','5'];
var secondArr = ['9','1','4','3','7'];
//
// for each element in the first array
//
var result = firstArr.map(function(val, index) {
// convert the current array value to number
// subtract the value contained in the second array
// and return the value
return +val - +secondArr[index];
});
console.log(result);
As long as your arrays are the same length, this should work..
function subtrArrays(arr1[], arr2[])
{
var final = [];
for(var i = 0; i < arr1.length(); i++)
{
final[i] = arr1[i] - arr2[i];
}
return final;
}
Use:
var myArray = subtrArrays(array('10','2','3','4','5'),array('9','1','4','3','7'));
This is one of those cases which seems like Array.prototype.map()
is the best fit but in fact it is not. If you consider the job as a whole Array.prototype.forEach()
seems better since the return value of Array.prototype.map()
is going to be useless for us. It's best to finalize the job in an iterating function which doesn't return anything.
var a = ['10','2','3','4','5'],
b = ['9','1','4','3','7'],
divs = [div1,div2,div3,div4,div5];
a.forEach((e,i) => e-b[i] < 0 && (divs[i].className = divs[i].className.replace(/\s*hidden\s*/,"")));
.hidden {display:none}
<div id="div1" class="hidden">div1 text</div>
<div id="div2" class="hidden">div2 text</div>
<div id="div3" class="hidden">div3 text</div>
<div id="div4" class="hidden">div4 text</div>
<div id="div5" class="hidden">div5 text</div>
You may decide to use forEach method . The forEach method is a method that is used to define the behavior of every element in an array. Let’s say subtract one from each element in the given array
let newArr=[ ]
const array= [ '10','2','3','4','5']
array.forEach(element =>{
newArr.push(element-1)
})
Console.log(newArr)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745384452a4625376.html
评论列表(0条)