简体   繁体   English

如何从JavaScript中的数组中删除对象?

[英]how to remove object from array in javascript?

I have two arrays totalArray and selectionArray , these values are always dynamic but for example purpose I given below that arrays, Now I want to remove selectionArray from totalArray using id not index because I have only five values per page. 我有两个数组totalArrayselectionArray ,这些值始终是动态的,但出于示例目的,我在该数组下面给出了这些值,现在,我想使用id而不是index从totalArray删除selectionArray ,因为每页只有五个值。 so if I remove value using index then it is removing values from other page also because page wise index is same found. 因此,如果我使用索引删除值,那么它也会从其他页面删除值,这也是因为找到了逐页索引。 so how to remove object using id? 那么如何使用ID删除对象? I use splice but is using other methods or any other solution available? 我使用拼接器,但是是否使用其他方法或任何其他解决方案?

  totalArray = [
     {id: 1, city: 'LONDON'},
     {id: 2, city: 'PARIS'},
     {id: 3, city: 'NEW YORK'},
     {id: 4, city: 'BERLIN'},
     {id: 5, city: 'MADRID'},
     {id: 6, city: 'ROME'},
     {id: 7, city: 'DUBLIN'},
     {id: 8, city: 'ATHENS'},
     {id: 9, city: 'ANKARA'},
     {id: 10, city: 'MOSCOW'},
  ]      

   selectionArray = [
     {id: 6, city: 'ROME'},
     {id: 7, city: 'DUBLIN'},
     {id: 8, city: 'ATHENS'},
     {id: 9, city: 'ANKARA'},
     {id: 10, city: 'MOSCOW'},
  ]

  selectionArray.forEach((item, i) => { 
      totalArray.splice(item.id, 1);
  });

Assuming you want to alter the original array , here is a solution following this approach: 假设您要更改原始数组 ,以下是采用此方法的解决方案:

  • Acquire the id of the looped item selectionArray, taking advantage of array.forEach . 获取id环状项目selectionArray的,趁着array.forEach
  • Find, in total array, the element with the same index of the looped one. 在整个数组中查找与循环的索引相同的元素。
  • Acquire its index, and splice the element from the array. 获取其索引,然后从数组中拼接元素。

This should be pretty similar to what you've tried to achieve with your code, I've tried to stay as close as possible to your try. 这应该与您尝试使用代码实现的效果非常相似,但我尝试尽可能地接近您的尝试。

 const totalArray = [ {id: 1, city: 'LONDON'}, {id: 2, city: 'PARIS'}, {id: 3, city: 'NEW YORK'}, {id: 4, city: 'BERLIN'}, {id: 5, city: 'MADRID'}, {id: 6, city: 'ROME'}, {id: 7, city: 'DUBLIN'}, {id: 8, city: 'ATHENS'}, {id: 9, city: 'ANKARA'}, {id: 10, city: 'MOSCOW'}, ], selectionArray = [ {id: 6, city: 'ROME'}, {id: 7, city: 'DUBLIN'}, {id: 8, city: 'ATHENS'}, {id: 9, city: 'ANKARA'}, {id: 10, city: 'MOSCOW'}, ]; selectionArray.forEach(({id: uid}) => { totalArray.splice( totalArray.indexOf(totalArray.find(({id}) => uid === id)), 1 ); }); console.log(totalArray); 

Please note that if both array elements comes from the same array source you can simply use indexOf . 请注意,如果两个数组元素都来自同一数组源,则可以简单地使用indexOf

This might not be the best option. 这可能不是最佳选择。 Basically it cycles both arrays and when the id is matching it removes the key from the first array 基本上,它循环两个数组,并且当id匹配时,它将从第一个数组中删除键

 totalArray = [ {id: 1, city: 'LONDON'}, {id: 2, city: 'PARIS'}, {id: 3, city: 'NEW YORK'}, {id: 4, city: 'BERLIN'}, {id: 5, city: 'MADRID'}, {id: 6, city: 'ROME'}, {id: 7, city: 'DUBLIN'}, {id: 8, city: 'ATHENS'}, {id: 9, city: 'ANKARA'}, {id: 10, city: 'MOSCOW'}, ] selectionArray = [ {id: 1, city: 'LONDON'}, {id: 7, city: 'DUBLIN'}, {id: 8, city: 'ATHENS'}, {id: 9, city: 'ANKARA'}, {id: 10, city: 'MOSCOW'}, ] for (key in totalArray){ for (removeKey in selectionArray){ if (totalArray[key].id == selectionArray[removeKey].id){ totalArray.splice(key,1) } } } console.log(totalArray) 

