如何使用lodash过滤数据

时间:2019-05-23 07:03:09

标签: javascript arrays object ecmascript-6 lodash

我的代码如下:

data = [
  {id: "15130", state: "INITIAL"},
  {id: "15129", state: "LOCKED"},
  {id: "10314", state: "APPROVED"},
  {id: "51", state: "APPROVED"},
  {id: "10313", state: "APPROVED_LOCKED"},
  {id: "10315", state: "APPROVED_LOCKED"}
]

filters = [{id: "2", name: "LOCKED", count: 2}]

let result = []

_.forEach(data, (ca) => {  
  if (filters.length > 0) {
    if (!_.some(filters, (item) => item.name === ca.state.includes('LOCKED') ? "LOCKED" : ca.state))
      return;
  }
  result.push(ca);
});

console.log(result)

我想从状态包括"LOCKED"的数据中获取所有记录。因此结果应该是:

result = [
  {id: "15129", state: "LOCKED"},
  {id: "10313", state: "APPROVED_LOCKED"},
  {id: "10315", state: "APPROVED_LOCKED"}
]

但是我得到了所有记录。

Here is the fiddle.

知道我做错了什么吗?

5 个答案:

答案 0 :(得分:2)

不需要Lodash,可以使用Vanilla JavaScript的Array.filter(),然后使用Array.includes(),它检查state中的字符串值是否包含字符串'LOCKED'。

const data = [
  {id: "15130", state: "INITIAL"},
  {id: "15129", state: "LOCKED"},
  {id: "10314", state: "APPROVED"},
  {id: "51", state: "APPROVED"},
  {id: "10313", state: "APPROVED_LOCKED"},
  {id: "10315", state: "APPROVED_LOCKED"}
]

const res = data.filter(obj => obj['state'].includes('LOCKED'));

console.log(res);

但是,请注意Array.includes()不是widely supported

答案 1 :(得分:0)

您可以通过检查过滤器数组的每个name来过滤该数组。

var data = [{ id: "15130", state: "INITIAL" }, { id: "15129", state: "LOCKED" }, { id: "10314", state: "APPROVED" }, { id: "51", state: "APPROVED" }, { id: "10313", state: "APPROVED_LOCKED" }, { id: "10315", state: "APPROVED_LOCKED" }],
    filters = [{ id: "2", name: "LOCKED", count: 2 }, { id: "1", name: "APPROVED", count: 2 }],
    result = data.filter(({ state }) => filters.some(({ name }) => state.includes(name)));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:0)

let list = [];
let zoom = 2;

for(let i=1; i<=100; i++) {
  list[i]=i;
}

function getZoomedList(zoom, list, minZoom, maxZoom) {
  startI = list.length/(maxZoom*2) * zoom;
  endI = list.length - startI + 1;
  return list.slice(startI, endI);
}

console.log(getZoomedList(2, list, 0, 5));

您可以使用状态允许的数组,并使用该状态过滤数据。尝试上面的代码。

答案 3 :(得分:0)

如何使用lodash

由于不需要修改原始数组,因此可以使用_.filter。必需的参数是输入数组和谓词,它们将用于过滤提供的数组。

    const data = [
      {id: "15130", state: "INITIAL"},
      {id: "15129", state: "LOCKED"},
      {id: "10314", state: "APPROVED"},
      {id: "51", state: "APPROVED"},
      {id: "10313", state: "APPROVED_LOCKED"},
      {id: "10315", state: "APPROVED_LOCKED"}
    ]

    const result = _.filter(data, (elem => {return elem.state.includes("LOCKED")}));

    console.log(result);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>

如果要修改原始数组,可以选择_.remove

答案 4 :(得分:0)

您可以使用纯JS:

 

const data = [
  {id: "15130", state: "INITIAL"},
  {id: "15129", state: "LOCKED"},
  {id: "10314", state: "APPROVED"},
  {id: "51", state: "APPROVED"},
  {id: "10313", state: "APPROVED_LOCKED"},
  {id: "10315", state: "APPROVED_LOCKED"}
];

const res = data.filter(({ state }) => state.includes("LOCKED"));

console.log(res);
 

.as-console-wrapper { max-height: 100% !important; top: auto; }