添加到数组兄弟

时间:2017-05-17 18:16:49

标签: javascript arrays

我有一个包含10个项目的数组:

| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

每个数组都包含一个0的int。

我想要做的是将值10添加到索引4.然后让3个外部索引接收减半的值。这是一个直观的例子:

| 0 | 1.25 | 2.5 | 5 | 10 | 5 | 2.5 | 1.25 | 0 |

所以我选择的任何索引都会得到10的值。左边和右边的第一个项将获得该值/ 2,更多的外部将获得该值/ 4等。

var indexesToCheck = 3; // Will check 3 before and 3 after the selected index
var array = [];
var arrayItems = 10;

for(var i = 0; i < arrayItems.length; i++) {
  array.push(0);
}

function setArrayValue(i, value) {
  array[i] = value;

  // How do I go about checking the next and previous 3 values and setting the halved value?
}

setArrayValue(4, 10);

非常感谢正确推动。

谢谢!

10 个答案:

答案 0 :(得分:3)

从示例代码的外观来看,您似乎想要修改数组,而不是返回一个新数组。以下是我的实施:

var indexesToCheck = 3;
var array = new Array(10).fill(0);

function setArrayValue(i, value) {
  array[i] = value;
  for (let j = 1; j <= indexesToCheck; j++) {
    value /= 2;
    array[i - j] = value;
    array[i + j] = value;
  }
}
setArrayValue(4, 10)
console.log(array);

答案 1 :(得分:1)

你可以使用两个指针,分别递增和递减它们,直到它们到达数组边界。

var indexesToCheck = 3;  //Will check 3 before and 3 after the selected index

//you don't need that variable above

var array = [];
var arrayItems = 10;

for (var i = 0; i < arrayItems; i++) {
  array.push(0);
}

function setArrayValue(i, value) {
  array[i] = value;
  var originalVal = value;

  var pointer1 = i - 1;
  var pointer2 = i + 1;

  while (pointer1 >= 0) {
    array[pointer1] = value / 2;
    value = value / 2;
    pointer1--;
  }

  value = originalVal;

  while (pointer2 < array.length) {
    array[pointer2] = value / 2;
    value = value / 2;
    pointer2++;
  }
}

setArrayValue(4, 10);

console.log(array);

当然,这可能会更加优雅和优化,但这可以让你开始,因为我认为这是最简单的自我记录,以便易于理解逻辑,试着想一想你如何做到这一点只需一个循环就可以干净利落,以及如何避免使用额外的变量。

答案 2 :(得分:1)

你可以每次迭代并将之前的数字除以2,然后你写下一个索引,然后下一个索引

var indexesToCheck = 3; // Will check 3 before and 3 after the selected index
var array = [];
var arrayItems = 10;

for (var i = 0; i < arrayItems; i++) {
  array.push(0);
}

function setArrayValue(start, value) {
  array[start] = value;  // set initial
  var prev = value;      // keep previous

  for (var i = 0; i < indexesToCheck; i++) {
    var p = prev / 2;           // divide by two
    if ( (start - i - 1) > -1 )          array[start - i - 1] = p;
    if ( (start + i + 1) < array.length) array[start + i + 1] = p;
    prev = p;
  }
}

setArrayValue(4, 10);

console.log(array);

答案 3 :(得分:1)

将逻辑分成两个独立的循环。当你离开给定索引时,一个将处理值减半,并且一个将处理它正确。例如:

function initializeArray(array, index, initialValue) {
    array[index] = initialValue;

    var i;

    //Handles logic going left (decreasing indices)
    var currentValue = initialValue;
    for(i = index-1; i >= 0; i--) {
        currentValue /= 2.0;
        array[i] = currentValue;
    }

    //Handles logic going right (increasing indices)
    currentValue = initialValue;
    for(i = index+1; i < array.length; i++) {
        currentValue /= 2.0;
        array[i] = currentValue;
    }
}

答案 4 :(得分:1)

一个简单的解决方案可能是

var leftIndex = 6; //Index of value
var rightIndex = 6; //Index of value 
var val = 10; //Value at Index 
var arr=[];
arr[leftIndex]=val;
while(leftIndex>0) {
    leftIndex--;
    arr[leftIndex] = arr[leftIndex+1]/2;
}
while(rightIndex<9) {
    rightIndex++;
    arr[rightIndex] = arr[rightIndex-1]/2;
}

答案 5 :(得分:1)

您可以计算数组项的值。

&#13;
&#13;
var length = 9,
    center = 10,
    border = 0,
    array = Array.apply(null, { length: length }).map(function (a, i, aa) {
        return i === 0 || i + 1 === aa.length ?
            border :
            center / (1 << Math.abs(Math.floor(aa.length / 2) - i));
    });

console.log(array);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 6 :(得分:1)

这应该可以解决问题。我不确定元素是否包含您想要保留的值,或者是否应该用零覆盖它们,但这很容易改变。

var arr = [0,0,0,0,0,0,0,0,0,0,0];

var changed = doThing(arr, 4, 10, 3); // add 10 to array el 4 and affect elements within 3

function doThing(arr, index, value, spread) {
  return arr.map(function(el, i) {
    var d = Math.abs(index - i);
    if (d <= spread) {
      return value * Math.pow(0.5, d);
    }
    return 0;
  });
}
      
console.log(changed);

  

答案 7 :(得分:1)

// We need to loop over the offset from the original index
for (var offset=1; offset <= indexToCheck; offset++) {
    // We want to handle moving offset to the left
    if(start - offset >= 0){ // Ensure we're still in bounds
        array[start-offset] += value/(2 * offset); // Add value divided by 2 for each offset
    }
    // We want to handle moving offset to the right
    if (start + offset < array.length) { // Ensure we're still in bounds
        array[start+offset] += value/(2 * offset);
    }
}

答案 8 :(得分:1)

@FXML
void handleStartButton(ActionEvent event) {
    drawerController.startScanning();
}

@FXML
void handleStopButton(ActionEvent event) {
    drawerController.startScanning();
}

答案 9 :(得分:0)

该问题的通用解决方案是:

  1. pivot (在本例中为pivot = 4)为指定 threshold (在本例中为threshold = 10)值且 indexBoundary (在这种情况下,indexBoundary = 3)是要检查的索引。
  2. 现在 leftStartIndex = max(0,pivot-indexBoundary)和 rightEndIndex = min(array.size() - 1,pivot + indexBoundary)。这也将处理边界情况。
  3. 从i-&gt; pivot-1开始到leftStartIndex(包括)do array [i] = threshold / 2 ^(pivot-i)。
  4. 同样地,从i-&gt; pivot + 1到rightEndIndex(包括)do array [i] = threshold / 2 ^(i-pivot)。
  5. 对于您的情况:

    pivot=4
    threshold=10
    indexBoundary=3
    
    for i->3 to 1, do
        array[i] = threshold/2^(pivot-i)
    i=3, array[3] = 10/2^(4-3) = 10/2 = 5
    i=2, array[2] = 10/2^(4-2) = 10/4 = 2.5
    i=1, array[1] = 10/2^(4-1) = 10/8 = 1.25
    
    
    for i->5 to 7, do
        array[i] = threshold/2^(i-pivot)
    i=3, array[3] = 10/2^(5-4) = 10/2 = 5
    i=2, array[2] = 10/2^(6-4) = 10/4 = 2.5
    i=1, array[1] = 10/2^(7-4) = 10/8 = 1.25
    

    如果有什么不清楚,请告诉我。