繁体   English   中英

删除数组中所有带有 id 的对象 - JS

[英]Delete all occurrences of an object with an id in an array - JS

如何删除对象数组中具有相同 ID 的重复对象? 我希望下面的代码只有 id 为 1 的对象。

这是我的解决方案:

 let data = [{ "selected": true, "id": 3, "ProductName": "Aniseed Syrup", "SupplierID": 1, "CategoryID": 2, "QuantityPerUnit": "12 - 550 ml bottles", "UnitPrice": 10, "UnitsInStock": 13, "UnitsOnOrder": 70, "ReorderLevel": 25, "Discontinued": false, "Category": { "CategoryID": 2, "CategoryName": "Condiments", "Description": "Sweet and savory sauces, relishes, spreads, and seasonings" } }, { "selected": true, "id": 3, "ProductName": "Aniseed Syrup", "SupplierID": 1, "CategoryID": 2, "QuantityPerUnit": "12 - 550 ml bottles", "UnitPrice": 10, "UnitsInStock": 13, "UnitsOnOrder": 70, "ReorderLevel": 25, "Discontinued": false, "Category": { "CategoryID": 2, "CategoryName": "Condiments", "Description": "Sweet and savory sauces, relishes, spreads, and seasonings" } }, { "selected": true, "id": 1, "ProductName": "Aniseed Syrup", "SupplierID": 1, "CategoryID": 2, "QuantityPerUnit": "12 - 550 ml bottles", "UnitPrice": 10, "UnitsInStock": 13, "UnitsOnOrder": 70, "ReorderLevel": 25, "Discontinued": false, "Category": { "CategoryID": 2, "CategoryName": "Condiments", "Description": "Sweet and savory sauces, relishes, spreads, and seasonings" } } ] let data1 = data.filter(item => { return _.isEqual(data.lastIndexOf(item.id), data.indexOf(item.id)) }) console.log(data1)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

它返回相同的列表。 我期望一个只有 id 1 对象的数组。

请就实现这一目标的方法提出建议。

PS:我也可以使用 lodash。

这个 lodash 函数会帮助它合并所有重复项吗

_.uniqBy(data,'id')

您可以使用一个object来跟踪id's和它的repetition count ,再次循环遍历数组并将其添加到最终输出,仅当它的计数为1

 let data = [{"selected": true,"id": 3,"ProductName": "Aniseed Syrup","SupplierID": 1,"CategoryID": 2,"QuantityPerUnit": "12 - 550 ml bottles","UnitPrice": 10,"UnitsInStock": 13,"UnitsOnOrder": 70,"ReorderLevel": 25,"Discontinued": false,"Category": {"CategoryID": 2,"CategoryName": "Condiments","Description": "Sweet and savory sauces, relishes, spreads, and seasonings"}},{"selected": true,"id": 3,"ProductName": "Aniseed Syrup","SupplierID": 1,"CategoryID": 2,"QuantityPerUnit": "12 - 550 ml bottles","UnitPrice": 10,"UnitsInStock": 13,"UnitsOnOrder": 70,"ReorderLevel": 25,"Discontinued": false,"Category": {"CategoryID": 2,"CategoryName": "Condiments","Description": "Sweet and savory sauces, relishes, spreads, and seasonings"}}, {"selected": true,"id": 1,"ProductName": "Aniseed Syrup","SupplierID": 1,"CategoryID": 2,"QuantityPerUnit": "12 - 550 ml bottles","UnitPrice": 10,"UnitsInStock": 13,"UnitsOnOrder": 70,"ReorderLevel": 25,"Discontinued": false,"Category": {"CategoryID": 2,"CategoryName": "Condiments","Description": "Sweet and savory sauces, relishes, spreads, and seasonings"}}] let tracker = {} data.forEach(({id}) => { tracker[id] = tracker[id] || 0 tracker[id]++ }) const final = data.filter(({id}) => tracker[id] === 1 ) console.log(final)

您不需要库,并且 ES6 在 0(n) 运行时完全有能力。 此外,您可能希望通过 ID 对数组内的对象进行规范化,以便更快地查找。

为了演示,我添加了第三个 id 为 4 的对象来显示此代码:

let data = [
  {"selected": true,"id": 3,"ProductName": "Aniseed Syrup","SupplierID": 1,"CategoryID": 2,"QuantityPerUnit": "12 - 550 ml bottles","UnitPrice": 10,"UnitsInStock": 13,"UnitsOnOrder": 70,"ReorderLevel": 25,"Discontinued": false,"Category": {"CategoryID": 2,"CategoryName": "Condiments","Description": "Sweet and savory sauces, relishes, spreads, and seasonings"}},
  {"selected": true,"id": 3,"ProductName": "Aniseed Syrup","SupplierID": 1,"CategoryID": 2,"QuantityPerUnit": "12 - 550 ml bottles","UnitPrice": 10,"UnitsInStock": 13,"UnitsOnOrder": 70,"ReorderLevel": 25,"Discontinued": false,"Category": {"CategoryID": 2,"CategoryName": "Condiments","Description": "Sweet and savory sauces, relishes, spreads, and seasonings"}}, 
  {"selected": true,"id": 4,"ProductName": "Aniseed Syrup","SupplierID": 1,"CategoryID": 2,"QuantityPerUnit": "12 - 550 ml bottles","UnitPrice": 10,"UnitsInStock": 13,"UnitsOnOrder": 70,"ReorderLevel": 25,"Discontinued": false,"Category": {"CategoryID": 2,"CategoryName": "Condiments","Description": "Sweet and savory sauces, relishes, spreads, and seasonings"}}]

const deduped = data.reduce( (acc, obj ) => {
  // restructure id from each object
  const {"id" : id}  = obj
  if ( acc[id] ) {
    return acc
  } else {
    return { ...acc, 
            [id]: obj
          }
  }
}, {})


重复数据删除中的返回值将通过 ID 进行规范化:

{ 3: 
   { selected: true,
     id: 3,
     ProductName: 'Aniseed Syrup',
     SupplierID: 1,
     CategoryID: 2,
     QuantityPerUnit: '12 - 550 ml bottles',
     UnitPrice: 10,
     UnitsInStock: 13,
     UnitsOnOrder: 70,
     ReorderLevel: 25,
     Discontinued: false,
     Category: 
      { CategoryID: 2,
        CategoryName: 'Condiments',
        Description: 'Sweet and savory sauces, relishes, spreads, and seasonings' } },
  4: 
   { selected: true,
     id: 4,
     ProductName: 'Aniseed Syrup',
     SupplierID: 1,
     CategoryID: 2,
     QuantityPerUnit: '12 - 550 ml bottles',
     UnitPrice: 10,
     UnitsInStock: 13,
     UnitsOnOrder: 70,
     ReorderLevel: 25,
     Discontinued: false,
     Category: 
      { CategoryID: 2,
        CategoryName: 'Condiments',
        Description: 'Sweet and savory sauces, relishes, spreads, and seasonings' } } }

暂无
暂无

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

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