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

arrays - How to filter records based on the status value in JavaScript object? - Stack Overflow

programmeradmin1浏览0评论

I have JS object that looks like this:

{
  "3": {
    "id": 3,
    "first": "Lisa",
    "last": "Morgan",
    "email": "[email protected]",
    "phone": "(508) 233-8908",
    "status": 0
  },
  "4": {
    "id": 4,
    "first": "Dave",
    "last": "Hart",
    "email": "[email protected]",
    "phone": "(509) 874-9411",
    "status": 1
  }
}

I would like to filter through the object and for example pull only record which status is '1'. One solution for that is to use array filter like this:

var filterJSON = Object.values(obj).filter(function (entry) {
                switch(frmFilter){
                    case '1':
                        return entry.status === 1;
                        break;
                    case '2':
                        return entry.status === 0;
                        break;
                    default:
                        return entry;
                }
            });

problem is code above will convert data into array like this:

[
  {
    "id": 3,
    "first": "Lisa",
    "last": "Morgan",
    "email": "[email protected]",
    "phone": "(508) 233-8908",
    "status": 1
  },
  {
    "id": 4,
    "first": "Dave",
    "last": "Hart",
    "email": "[email protected]",
    "phone": "(509) 874-9411",
    "status": 1
  }
]

As you can see data set is an array, I want to keep my data in object same as one in the first example before filter was applied. Is there a way to filter through the object and achieve the same output as one with filtering through array?

I have JS object that looks like this:

{
  "3": {
    "id": 3,
    "first": "Lisa",
    "last": "Morgan",
    "email": "[email protected]",
    "phone": "(508) 233-8908",
    "status": 0
  },
  "4": {
    "id": 4,
    "first": "Dave",
    "last": "Hart",
    "email": "[email protected]",
    "phone": "(509) 874-9411",
    "status": 1
  }
}

I would like to filter through the object and for example pull only record which status is '1'. One solution for that is to use array filter like this:

var filterJSON = Object.values(obj).filter(function (entry) {
                switch(frmFilter){
                    case '1':
                        return entry.status === 1;
                        break;
                    case '2':
                        return entry.status === 0;
                        break;
                    default:
                        return entry;
                }
            });

problem is code above will convert data into array like this:

[
  {
    "id": 3,
    "first": "Lisa",
    "last": "Morgan",
    "email": "[email protected]",
    "phone": "(508) 233-8908",
    "status": 1
  },
  {
    "id": 4,
    "first": "Dave",
    "last": "Hart",
    "email": "[email protected]",
    "phone": "(509) 874-9411",
    "status": 1
  }
]

As you can see data set is an array, I want to keep my data in object same as one in the first example before filter was applied. Is there a way to filter through the object and achieve the same output as one with filtering through array?

Share Improve this question asked Aug 1, 2018 at 2:37 espresso_coffeeespresso_coffee 6,12012 gold badges96 silver badges220 bronze badges 2
  • Will for...in loop help? – samuellawrentz Commented Aug 1, 2018 at 2:42
  • @samuellawrentz Maybe, I'm not sure what is your suggestion. – espresso_coffee Commented Aug 1, 2018 at 2:43
Add a ment  | 

5 Answers 5

Reset to default 1

You can use .filter() with .reduce() to convert the filtered array back into an object:

var obj = {
  "3": {
    "id": 3,
    "first": "Lisa",
    "last": "Morgan",
    "email": "[email protected]",
    "phone": "(508) 233-8908",
    "status": 0
  },
  "4": {
    "id": 4,
    "first": "Dave",
    "last": "Hart",
    "email": "[email protected]",
    "phone": "(509) 874-9411",
    "status": 1
  }
}

var frmFilter = "1";

var filterJSON = Object.keys(obj).filter(function (key) {
    let entry = obj[key];
    switch(frmFilter){
        case '1':
            return entry.status === 1;
            break;
        case '2':
            return entry.status === 0;
            break;
        default:
            return entry;
    }
}).reduce( (res, key) => (res[key] = obj[key], res), {} );

console.log(filterJSON);

Had some help from this answer: JavaScript: filter() for Objects

Try this:

const data = {
  "3": {
    "id": 3,
    "first": "Lisa",
    "last": "Morgan",
    "email": "[email protected]",
    "phone": "(508) 233-8908",
    "status": 0
  },
  "4": {
    "id": 4,
    "first": "Dave",
    "last": "Hart",
    "email": "[email protected]",
    "phone": "(509) 874-9411",
    "status": 1
  }
};

const filtered = Object.values(data).filter(e => e.status == 1);
const output = {};
filtered.forEach(e => output[e.id] = e);
console.log(output);

If you are using the filter Array function, it returns a simple array, not an object like the original (obj)

You can do a simple for-each copying and modifying object properties in another object:

var filterJSON = {};
for (var key in obj) {
  if (obj.hasOwnProperty(key)){

        switch(frmFilter){
                case '1':
                    if (filterJSON[key].status == 1){
                       filterJSON[key] = obj[key];
                    } 
                    break;
                case '2':
                    if (filterJSON[key].status == 0){
                       filterJSON[key] = obj[key];
                    }
                    break;
         }
   }
}

This is what you are looking for:

const filter = (data, predicate) => {
  return Object.keys(data).reduce((acc, current) => {
    if (predicate(data[current])) {
      acc[current] = data[current];
    }
    return acc;
  }, {});
}

// result contains only items with status of 1
const result = filter(data, (item) => item.status === 1);

// result2 contains only items with last name of length > 4
const result2 = filter(data, (item) => item.last.length > 4);

The predicate is called on each item and if it returns true the item is included in the result.

I was telling the for...in loop in JS that is used to iterate over the properties of an object.

https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/for...in

var obj = {
  "3": {
    "id": 3,
    "first": "Lisa",
    "last": "Morgan",
    "email": "[email protected]",
    "phone": "(508) 233-8908",
    "status": 0
  },
  "4": {
    "id": 4,
    "first": "Dave",
    "last": "Hart",
    "email": "[email protected]",
    "phone": "(509) 874-9411",
    "status": 1
  }
}
var filteredObj = {};
for (var props in obj){
  if(obj[props].status===1)
  filteredObj[props] = obj[props] ; 
  }
  
  console.log(filteredObj)

You are iterating over properties of obj and checking for status. If status equals 1 we are assigning that property to the filteredObj Object.

发布评论

评论列表(0)

  1. 暂无评论