简体   繁体   English

从数组中删除指定值

[英]remove specified value from array

How can I check whether an array contains a value, and if so, remove it?如何检查数组是否包含值,如果包含,将其删除? PS: For this exercise, I'm not allowed to use anything more than than .pop, .push and .length array functions. PS:对于这个练习,除了 .pop、.push 和 .length 数组函数之外,我不允许使用任何东西。 My logic is the following: if the specified value is within the array, reorder the array so that the last element of it will contain this value, then remove it with .pop.我的逻辑如下:如果指定的值在数组内,则重新排列数组,使其最后一个元素包含此值,然后使用 .pop 将其删除。 But how can I find this value and reorder it without using anything more than those array functions I specified above?但是,除了我上面指定的那些数组函数之外,我如何才能找到这个值并对其重新排序呢?

This is what I managed to come up with so far:这是我到目前为止设法想出的:

 let array_1 = [1,2,3]; if (array_1 == 2){ //reorder somehow array_1.pop(); } console.log(array_1);

If you are limited to pop , push and length , you can loop over all elements, check if a given element matches the value you are looking for, and add them to a new array using push.如果仅限于poppushlength ,则可以遍历所有元素,检查给定元素是否与您要查找的值匹配,然后使用 push 将它们添加到新数组中。

 let array_1 = [1,2,3]; let newArray = []; for (let i = 0; i < array_1.length; i++) { if (array_1[i] !== 2) { newArray.push(array_1[i]); } } console.log(newArray);

Here's another option using pop这是使用 pop 的另一种选择

 const filter = (array, target) => { const newArray = []; let tmp; while(tmp = array.pop()) { if (tmp !== target) { newArray.push(tmp) } } console.log(newArray) return newArray; } filter([1,2,3,4], 2) // [4, 3, 1] Note that it reversed the order of the array!

Using this approach, you are not creating a new array but modifying it.使用这种方法,您不是创建一个新数组,而是修改它。 It uses .pop() .它使用.pop()

let array_1 = [1, 2, 3];

// Iterate all array
for (let i = 0; i < array_1.length; i++) {
    // While there is a 2 element in the actual index, move all elements (from i index) to the previous index
    while(array_1[i] === 2) {
      for (let j = i; j < array_1.length - 1; j++) {
        array_1[j] = array_1[j + 1];
      }
      // Now remove the last element (since we move all elements to the previous index)
      array_1.pop();
    }
}

console.log(array_1);

Here a snippet so you can try it这是一个片段,以便您可以尝试

 let array_1 = [1, 2, 3, 4, 2, 5, 2, 6, 2]; for (let i = 0; i < array_1.length; i++) { while(array_1[i] === 2) { for (let j = i; j < array_1.length - 1; j++) { array_1[j] = array_1[j + 1]; } array_1.pop(); } } console.log(array_1);

This would mantain the order of the array, but without the "2" elements.这将保持数组的顺序,但没有“2”元素。

 // using splice // splice(indexStart, how many, replace with) // example : let arr = [0,1,2,3,4,5]; // remove at index 1 arr.splice(1,1); console.log( arr ); // replace at index 1 arr.splice(1,1,"new 1"); console.log( arr ); // merge index 2 and 3 arr.splice(2,2,"merge 2 and 3"); console.log( arr ); // create 2 new items start at index 2 arr.splice(2,2,"new 2", "new 3"); console.log( arr );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM