Remove multiple Objects from an array of object

Viewed 409

I have an object, which contains an array of object, and I would to remove some objects from the createValue array.

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. Basically with splice i deleted everything but the "Table" field is in the middle so I do not know how to

8 Answers

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.

You could use delete operator or 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.splice() – Deletes from a specific Array index.
array.shift() – Removes from the beginning of an Array.
array.slice() – Deletes unnecessary items and returns required items.
array.filter() – Allows you to remove elements from an Array programmatically
- appdividend.com

I would recommend using the filter method, it will return a new array given each condition inside it.

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.

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.

  1. Array that needs to be modified (In you case 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.

    Here is the function :

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

    Just overwrite obj2.createValues like this :

     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.

The function filter returns a new array with the elements that meet the condition.

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; }

Related