遍历数组并删除最小的数字

Looping through array and removing smallest number

我想使用 for 循环查找数组中的最小数字 50 次,然后使用 splice() 将其删除。最后我想以一个递增的数字列表结束。我遇到的问题是我的程序只找到第一个数组的最小数字而不是更新的数组。

array=[]
for(i=0; i<50; i++) {
    array[i]=parseInt(Math.random()*100+1);
    }
min = Math.min(...array)
minindex = array.indexOf(min);
splice = array.splice(minindex, 1)
console.log(splice)        

也许是这样的?

const array = [];
//create array
for (let i = 0; i < 100; i++) {
  array.push(Math.floor(Math.random() * 100));
}
for (let i = 0; i < 50; i++) {
  array.splice(array.indexOf(Math.min.apply(Math, array)), 1)
}
array.sort()
console.log(array)

或者更简单,开头用array.sort()

const array = [];
    //create array
    for (let i = 0; i < 100; i++) {
      array.push(Math.floor(Math.random() * 100));
    }
    array.sort();
    for (let i = 0; i < 50; i++) {
      array.splice(0, 1)
    }
    console.log(array)

一个简单的方法...

const array = Array
  .from(
    { length: 50 },
    () => parseInt(Math.random() * 100 + 1),
  )
  .sort((a, b) => a - b);

// mutate/shorten `array`, remove all values equal to the minimum value.
array.splice(0, array.lastIndexOf(array[0]) + 1);

上述实现的证明...

const array = Array
  .from(
    { length: 50 },
    () => parseInt(Math.random() * 100 + 1),
  )
  .sort((a, b) => a - b);

console.log({
  arrayLength: array.length,
  minValue: array[0],
  sorted: array,
});

// mutate/shorten `array`, remove all values equal to the minimum value.
array.splice(0, array.lastIndexOf(array[0]) + 1);

console.log({
  arrayLength: array.length,
  shortened: array,
});
.as-console-wrapper { min-height: 100%!important; top: 0; }

通过主动 removing/slicing 项目改变数组的类似任务可以基于更通用的基于 reject 的方法...

function reject(targetArray, condition, target) {
  const rejectedItemList = [];

  let idx = targetArray.length;
  const copy = [...targetArray];

  // Processing the array from RIGHT to LEFT keeps the `idx` always in sync
  // with both related array items, the one of the mutated and also the one
  // of the non mutated version of the processed target array reference.
  // Thus the `condition` always gets passed the non mutated shallow copy.

  while (idx) {
    if (
      // take *sparse array* into account.
      targetArray.hasOwnProperty(--idx) &&

      // [item, idx, copy] called within `target` context.
      condition.call((target ?? null), copy[idx], idx, copy)
    ) {
      // keep filling the list of rejected items
      // FROM its LEFT side while mutating the target array.
      rejectedItemList.unshift(targetArray.splice(idx, 1)[0]);
    }
  }
  // returns an array of rejected items
  // but not the processed and mutated
  // target array reference.

  return rejectedItemList;
}

const array = Array
  .from(
    { length: 50 },
    () => parseInt(Math.random() * 100 + 1),
  );
const minValue =  Math.min(...array);

console.log({
  arrayLength: array.length,
  minValue,
  array,
});
console.log({
  rejectedItems: reject(array, value => value === minValue),
  arrayLength: array.length,
  sorted: array.sort((a, b) => a - b),
});
.as-console-wrapper { min-height: 100%!important; top: 0; }