Home > database >  Remove multiple Objects from an array of object
Remove multiple Objects from an array of object

Time:06-22

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

CodePudding user response:

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()

CodePudding user response:

Here is the example to remove first two items from array. I don't know is there a criteria how to select items for deleting?

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"
               },
               ]

        };
        
obj2.createValues.splice(0,2)
console.log(obj2)

CodePudding user response:

There are some ways to do it:

    array.pop() – The pop() method removes from the end of an Array.
    array.splice() – The splice() method deletes from a specific Array index.
    array.shift() – The shift() method removes from the beginning of an Array.
    array.slice() – The slice() method deletes unnecessary items and returns required items.
    array.filter() – allows you to remove elements from an Array programmatically

[source for above information][1]

I would recommend you using filter method, it will return new array according to given condition inside it.

CodePudding user response:

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)

CodePudding user response:

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

CodePudding user response:

Hope this one will help you

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

// return array of object with value = "TEST1"
let filterValue = obj2.createValues.filter(function(em) {
    return em.value === "TEST1"
})

console.log('filterValue  : ', filterValue )

// return 2 value from index = 0
let spliceValue = obj2.createValues.splice(0,2)

console.log('spliceValue : ', spliceValue)

CodePudding user response:

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)
    
  • Related