Home > Software design >  remove line items by property, not values, in an object
remove line items by property, not values, in an object

Time:05-24

I have an object like this:

var obj = [
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 },
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 },
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 }
]

I want obj to now represent this:

var obj = [
 {
        item1: 'test',
    item2: 'something',
 },
 {
        item1: 'test',
    item2: 'something',
 },
 {
        item1: 'test',
    item2: 'something',
 }
]

I found a lot of answers about object manipulation and using keys etc

JavaScript: filter() for Objects

How to filter an object of objects efficiently?

How to filter an object with its values in ES6

The problem with these answers is

  1. its a specific object format, and they don't cover how to do multiple objects

  2. It removes objects completely, not value and keys/line items

The closest I have it is this:

var obj = [
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 },
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 },
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 }
]

var newObj = [];

obj.forEach(function(el){
    newObj.push(el.item1);
  newObj.push(el.item2);
})

console.log(newObj)

Obviously this isn't quite it because I need to push the full object not just the key values.

I'm trying something like this How to filter an object with its values in ES6 but this answer won't work because it's doing by value and not by key(?)

var obj = [
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 },
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 },
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 }
]

var acceptedProps = ['item1', 'item2'];

var newObj = Object.keys(obj).reduce(function(r, e) {
  if (acceptedProps.includes(obj[e])) r[e] = obj[e]
  return r;
}, {})

console.log(newObj)

It's just giving me an empty array. I tried flipping some stuff around as well

var obj = [
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 },
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 },
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 }
]

var acceptedProps = ['item1', 'item2'];

var newObj = Object.keys(obj).reduce(function(r, e) {
  if (acceptedProps.includes(obj[r])) r[e] = obj[r]
  return r;
}, {})

console.log(newObj)

idk what r and e means in this function?

How do you filter objects by property and not value, for an array of multiple objects?

EDIT - okay I just found this answer:

Remove array element based on object property

var obj = [
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 },
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 },
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 }
]

var newObj = obj.filter(function( obj ) {
    return obj.field !== 'item3';
});

console.log(newObj)

So now...I have to somehow change it to a lop(?) and update each object without the line item...

var obj = [
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 },
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 },
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 }
]

var obj = [
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 },
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 },
 {
        item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
 }
]

var newObj = obj.forEach(function(el) {
  el.filter(function( el ) {
    return el.field !== 'item3';
  });
})

console.log(newObj)

but I get:

el.filter is not a function ...

How do I do this? what am I doing wrong?

CodePudding user response:

You can use Array.map() to iterate over the array items to create new array, Object.entries() to filter out the values of the objects that are the items of the array, and use Object.fromEntries() to build object back from the filtered entries.

var obj = [{
    item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
  },
  {
    item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
  },
  {
    item1: 'test',
    item2: 'something',
    item3: 'REMOVE THIS',
    item4: 'REMOVE THIS',
    item5: 'REMOVE THIS',
  }
];

var newObj = obj.map(item => Object.fromEntries(
  Object.entries(item)
  .filter(([key, value]) => value !== 'REMOVE THIS')
));

console.log(newObj);

CodePudding user response:

You can try something like that

obj.map((item) => {
  return {
    item1: item.item1,
    item2: item.item2,
  }
});
  • Related