【问题标题】:Fuzzy search deep array only on certain properties仅对某些属性进行模糊搜索深度数组
【发布时间】:2016-10-25 20:00:01
【问题描述】:

我有一个 JSON 数据集,它在返回时可能非常大,每个对象的结构如下:

{
    "ctr": 57,
    "averageECPC": 23,
    "cost": 2732.54,
    "margin": 66,
    "profit": 2495.9,
    "property": {
        "value": "Izzby",
        "uri": "/Terrago/2"
    },
    "status": {
        "content": "<p>Some Content</p>",
        "stage": 1
    },
    "alerts": {
        "status": 2
    },
    "revenue": {
        "value": 2573.13,
        "compare": 0
    },
    "children": [{
        "ctr": 79,
        "averageECPC": 54,
        "cost": 3554.78,
        "margin": 88,
        "profit": 3145.81,
        "property": {
            "value": "Comvex",
            "uri": "/Octocore/4"
        },
        "status": {
            "content": "<p>Some Content</p>",
            "stage": 1
        },
        "alerts": {
            "status": 2
        },
        "revenue": {
            "value": 1247.92,
            "compare": 0
        }
    }]
}

现在我想搜索数组中的所有对象并只返回包含某种字符串的对象,但我只想搜索某些属性。

我基本上有另一个数组,其中包含我要搜索的键,例如

const iteratees = ['ctr', 'property.value', 'status.stage']

我在项目中有可用的 lodash,但我不知道从哪里开始。

有什么想法吗?

【问题讨论】:

  • which include a string of some sort你能澄清一下吗?
  • 这只是我要搜索的字符串。例如,我可能想搜索字符串“izz”,它应该返回 property.value 设置为“Izzby”的对象
  • 因此,iteratees 中的一些键应该有一些您想要搜索的值并且您想要返回这些对象。
  • 完全正确。但只搜索 iteratees 数组中的关键字,没有其他关键字。
  • 先把结构扁平化到一层可以吗?使用 parentId 引用每个项目的父项。

标签: javascript lodash


【解决方案1】:

您可以使用filter()some()reduce() 来执行此操作。

const iteratees = ['ctr', 'property.value', 'status.stage'];
var searchFor = 'lo';

var result = arr.filter(function(o) {
  return iteratees.some(function(e) {
    var res = e.split('.').reduce(function(a, b) {
      if(a) return a[b];
    }, o);
    if(res) {
      if((res).toString().indexOf(searchFor) != -1) return true;
    }
  })
})

var arr = [{
  "ctr": 'lorem',
  "averageECPC": 23,
  "cost": 2732.54,
  "margin": 66,
  "profit": 2495.9,
  "property": {
    "value": "Izzby",
    "uri": "/Terrago/2"
  },
  "status": {
    "content": "<p>Some Content</p>",
    "stage": 1
  },
  "alerts": {
    "status": 2
  },
  "revenue": {
    "value": 2573.13,
    "compare": 0
  },
  "children": [{
    "ctr": 79,
    "averageECPC": 54,
    "cost": 3554.78,
    "margin": 88,
    "profit": 3145.81,
    "property": {
      "value": "Comvex",
      "uri": "/Octocore/4"
    },
    "status": {
      "content": "<p>Some Content</p>",
      "stage": 1
    },
    "alerts": {
      "status": 2
    },
    "revenue": {
      "value": 1247.92,
      "compare": 0
    }
  }]
}, {
  name: 'lorem',
  ctr: 12,
  property: {
    value: 1
  },
  status: {
    stage: 1
  }
}, {
  name: 'ipsum'
}]

const iteratees = ['ctr', 'property.value', 'status.stage'];
var searchFor = 'lo';

var result = arr.filter(function(o) {
  return iteratees.some(function(e) {
    var res = e.split('.').reduce(function(a, b) {
      if (a) return a[b];
    }, o);
    if (res) {
      if ((res).toString().indexOf(searchFor) != -1) return true;
    }
  })
})

console.log(result)

【讨论】:

  • 我应该提到,searchFor 字符串来自用户通过文本输入输入的内容,而上面的内容在一个只有 100 个对象的数组中看起来非常慢。以上将被称为输入的onChange
猜你喜欢
  • 2014-06-04
  • 2013-02-18
  • 1970-01-01
  • 2022-03-17
  • 2023-01-27
  • 2021-11-14
  • 2017-05-24
  • 1970-01-01
  • 2013-05-23
相关资源
最近更新 更多