[英]Lodash to find if object property exists in array
我有一个这样的对象数组:
[ {"name": "apple", "id": "apple_0"},
{"name": "dog", "id": "dog_1"},
{"name": "cat", "id": "cat_2"}
]
我想插入另一个名为apple
元素,但是,因为我不想在那里重复,我怎样才能使用lodash来查看数组中是否有一个具有相同名称的对象?
你可以像这样使用Lodash _.find()
。
var data = [ {"name": "apple", "id": "apple_0"}, {"name": "dog", "id": "dog_1"}, {"name": "cat", "id": "cat_2"} ] if(!_.find(data, {name: 'apple'})) { data.push({name: 'apple2'}); } console.log(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
参考文档: https : //lodash.com/docs/4.17.14#find
这是表格
_.has(object, path)
例:
const countries = { country: { name: 'Venezuela' } }
const isExist = _.has(countries, 'country.name')
// isExist = true
有关更多信息,请参阅Lodash文档
这是lodash的另一个例子
var a = [ {"name": "apple", "id": "apple_0"},
{"name": "dog", "id": "dog_1"},
{"name": "cat", "id": "cat_2"}
]
var b = _.find(a, ['name', "apple2"]);
if(_.isObject(b)){
console.log('exists')
}else{
console.log('insert new')
}
您可以使用Array.prototype.find()
或lodash的_.find()
:
const addItem = (arr, item) => { if(!arr.find((x) => x.name === item.name)) { // you can also change `name` to `id` arr.push(item); } }; const arr = [ {"name": "apple", "id": "apple_0"}, {"name": "dog", "id": "dog_1"}, {"name": "cat", "id": "cat_2"} ]; addItem(arr, { "name": "apple", "id": "apple_0" }); addItem(arr, { "name": "pear", "id": "pear_3" }); console.log(arr);
而且更短但可读性更低:
const addItem = (arr, item) => arr.find((x) => x.name === item.name) || arr.push(item); // you can also change `name` to `id` const arr = [ {"name": "apple", "id": "apple_0"}, {"name": "dog", "id": "dog_1"}, {"name": "cat", "id": "cat_2"} ]; addItem(arr, { "name": "apple", "id": "apple_0" }); addItem(arr, { "name": "pear", "id": "pear_3" }); console.log(arr);
这对我有用(在测试出不同的解决方案后):
addItem(items, item) {
let foundObject = _.find(items, function(e) {
return e.value === item.value;
});
if(!foundObject) {
items.push(item);
}
return items;
}
以下是使用lodash
4.17.5
实现此目的的三种方法:
说你要添加对象entry
到对象的数组numbers
,只有entry
不存在。
let numbers = [
{ to: 1, from: 2 },
{ to: 3, from: 4 },
{ to: 5, from: 6 },
{ to: 7, from: 8 },
{ to: 1, from: 2 } // intentionally added duplicate
];
let entry = { to: 1, from: 2 };
/*
* 1. This will return the *index of the first* element that matches:
*/
_.findIndex(numbers, (o) => { return _.isMatch(o, entry) });
// output: 0
/*
* 2. This will return the entry that matches. Even if the entry exists
* multiple time, it is only returned once.
*/
_.find(numbers, (o) => { return _.isMatch(o, entry) });
// output: {to: 1, from: 2}
/*
* 3. This will return an array of objects containing all the matches.
* If an entry exists multiple times, if is returned multiple times.
*/
_.filter(numbers, _.matches(entry));
// output: [{to: 1, from: 2}, {to: 1, from: 2}]
/*
* 4. This will return `true` if the entry exists, false otherwise.
*/
_.some(numbers, entry);
// output: true
如果要返回一个Boolean
(即,假设您没有使用_.some()
),在第一种情况下,您只需检查正在返回的索引值:
_.findIndex(numbers, (o) => { return _.isMatch(o, entry) }) > -1;
// output: true
Lodash
文档是示例和实验的重要来源。
如果您只想在数组中插入一个值,那么使用_.find
可能是一个选项。 但是,如果您有兴趣插入一个或多个,我建议使用_.unionBy
:
var currentArr = [{ "name": "apple", "id": "apple_0" }, { "name": "dog", "id": "dog_1" }, { "name": "cat", "id": "cat_2" }], arrayOneValue = [{ "name": "apple", "id": "apple_0" }], arrayTwoValues = arrayOneValue.concat({ "name": "lemon", "id": "lemon_0" }) console.log(_.unionBy(currentArr, arrayOneValue, 'name')); console.log(_.unionBy(currentArr, arrayTwoValues, 'name')); // It also allow you to perform the union using more than one property console.log(_.unionBy(currentArr, arrayTwoValues, 'name', 'id'));
<script src="https://cdn.jsdelivr.net/lodash/4.16.4/lodash.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.