简体   繁体   English

从对象数组中删除多个对象

[英]Remove multiple Objects from an array of object

I have an object, which contains an array of object, and I would to remove some objects from the createValue array.我有一个对象,其中包含一个对象数组,我想从 createValue 数组中删除一些对象。

I would like to be able to delete multiple objects at the same time.我希望能够同时删除多个对象。 I tried with splice but when I delete it is written undefined.我尝试了拼接,但是当我删除它时,它被写成未定义的。

here is my code :这是我的代码:

    let obj2 = {
                projectId: 0,
                gridId: 0, 
                createValues : [
                {
                 "field": "ID",
                 "value": "40212"
                },
                {
                "field": "FLD_STR_101",
                "value": "TEST1"
                },
                {
                "field": "FLD_STR_101",
                "value": "TEST1"
                },
                {
                "field": "FLD_STR_101",
                "value": "TEST1"
                },
                {
                "field": "Table",
                "value": "TEST1"
                },
                {
                "field": "FLD_STR_101",
                "value": "TEST1"
                },
                {
                "field": "log",
                "value": "TEST1"
                },
               {
                "field": "crea",
                "value": "TEST1"
               },
               {
                "field": "off",
                "value": "TEST1"
                },
               ]

        };


       obj2.createValues.splice(0,2)
       obj2.createValues.splice(4,4)

I would like to delete the off, crea, log, table and id.我想删除off、crea、log、table和id。 Basically with splice i deleted everything but the "Table" field is in the middle so I do not know how to基本上通过拼接我删除了所有内容,但“表格”字段位于中间,所以我不知道如何

The splice() method returns the removed item(s) in an array and slice() method returns the selected element(s) in an array, as a new array object. splice() 方法在数组中返回已删除的项目,而 slice() 方法将数组中的选定元素作为新的数组对象返回。

You could use delete operator or filter()您可以使用删除运算符filter()

Here is one way to do it using loops.这是使用循环的一种方法。

 let obj2 = { projectId: 0, gridId: 0, createValues : [ { "field": "ID", "value": "40212" }, { "field": "FLD_STR_101", "value": "TEST1" }, { "field": "FLD_STR_101", "value": "TEST1" }, { "field": "FLD_STR_101", "value": "TEST1" }, { "field": "Table", "value": "TEST1" }, { "field": "FLD_STR_101", "value": "TEST1" }, { "field": "log", "value": "TEST1" }, { "field": "crea", "value": "TEST1" }, { "field": "off", "value": "TEST1" }, ] }; let array = [] obj2.createValues.forEach((value, index) => { if(value.field !== "ID" && value.field !== "log" && value.field !== "off" && value.field !== "crea" && value.field !== "Table") { array.push(value) } }) obj2.createValues = array console.log(obj2)

There are some ways to do it:有一些方法可以做到:

array.pop() – Removes from the end of an Array. array.pop() - 从数组的末尾删除。
array.splice() – Deletes from a specific Array index. array.splice() - 从特定数组索引中删除。
array.shift() – Removes from the beginning of an Array. array.shift() – 从数组的开头删除。
array.slice() – Deletes unnecessary items and returns required items. array.slice() - 删除不需要的项目并返回需要的项目。
array.filter() – Allows you to remove elements from an Array programmatically array.filter() - 允许您以编程方式从数组中删除元素
- appdividend.com -appdividend.com

I would recommend using the filter method, it will return a new array given each condition inside it.我建议使用 filter 方法,它会根据其中的每个条件返回一个新数组。

Here I am removing duplicate objects according to value, you can edit this code to delete multiple objects according to your needs.这里我根据值删除重复的对象,您可以根据需要编辑此代码以删除多个对象。

let newArray = [];
let removeDuplicate = [];
obj2.createValues.forEach(ele => {
   if (!removeDuplicate.includes(ele.value)) {
      removeDuplicate.push(ele.value);
      newArray.push(ele);
   }
})

