如果我在数组中添加2个数字并推送到新数组,如何从第二个数组中的值中找到这2个数字

时间:2019-04-05 07:58:02

标签: javascript ecmascript-6

我需要能够从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");
    }
  }

1 个答案:

答案 0 :(得分:0)

您可以使lengthInputs包含具有firstsecondsum属性的对象,其中firstsecond是具有{{ 1}}(在index中的索引)和cutArrayvalue)和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/