最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to remove object from array if property in object do not exist - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question asked Jul 16, 2018 at 17:52 CkappoCkappo 5972 gold badges10 silver badges27 bronze badges 0
Add a comment  | 

6 Answers 6

Reset to default 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);

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);

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);

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)

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);

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);

发布评论

评论列表(0)

  1. 暂无评论