Reputation:
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"
}
]
Upvotes: 10
Views: 27584
Reputation: 453
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);
Upvotes: 1
Reputation: 455
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
}
Upvotes: 0
Reputation: 37775
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);
Upvotes: 34
Reputation: 109
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);
Upvotes: 0
Reputation: 3337
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);
Upvotes: 1
Reputation: 17190
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;}
Upvotes: 5