如何在Lodash中使用数组过滤条件反对对象?

时间:2019-05-31 12:00:19

标签: javascript arrays object ecmascript-6 lodash

如何过滤对象在lodash中的另一个对象?但是条件对象是数组。

var form = { type: ['xyz', 'abc'], name: 'pre-1' }

var items = [
 { type: 'xyz', name: 'pre-1' },
 { type: 'abc', name: 'pre-1' }, 
 { type: 'xyz', name: 'pre-2' }, 
]

const results = lodash.filter(items, form);

results应该是xyzabc

results = [{ type: 'xyz', name: 'pre-1' }, { type: 'abc', name: 'pre-1' }]

我可以使用filter方法来做某事吗?

5 个答案:

答案 0 :(得分:1)

那么,您可以对lodash使用自定义过滤器方法。请尝试以下:

var form = { type: ['xyz', 'abc'], name: 'pre-1' }

var items = [
 { type: 'xyz', name: 'pre-1' },
 { type: 'abc', name: 'pre-1' }, 
 { type: 'xyz', name: 'pre-2' }, 
]

let filtered = _.filter(items, (item)=>{
    return form.type.indexOf(item.type) >= 0 && form.name == item.name
});

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

答案 1 :(得分:0)

var form = { type: ['xyz', 'abc'], name: 'pre-1' }

var items = [
 { type: 'xyz', name: 'pre-1' },
 { type: 'abc', name: 'pre-1' }, 
 { type: 'xyz', name: 'pre-2' }, 
]

var item = _.filter(items, i => {
 return form.type.indexOf(i.type) > -1 && form.name === i.name
});

console.log(item)

答案 2 :(得分:0)

检查是否为form.name === item.name,然后使用Array.includes()检查item.type是否出现在form.type中。

注意:我使用了香草JS过滤器并包含方法,但是您可以用lodash的_.filter()_.includes()代替它们。

const form = { type: ['xyz', 'abc'], name: 'pre-1' }

const items = [{"type":"xyz","name":"pre-1"},{"type":"abc","name":"pre-1"},{"type":"xyz","name":"pre-2"}]

const result = items.filter(item =>
  form.name === item.name && form.type.includes(item.type)
)

console.log(result)

lodash的另一个选项是使用_.intersectionWith(),然后在编译器函数中检查项目名称是否与表单名称相同,并且类型中是否存在该类型。

const form = { type: ['xyz', 'abc'], name: 'pre-1' }

const items = [{"type":"xyz","name":"pre-1"},{"type":"abc","name":"pre-1"},{"type":"xyz","name":"pre-2"}]

const result = _.intersectionWith(items, form.type, (item, type) =>
  form.name === item.name && item.type === type
)

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

答案 3 :(得分:0)

如果您将项目reduce {typename组合在一起的对象映射中找到,则key可以是一个简单的映射有或没有lodash:

var form = { type: ['xyz', 'abc'], name: 'pre-1' }

var items = [
 { type: 'xyz', name: 'pre-1' },
 { type: 'abc', name: 'pre-1' }, 
 { type: 'xyz', name: 'pre-2' }, 
].reduce((r,c) => (r[`${c.type}-${c.name}`] = c, r), {}) // convert to object map

let result = form.type.map(t => items[`${t}-${form.name}`]) // map by key

console.log(result)

使用lodash,您可以使用_.keyBy_.map

var form = { type: ['xyz', 'abc'], name: 'pre-1' }

var items = _.keyBy([
 { type: 'xyz', name: 'pre-1' },
 { type: 'abc', name: 'pre-1' }, 
 { type: 'xyz', name: 'pre-2' }, 
], x => `${x.type}-${x.name}`)

let result = _.map(form.type, t => items[`${t}-${form.name}`])

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

从性能的角度来看,您位于O(N)领域。

答案 4 :(得分:0)

在纯JS中很简单:

var form = { type: ['xyz', 'abc'], name: 'pre-1' }

var items = [
 { type: 'xyz', name: 'pre-1' },
 { type: 'abc', name: 'pre-1' }, 
 { type: 'xyz', name: 'pre-2' }, 
]

const res = items.filter(({ type, name }) => form.type.includes(type) && form.name == name);

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

如果您确实愿意,可以使用Lodash的_.filter方法,如下所示:

var form = { type: ['xyz', 'abc'], name: 'pre-1' }

var items = [
 { type: 'xyz', name: 'pre-1' },
 { type: 'abc', name: 'pre-1' }, 
 { type: 'xyz', name: 'pre-2' }, 
]

const res = _.filter(items, ({ type, name }) => form.type.includes(type) && form.name == name);

console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>