过滤对象键和值

时间:2019-02-13 08:08:07

标签: javascript performance lodash javascript-objects

我有一个看起来像这样的对象:

var myObj = {
    "account id": "123TRANS",
    "account name": "Optimus Prime",
    "creator id": "Prime",
    "contact tel": 08915243209,
    "house no": 209,
    "is registered": true,
    "price": 398.83,
    "first name": "paul",
    "last name": "jones",
    "dob": "27-Jun-1978 00:00:00",
    "previous orders": true,
    "average price": 123.78,
    "lace colour": "blue",
    "first line": "Jupiter Close",
    "city": "New York",
    "alias": "Jupiter",
    "role": "Accountant"
}

在我的HTML页面上,我有一个<input>字段,用户可以在其中输入文本(至少3个字符)。我将能够根据输入字符串的匹配项来过滤上面的整个对象。

例如,如果string =“ Prime”响应为:

{
    "account name": "Optimus Prime",
    "creator id": "Prime",
}

例如,string =“ acc”响应为:

{
    "account id": "123TRANS",
    "account name": "Optimus Prime",
    "role": "Accountant"
}

例如,字符串=“ 209”响应为:

{
    "contact tel": 08915243209,
    "house no": 209,
}

我尝试使用.reduce,但这会产生不同的结果:

var myObject = {from above};
var filter = 'prime';

myObject = Object.keys(myObject).reduce((r, e) => {
    // Avoid int/float/bool and check key + value
    if(e !== 'contact tel' &&
       e !== 'house no' &&
       e !== 'previous orders') {

       // Check Key or Value for filter
       if(myObject[e].toLowerCase().includes(filter.toLowerCase) || e.toLowerCase().includes(filter.toLowerCase())) {
           r[e] = myObject[e]
       }
    }

    // Check int/float/bool and check key
    if(e === 'contact tel' ||
       e === 'house no' ||
       e === 'previous orders') {

       // Check Key or Value for filter
       if(e.toLowerCase().includes(filter.toLowerCase())) {
           r[e] = myObject[e]
       }
    }

    return r;  

}, {})

我也可以访问lodash,但是找不到能检查键AND值的任何东西,只能检查键OR值。

3 个答案:

答案 0 :(得分:1)

您可以获取条目,过滤键和值并构建一个新对象作为结果。

function find(object, search) {
    search = search.toLowerCase();
    return Object.assign(...Object
        .entries(object)
        .filter(a => a.some(s => s.toString().toLowerCase().includes(search)))
        .map(([k, v]) => ({ [k]: v }))
    );
}

var myObj = { "account id": "123TRANS", "account name": "Optimus Prime", "creator id": "Prime", "contact tel": "08915243209", "house no": 209, "is registered": true, price: 398.83, "first name": "paul", "last name": "jones", dob: "27-Jun-1978 00:00:00", "previous orders": true, "average price": 123.78, "lace colour": "blue", "first line": "Jupiter Close", city: "New York", alias: "Jupiter", role: "Accountant" };

console.log(find(myObj, 'ACC'));

答案 1 :(得分:1)

`

var myObj = {
    "account id": "123TRANS",
    "account name": "Optimus Prime",
    "creator id": "Prime",
    "contact tel": 08915243209,
    "house no": 209,
    "is registered": true,
    "price": 398.83,
    "first name": "paul",
    "last name": "jones",
    "dob": "27-Jun-1978 00:00:00",
    "previous orders": true,
    "average price": 123.78,
    "lace colour": "blue",
    "first line": "Jupiter Close",
    "city": "New York",
    "alias": "Jupiter",
    "role": "Accountant"
},
filterString = 'prime';
Object.entries(myObj)
  .filter((elem) => {
    return (elem.join('').toLowerCase().indexOf(filterString) > -1);
  })
  .reduce((accumulator, [key, value]) => {
    accumulator[key] = value;
    return accumulator;
  }, {});`

答案 2 :(得分:1)

您可以使用_.pickBy()来获取基于包含文本的键或值的子对象:

const findInObj = (obj, str) => {
  const pattern = new RegExp(str, 'i')
  return _.pickBy(obj, (v, k) => pattern.test(v) || pattern.test(k))
}

const myObj = {"account id":"123TRANS","account name":"Optimus Prime","creator id":"Prime","contact tel":8915243209,"house no":209,"is registered":true,"price":398.83,"first name":"paul","last name":"jones","dob":"27-Jun-1978 00:00:00","previous orders":true,"average price":123.78,"lace colour":"blue","first line":"Jupiter Close","city":"New York","alias":"Jupiter","role":"Accountant"}

const result = findInObj(myObj, 'acc')

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>