Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 查找数组中是否存在对象属性的详细信息_Javascript_Arrays_Reactjs_Lodash - Fatal编程技术网

Javascript 查找数组中是否存在对象属性的详细信息

Javascript 查找数组中是否存在对象属性的详细信息,javascript,arrays,reactjs,lodash,Javascript,Arrays,Reactjs,Lodash,我有这样一个对象数组: [ {"name": "apple", "id": "apple_0"}, {"name": "dog", "id": "dog_1"}, {"name": "cat", "id": "cat_2"} ] 我想插入另一个元素,也叫apple,但是,因为我不想在其中重复,我如何使用lodash来查看数组中是否已经存在同名的对象 您可以使用或lodash的\uu.find(): const addItem=(arr,item)=>{ 如果(!arr.fin

我有这样一个对象数组:

[ {"name": "apple", "id": "apple_0"}, 
  {"name": "dog",   "id": "dog_1"}, 
  {"name": "cat", "id": "cat_2"}
]
我想插入另一个元素,也叫
apple
,但是,因为我不想在其中重复,我如何使用lodash来查看数组中是否已经存在同名的对象

您可以使用或lodash的
\uu.find()

const addItem=(arr,item)=>{
如果(!arr.find((x)=>x.name==item.name)){//您还可以将'name'更改为'id'`
arr.push(项目);
}
};
常数arr=[
{“name”:“apple”,“id”:“apple_0”},
{“name”:“dog”,“id”:“dog_1”},
{“名称”:“猫”,“id”:“猫2”}
];
附加项(arr,{“name”:“apple”,“id”:“apple_0”});
附加项(arr,{“name”:“pear”,“id”:“pear_3”});

控制台日志(arr)
您可以像这样使用Lodash
\uuu.find()

var data=[{“name”:“apple”,“id”:“apple\u 0”},
{“name”:“dog”,“id”:“dog_1”},
{“名称”:“猫”,“id”:“猫2”}
]
if(!\.find(数据,{name:'apple'})){
push({name:'apple2'});
}
console.log(数据)

这里是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')
}

如果您只想在数组中插入一个值,那么可以使用
查找。但是,如果您有兴趣插入一个或多个,我建议使用
.unionBy

var currentArr=[{
“名称”:“苹果”,
“id”:“苹果0”
}, {
“名称”:“狗”,
“id”:“dog_1”
}, {
“名称”:“猫”,
“id”:“cat_2”
}],
arrayOneValue=[{
“名称”:“苹果”,
“id”:“苹果0”
}],
arrayTwoValues=arrayOneValue.concat({
“名称”:“柠檬”,
“id”:“lemon_0”
})
log(u.unionBy(currentArr,arrayOneValue,'name');
log(u.unionBy(currentArr,arrayTwoValues,'name');
//它还允许您使用多个属性执行联合
log(u.unionBy(currentArr,arrayTwoValues,'name','id')

这就是对我有效的方法(在测试了不同的解决方案之后):


使用
lodash
4.17.5
实现这一点有三种方法:

假设您要将对象
条目
添加到对象数组
数字
,前提是
条目
不存在

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
如果要返回
布尔值
(即,假设您没有使用
。.some()
),在第一种情况下,您只需检查返回的索引值:

_.findIndex(numbers, (o) => { return _.isMatch(o, entry) }) > -1;
// output: true
是示例和实验的重要来源。

这是一种形式

_.has(object, path)
例如:

const countries = { country: { name: 'Venezuela' } }
const isExist = _.has(countries, 'country.name')
// isExist = true

有关详细信息

ARRAY.find(function(el){return el.name=='apple';})
如果
未定义
,则数组中没有名为
“apple”
@reectrix
\uhas.has(object,path)
IMO的元素,最好使用“=====undefined`Why-”来测试它!undefined==true?可读性是一个很好的动机,但实际上这是个人的观点。我将添加一个较短且可读性较差的选项:)如果它是一个数组,如
[0,1,2]
,您将发现
0
,在这种情况下,
!0==真
,在这个特定的上下文中,您的方法很好:)确实您是对的,但在处理对象时不是这样的。\ u.find()文档的链接:这不是检查属性是否存在,而是假设属性存在,并检查其值是否正确。回答错了。虽然这是一个令人困惑的问题,但盖伊问了一件事,然后解释了另一件事。它告诉我们这条路径是否存在,而不是它的值。
const countries = { country: { name: 'Venezuela' } }
const isExist = _.has(countries, 'country.name')
// isExist = true