Remove Property for All Objects in Array

Remove property for all objects in array

The only other ways are cosmetic and are in fact loops.

For example :

array.forEach(function(v){ delete v.bad });

Notes:

  • if you want to be compatible with IE8, you'd need a shim for forEach. As you mention prototype, prototype.js also has a shim.
  • delete is one of the worst "optimization killers". Using it often breaks the performances of your applications. You can't avoid it if you want to really remove a property but you often can either set the property to undefined or just build new objects without the property.

How to remove properties from an object array?

You can delete object property like this e.g.

    var myprop = [
{prop1: 'A', prop2: "1", prop3: "descr1"},
{prop1: 'B', prop2: "2", prop3: "descr2"},
{prop1: 'C', prop2: "3", prop3: "descr3"},
];

myprop = myprop.filter(function (props) {
delete props.prop2;
return true;
});
console.log(myprop);

How to remove a property from array of objects

Iterate over your array and use delete keyword.

let array = [
{ name: "Matthew", age: 23 },
{ name: "James", age: 20 },
{ name: "Daniel", age: 25 },
{ name: "Joshua", age: 22 }
]
array.forEach(function(v){ delete v.age });
console.log(array);

How to remove object property from an array of objects if the value is 0

Array#forEach doesn't return an array.

To achieve your desired output, in each iteration, use Object#entries to get the key-value pairs of the current object, and Array#filter to filter the ones where the value is zero. To construct the resulting object again, use Object#fromEntries:

const data = [ { percent: 123, unit: -1 }, { percent: 456, unit: 0 }, { percent: 0, unit: 5}, { percent: 789, unit: -3 } ];

const newData = data.map(e =>
Object.fromEntries(
Object.entries(e).filter(([key, value]) => value !== 0)
)
);

console.log(newData)

Remove specific properties from Array objects in Node.js

You can use Array.prototype.map() to customise your result array, taking a callback function as parameter which returns a new customised object, having only car and ID properties, in each iteration.

The map() method creates a new array with the results of calling a
provided function on every element in the calling array.

This is how should be your code:

var results = arr.map(function(item){
return {car : item["car"], ID : item["ID"]}
});

Demo:

var arr = [{"car":"Toyota","ID":"1", "Doors": "4", "price": "20.000"},{"car":"Chevrolet","ID":"2", "Doors": "2", "price": "15.000"}];
var results = arr.map(function(item){ return {car : item["car"], ID : item["ID"]}});console.log(JSON.stringify(results));

Remove array element based on object property

One possibility:

myArray = myArray.filter(function( obj ) {
return obj.field !== 'money';
});

Please note that filter creates a new array. Any other variables referring to the original array would not get the filtered data although you update your original variable myArray with the new reference. Use with caution.

remove objects from array by object property

I assume you used splice something like this?

for (var i = 0; i < arrayOfObjects.length; i++) {
var obj = arrayOfObjects[i];

if (listToDelete.indexOf(obj.id) !== -1) {
arrayOfObjects.splice(i, 1);
}
}

All you need to do to fix the bug is decrement i for the next time around, then (and looping backwards is also an option):

for (var i = 0; i < arrayOfObjects.length; i++) {
var obj = arrayOfObjects[i];

if (listToDelete.indexOf(obj.id) !== -1) {
arrayOfObjects.splice(i, 1);
i--;
}
}

To avoid linear-time deletions, you can write array elements you want to keep over the array:

var end = 0;

for (var i = 0; i < arrayOfObjects.length; i++) {
var obj = arrayOfObjects[i];

if (listToDelete.indexOf(obj.id) === -1) {
arrayOfObjects[end++] = obj;
}
}

arrayOfObjects.length = end;

and to avoid linear-time lookups in a modern runtime, you can use a hash set:

const setToDelete = new Set(listToDelete);
let end = 0;

for (let i = 0; i < arrayOfObjects.length; i++) {
const obj = arrayOfObjects[i];

if (setToDelete.has(obj.id)) {
arrayOfObjects[end++] = obj;
}
}

arrayOfObjects.length = end;

which can be wrapped up in a nice function:

const filterInPlace = (array, predicate) => {    let end = 0;
for (let i = 0; i < array.length; i++) { const obj = array[i];
if (predicate(obj)) { array[end++] = obj; } }
array.length = end;};
const toDelete = new Set(['abc', 'efg']);
const arrayOfObjects = [{id: 'abc', name: 'oh'}, {id: 'efg', name: 'em'}, {id: 'hij', name: 'ge'}];
filterInPlace(arrayOfObjects, obj => !toDelete.has(obj.id));console.log(arrayOfObjects);

How to remove property from objects inside array in react state

This is how you can delete particular property for all objects in array.

let array = [{name: "this is name1", title:"This is title 2"},{name:"this is name2", title:"This is title1"}];
let newArray = array.map(function(item) { delete item.name; return item; });console.log(newArray)


Related Topics



Leave a reply



Submit