You can use Array#filter to remove items from an array. 您可以使用Array#filter从数组中删除项目。 Combine this with Array#every you can ensure that the item is not found in the removal array. 将此与Array#every结合使用,可以确保在移除数组中找不到该项目。

 const totalArray = [ { id: 1, city: "LONDON" }, { id: 2, city: "PARIS" }, { id: 3, city: "NEW YORK" }, { id: 4, city: "BERLIN" }, { id: 5, city: "MADRID" }, { id: 6, city: "ROME" }, { id: 7, city: "DUBLIN" }, { id: 8, city: "ATHENS" }, { id: 9, city: "ANKARA" }, { id: 10, city: "MOSCOW" } ]; const selectionArray = [ { id: 6, city: "ROME" }, { id: 7, city: "DUBLIN" }, { id: 8, city: "ATHENS" }, { id: 9, city: "ANKARA" }, { id: 10, city: "MOSCOW" } ]; const remainingArray = totalArray.filter(city => selectionArray.every(selCity => selCity.id !== city.id) ); console.log(remainingArray); 

The filter function works by calling a callback for each item. filter函数通过为每个项目调用回调来工作。 If the callback returns true the item is kept. 如果回调返回true ,则保留该项目。 If the callback returns false the item is removed. 如果回调返回false ,则删除该项目。 The function returns the remaining values. 该函数返回剩余值。

The every function works by calling a callback for each item in the array. every函数通过为数组中的每个项目调用回调来工作。 If the callback returns true for all item in the array, the function returns true . 如果回调函数返回true数组中的所有项目,该函数返回true If the callback returns false for any item then the function returns false . 如果回调对任何项目返回false ,则该函数返回false

For every city in totalArray , we check to see if the id is not in every single one of the cities from selectionArray . 对于totalArray每个city ,我们都检查id是否不在 selectionArray中的每个城市中。

 const totalArray = [ {id: 1, city: 'LONDON'}, {id: 2, city: 'PARIS'}, {id: 3, city: 'NEW YORK'}, {id: 4, city: 'BERLIN'}, {id: 5, city: 'MADRID'}, {id: 6, city: 'ROME'}, {id: 7, city: 'DUBLIN'}, {id: 8, city: 'ATHENS'}, {id: 9, city: 'ANKARA'}, {id: 10, city: 'MOSCOW'}, ]; const selectionArray = [ {id: 6, city: 'ROME'}, {id: 7, city: 'DUBLIN'}, {id: 8, city: 'ATHENS'}, {id: 9, city: 'ANKARA'}, {id: 10, city: 'MOSCOW'}, ]; // Using splice // We loop over the "selectionArray" and use .findIndex() to find the index of each object. const result_splice = [ ...totalArray ]; selectionArray.forEach( selection => { const index_in_totalArray = result_splice.findIndex( total => total.id === selection.id ); result_splice.splice( index_in_totalArray, 1 ); }); console.log( 'RESULT_SPLICE' ); console.log( result_splice ); // Using .filter(); // We extract all the selected ids and then .filter() the "totalArray" to remove // all items with an id found inside "selected_ids" const selected_ids = selectionArray.map( selection => selection.id ); const result_includes = totalArray.filter( total => !selected_ids.includes( total.id )); console.log( 'RESULT_INCLUDES' ); console.log( result_includes ); // Using .find() // Almost the same as using .filter(), but instead of extracting the ids beforehand, we just loop over the "selectedArray" with .find() const result_find = totalArray.filter( total => { return !selectionArray.find( selection => selection.id === total.id ); }); console.log( 'RESULT_FIND' ); console.log( result_find ); const json_splice = JSON.stringify( result_splice ); const json_includes = JSON.stringify( result_includes ); const json_find = JSON.stringify( result_find ); console.log( 'All results are equal:' );json_find && console.log( json_splice === json_includes && json_splice === json_find && json_includes === json_find ); 

Fastest and simplest way will be: 最快,最简单的方法是:

 const totalArray = [ { id: 1, city: 'LONDON' }, { id: 2, city: 'PARIS' }, { id: 3, city: 'NEW YORK' }, { id: 4, city: 'BERLIN' }, { id: 5, city: 'MADRID' }, { id: 6, city: 'ROME' }, { id: 7, city: 'DUBLIN' }, { id: 8, city: 'ATHENS' }, { id: 9, city: 'ANKARA' }, { id: 10, city: 'MOSCOW' }, ] const selectionArray = [ { id: 6, city: 'ROME' }, { id: 7, city: 'DUBLIN' }, { id: 8, city: 'ATHENS' }, { id: 9, city: 'ANKARA' }, { id: 10, city: 'MOSCOW' }, ]; const ids = selectionArray.map(({ id }) => id); const filteredArray = totalArray.filter(({ id }) => !ids.includes(id)); console.log(filteredArray); 

in javascript you could get the index of a value using next: 在javascript中,您可以使用next获取值的索引:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.indexOf("Apple");

then to remove the that item: 然后删除该项目:

arr.splice(a, 1);

Hope it could help if not please see: 希望它能对您有所帮助,请参阅:

Love2Dev Removing Array Items By Value Using Splice Love2Dev使用接头按值删除数组项

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

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