Skip to content Skip to sidebar Skip to footer

How To Remove Specific Key And Value From Array Object In Javascript?

Here I have arrayData array object in this arrayData I have multiple object I want to remove index and type key values from this array object how to remove from this arrayData ? ar

Solution 1:

You can use rest parameter. which will come handy when you have lot's of keys which you want to keep and removing only few of them.

const arrayData= [{index: 0,is_required: true,name: "vmvdnksl",type: "LONG_TEXT"},{index: 1,is_required: true,name: "dsvnlk",type: "MULTIPLE_SELECTORS"}];

const result = arrayData.map(({type,index,...rest}) => ({...rest}));

console.log(result);

Solution 2:

You can use Array.map() and destructuring for this task:

const arrayData = [
    {
       index: 0,
       is_required: true,
       name: "vmvdnksl",
       type: "LONG_TEXT"
     },
     {
       index: 1,
       is_required: true,
       name: "dsvnlk",
       type: "MULTIPLE_SELECTORS"
     }
];

let res = arrayData.map(({is_required, name}) => ({is_required, name}));
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100%!important; top:0;}

I prefer to never mutate the original data, but in the case you need this, you can do this way (or using delete as others has shown):

const arrayData = [
    {
       index: 0,
       is_required: true,
       name: "vmvdnksl",
       type: "LONG_TEXT"
     },
     {
       index: 1,
       is_required: true,
       name: "dsvnlk",
       type: "MULTIPLE_SELECTORS"
     }
];

let res = arrayData.forEach(
    ({is_required, name}, idx, arr) => arr[idx] = ({is_required, name})
);
console.log(arrayData);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100%!important; top:0;}

Solution 3:

For Array you can use the map() function

var arrayData = [
      {
           index: 0,
           is_required: true,
           name: "vmvdnksl",
           type: "LONG_TEXT"
      },
      {
           index: 1,
           is_required: true,
           name: "dsvnlk",
           type: "MULTIPLE_SELECTORS"
      }
    ],
    mappedArrayData = arrayData.map(({is_required, name}) => {
       return {is_required, name};
    })
    
    console.log(mappedArrayData);

For Object use the delete operator.

var arrayData = {
  0: {
       index: 0,
       is_required: true,
       name: "vmvdnksl",
       type: "LONG_TEXT"
     },
  1: {
       index: 1,
       is_required: true,
       name: "dsvnlk",
       type: "MULTIPLE_SELECTORS"
     }
  };
  
  
for (let key in arrayData) {
  delete arrayData[key].index;
  delete arrayData[key].type;
}
  
console.log(arrayData);

Solution 4:

You can remove properties from objects using the delete keyword.

var arrayData = [
  0: {
       index: 0,
       is_required: true,
       name: "vmvdnksl",
       type: "LONG_TEXT"
     },
  1: {
       index: 1,
       is_required: true,
       name: "dsvnlk",
       type: "MULTIPLE_SELECTORS"
     }
 ];

for (var i in arrayData) {
  for (var j in arrayData[i]) {
    if (j === 'index' || j === 'type') {
      delete arrayData[i][j];
    }
  }
}

console.log(arrayData);

Solution 5:

You can simply use Array.map() to show only required properties:

const arrayData= [
   {
       index: 0,
       is_required: true,
       name: "vmvdnksl",
       type: "LONG_TEXT"
     },
   {
       index: 1,
       is_required: true,
       name: "dsvnlk",
       type: "MULTIPLE_SELECTORS"
     }
   ];

const result = arrayData.map(({is_required, name}) => ({is_required, name}));

console.log(result);

Post a Comment for "How To Remove Specific Key And Value From Array Object In Javascript?"