我需要能够从cutArray
中获得两个值,以在lengthInputs
中创建新值。到目前为止,这是我的代码:
function calculate() {
let cutArray = Array.from(document.querySelectorAll(".cutLength").values()); //gets all input box values as string
let quantityArray = Array.from(document.querySelectorAll(".quantity").values());
let lengthInputs = [];
//loops through input values and adds each individual index together as an integer then returns it to lengthInputs array
for (i = 0; i < cutArray.length; i++) {
console.log(cutArray[i].value);
for (k = 0; k < cutArray.length; k++) {
lengthInputs.push(parseInt(cutArray[i].value) + parseInt(cutArray[k].value));
console.log(cutArray[k].value + "k");
}
}
答案 0 :(得分:0)
您可以使lengthInputs
包含具有first
,second
和sum
属性的对象,其中first
和second
是具有{{ 1}}(在index
中的索引)和cutArray
(value
)和cutArray[index]
是两个值的总和:
sum
如果不需要function calculate() {
const cutArray = Array.from({length: 10}, (_) => Math.random() * 100)
const quantityArray = Array.from({length: 10}, (_) => Math.random() * 100)
const lengthInputs = [];
for (let i = 0; i < cutArray.length; i++) {
for (let k = 0; k < cutArray.length; k++) {
lengthInputs.push({
first: { index: i, value: cutArray[i] },
second: { index: k, value: cutArray[k] },
sum: cutArray[i] + cutArray[k]
});
}
}
return lengthInputs;
}
console.log(calculate())
和first
的索引,也可以将它们作为特定值。
https://jsfiddle.net/q8gtdhws/5/