const newObj = {...obj2, createValues: newArray}
console.log(newObj)

Note: There was a missing closing array tag ] in the question code, after the last element.注意:问题代码中最后一个元素之后缺少结束数组标记 ]。

I'm not sure what have you tried with Splice if it was returning undefined , but here is a working solution of creating a new array, with removing elements on first 2 index values.如果 Splice 返回undefined ,我不确定您对Splice进行了哪些尝试,但这是一个创建新数组的有效解决方案,删除前 2 个索引值上的元素。

 let obj2 = { projectId: 0, gridId: 0, createValues : [ { "field": "ID", "value": "40212" }, { "field": "FLD_STR_101", "value": "TEST1" }, { "field": "Table", "value": "TEST1" }, { "field": "hello", "value": "TEST1" } ] }; const newObj2 = obj2.createValues.splice(2);// will delete first 2 elements console.log(newObj2);

EDIT : Hope this one will help you编辑:希望这个对你有帮助

 let obj2 = { projectId: 0, gridId: 0, createValues : [ { "field": "ID", "value": "40212" }, { "field": "FLD_STR_101", "value": "TEST1" }, { "field": "FLD_STR_101", "value": "TEST1" }, { "field": "FLD_STR_101", "value": "TEST1" }, { "field": "Table", "value": "TEST1" }, { "field": "FLD_STR_101", "value": "TEST1" }, { "field": "log", "value": "TEST1" }, { "field": "crea", "value": "TEST1" }, { "field": "off", "value": "TEST1" } ] }; let filterValue = obj2.createValues.filter(function(em) { return (em.field !== "ID" && em.field !== "Table" && em.field !== "log" && em.field !== "crea" && em.field !== "off") }) console.log('filterValue : ', filterValue )

You can do this using filter, I have created function called "removeFieldFromArray" which will accept two parametes.您可以使用过滤器来做到这一点,我创建了一个名为“removeFieldFromArray”的函数,它将接受两个参数。

  1. Array that needs to be modified (In you case obj2.createValues)需要修改的数组(在您的情况下为 obj2.createValues)

  2. Array that will contain fields that you want to remove In your case : ['off', 'crea', 'log', 'table', 'ID'] Function will return modified array that will not have fields you have passed in second parameter.将包含您要删除的字段的数组在您的情况下: ['off', 'crea', 'log', 'table', 'ID'] 函数将返回修改后的数组,其中不包含您第二次传递的字段范围。

    Here is the function :这是功能:

     function removeFieldFromArray(array, fields) { return array.filter(r => !fields.includes(r.field)) }

    Just overwrite obj2.createValues like this :只需像这样覆盖 obj2.createValues :

     obj2.createValues = removeFieldFromArray(obj2.createValues, ['off', 'crea', 'log', 'table', 'ID']) console.log(obj2)

You can use the function Array.prototype.filter to filter out those elements that does not meet a condition.您可以使用函数Array.prototype.filter过滤掉那些不满足条件的元素。

The function filter returns a new array with the elements that meet the condition.函数filter返回一个包含满足条件的元素的新数组。

 const obj = { projectId: 0, gridId: 0, createValues : [ { "field": "ID", "value": "40212" }, { "field": "FLD_STR_101", "value": "TEST1" }, { "field": "FLD_STR_101", "value": "TEST1" }, { "field": "FLD_STR_101", "value": "TEST1" }, { "field": "Table", "value": "TEST1" }, { "field": "FLD_STR_101", "value": "TEST1" }, { "field": "log", "value": "TEST1" }, { "field": "crea", "value": "TEST1" }, { "field": "off", "value": "TEST1" }, ] }, targetValues = ["log", "off", "crea", "ID", "Table"], key = "field", filterHandler = o => !targetValues.includes(o[key]); obj.createValues = obj.createValues.filter(filterHandler); console.log(obj);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

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

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