7

I have the following collection of data

[{
 id: '1',
 date: '2017-01-01',
 value: 2
 },
 {
 id: '2',
 date: '2017-01-02',
 value: 3
 },
 {
 id: '3',
 value: 3
 },
 id: '4',
 date: '2017-01-02',
 value: 3
 }]

I want to delete any object that does not have the 'date' property. In the example above, the object with the id 3 should be deleted.

The finished object should look like this

[{
 id: '1',
 date: '2017-01-01',
 value: 2
 },
 {
 id: '2',
 date: '2017-01-02',
 value: 3
 },
 id: '4',
 date: '2017-01-02',
 value: 3
 }]

I tried to find and delete a undefined value with lodash. It does not work. The object looks exactly as it is entering.

  _.each(obj, (val) => {
    _.remove(val, value => value['XXX-BUDAT'] === undefined);
   });

How can I use lodash for this purpose?

Thanks in advance

0

6 Answers 6

5

You can use .filter(), Object.keys(), and .includes()

let input = [
   { id: '1', date: '2017-01-01', value: 2},
   { id: '2', date: '2017-01-02', value: 3},
   { id: '3', value: 3 },
   { id: '4', date: '2017-01-02', value: 3 }
]
 
 let output = input.filter(obj => Object.keys(obj).includes("date"));
 
 console.log(output);

3
  • ah ok. Instead of removing you create and object with keys that includes date. Nice thank you mickl
    – Ckappo
    Jul 16, 2018 at 18:00
  • @Ckappo If this was useful to you, please upvote this question for future readers and consider accepting it if you think it is the best one for your question. Jul 16, 2018 at 18:22
  • @mickl It might make more sense to use Object.hasOwnProperty, wouldnt it? That way you dont have to generate + iterate the object key collection.
    – Marie
    Jul 19, 2018 at 13:56
4

You can filter the array based on that property like this:

const initial = [{
    id: '1',
    date: '2017-01-01',
    value: 2
  },
  {
    id: '2',
    date: '2017-01-02',
    value: 3
  },
  {
    id: '3',
    value: 3
  }, { // this left curly brace was missing!, check that out
    id: '4',
    date: '2017-01-02',
    value: 3
  }
];

const finalR = initial.filter((obj) => obj.hasOwnProperty('date') && !!obj.date);

console.log(finalR);

2
  • Note that this may cause problems as it looks up the prototype chain too. Jul 16, 2018 at 17:59
  • I don't know why my previous comment was deleted.... anyway. The answer is updated after the comment of ibrahim mahrir Jul 17, 2018 at 13:48
3

You can use Array#filter and Object#hasOwnProperty to do so:

var newArray = oldArray.filter(function(obj) {
    return obj.hasOwnProperty("date");
});

Which can be shortened out using an arrow function:

var newArray = oldArray.filter(obj => obj.hasOwnProperty("date"));

Lodash solution:

var newArray = _.filter(oldArray, obj => _.has(obj, "date"));

Example:

var oldArray = [{id: '1', date: '2017-01-01', value: 2 }, { id: '2', date: '2017-01-02', value: 3 }, { id: '3', value: 3 }, {id: '4', date: '2017-01-02', value: 3}];

var newArray = oldArray.filter(obj => obj.hasOwnProperty("date"));

console.log(newArray);

1

You can use Array.prototype.filter. In addition, you can use ES6 object assignment destructiring to make it concise:

var data=[{id:'1',date:'2017-01-01',value:2},{id:'2',date:'2017-01-02',value:3},{id:'3',value:3},{id:'4',date:'2017-01-02',value:3}];

var result = data.filter(({date}) => date);

console.log(result)
0

First of all your array of object is not valid, fix it by wrapping the last object element with preceding curly brace {. See Array.prototype.filter() and Object.prototype.hasOwnProperty() that'll check your current object item has the date key or not? if it has then it'll return that object other wise that item will not return.

var array_of_object = [{
    id: '1',
    date: '2017-01-01',
    value: 2
  },
  {
    id: '2',
    date: '2017-01-02',
    value: 3
  },
  {
    id: '3',
    value: 3
  },
  { //missing brace here
    id: '4',
    date: '2017-01-02',
    value: 3
  }
];


function filterByValue(item) {
  return item.hasOwnProperty('date');
}
var arrByID = array_of_object.filter(filterByValue);
console.log(arrByID);

0

This is a native javascript solution:

var arr = [
  {id: '1', date: '2017-01-01', value: 2},
  {id: '2', date: '2017-01-02', value: 3},
  {id: '3', value: 3},
  {id: '4', date: '2017-01-02', value: 3}
]

for(var i = 0; i < arr.length; i++) {
  if(!arr[i].hasOwnProperty("date")) {
    arr.splice(i,1);
  }
}

console.log(arr);

1
  • Note that this may cause problems as it looks up the prototype chain too. See this comment; I omitted this as well in my first attempt to answer the question :) A solution could be to use the hasOwnProperty func. Jul 16, 2018 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.