7

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 ?

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

after removiing index and type I want this type result

 arrayData : [
  0: {
       is_required: true
       name: "vmvdnksl"
     }
  1: {
       is_required: true
       name: "dsvnlk"
     }
   ]
  • 1
    Possible duplicate of [How can I completely remove an object from an array in Javascript?](https://stackoverflow.com/questions/18158846/how-can-i-completely-remove-an-object-from-an-array-in-javascript) – Nithya Rajan Mar 06 '19 at 05:19
  • Please, update with your expected result. I'm assuming you want the same structure but not showing `index` and `type`, is that correct? – Shidersz Mar 06 '19 at 05:21
  • @Shidersz i update my question –  Mar 06 '19 at 05:23
  • Ok, one more thing, is `arrayData` an array or an object? – Shidersz Mar 06 '19 at 05:24
  • @Shidersz arrayData is array –  Mar 06 '19 at 05:27

6 Answers6

28

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);
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • what if there is array of keys and not separate keys . how to write that I map ?? – minigeek Apr 01 '21 at 06:14
  • @minigeek can you please post an example of what you're stating, btw you can destructure array as well ref https://stackoverflow.com/questions/54605286/what-is-destructuring-assignment-and-its-uses – Code Maniac Apr 01 '21 at 10:58
  • yes I tried ..not able to put up proper logic. basically instead of type, index as known keys. i have array of strings which has those keys ['type', 'index'] .. I am passing it as input to my component in angular. your answer is best way to do if we know keys but for now I have to do with delete and filter combination – minigeek Apr 01 '21 at 11:01
4

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;}
Shidersz
  • 16,846
  • 2
  • 23
  • 48
  • 1
    is there any possiblity to remove using type and index using index and type not is_required or name? –  Mar 06 '19 at 06:12
  • 1
    @Akki Check the answer given by **Code Maniac** or use `delete` as other answer has shown for what you ask on the first comment. **tip:** to get votes on your question, next time you should provide what you have tried so far. – Shidersz Mar 06 '19 at 15:09
1

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);
henser
  • 3,307
  • 2
  • 36
  • 47
Vappor Washmade
  • 423
  • 4
  • 14
1

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);
henser
  • 3,307
  • 2
  • 36
  • 47
0

Try using JavaScript's delete operator, as shown in this code example:

for(let o in arrayData){
    delete arrayData[o].index;
    delete arrayData[o].type
}
Clomp
  • 3,168
  • 2
  • 23
  • 36
Eftakhar
  • 455
  • 4
  • 17
0

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);
Khyati Sharma
  • 109
  • 1
  • 9
  • is there any possiblity to remove using type and index using index and type not is_required or name? –  Mar 06 '19 at 06:13
  • This is rare, the last answer, clearly showing the same code that much of the others answers already shown, get more upvotes? – Shidersz Mar 06 '19 at 15:14