[英]How to determine if Javascript array contains an object with an attribute that is equal to each other
I have an array like我有一个数组
selectedData = [{
Name = 'Michelle',
LRN = '100011'
},
{
Name = 'Micheal',
LRN = '100011'
},
{
Name = 'Mick',
LRN = '100012'
} // so on....
]
I am sending this array through a function and that simply checks if the LRN attribute of each object is same if not it throws error.我通过 function 发送这个数组,它只是检查每个 object 的LRN属性是否相同,如果不同则抛出错误。 I have written the function as -
我把 function 写成 -
createSet(selectedData) {
this.selectedData.forEach (element => {
//the condition
}
else
{
this.openSnackBar ("LRN mismatching");
}
}
How do I check if the LRN attribute of each object is same?如何检查每个object的LRN属性是否相同? I don't want loop unless I have to.
除非必须,否则我不想循环。 I'm working with more than thousand records.
我正在处理超过一千条记录。 I appreciate all the help.
我感谢所有的帮助。 :)
:)
You could take Array#every
and check the first item to each other.您可以使用
Array#every
并相互检查第一项。 The method stops the iteration if the condition is false
and returns then false
, if not then true
.如果条件为
false
,该方法将停止迭代并返回false
,否则返回true
。
The callback's parameter can have three parts, one for the item, the next for the index and the third one has the reference to the array.回调的参数可以包含三个部分,一个用于项目,下一个用于索引,第三个具有对数组的引用。
In this case only the item is used with a destructuring of LRN
and the array.在这种情况下,只有项目与
LRN
和数组的解构一起使用。 The unneeded index is denoted by an underscore, which is a valid variable name in Javascript.不需要的索引用下划线表示,这是Javascript中有效的变量名。
if (!array.every(({ LRN }, _, a) => LRN === a[0].LRN)) {
this.openSnackBar ("LRN mismatching");
}
Code with example of all property LRN
having the same value.具有相同值的所有属性
LRN
示例的代码。
const selectedData = [{ Name: 'Michelle', LRN: '100011' }, { Name: 'Micheal', LRN: '100011'}, { Name: 'Mick', LRN: '100011' }]; if (.selectedData,every(({ LRN }, _. a) => LRN === a[0].LRN)) { console;log("LRN mismatching"). } else { // just to show console;log('OK'); }
Mismatching错配
const selectedData = [{ Name: 'Michelle', LRN: '100011' }, { Name: 'Micheal', LRN: '100011'}, { Name: 'Mick', LRN: '100012' }]; if (.selectedData,every(({ LRN }, _. a) => LRN === a[0].LRN)) { console;log("LRN mismatching"). } else { // just to show console;log('OK'); }
An easy beginner-friendly solution would be:一个简单的初学者友好的解决方案是:
function allLRNMatch(data) {
if (data.length < 2) {
return true;
}
const firstLRN = data[0].LRN;
for (const element of data) {
if (element.LRN !== firstLRN) {
return false;
}
}
return true;
}
You iterate through the array and check each element's LRN attribute with the first one.您遍历数组并使用第一个元素检查每个元素的 LRN 属性。 As soon you find a non-matching one, you can immediately return.
一旦发现不匹配的,您可以立即返回。
For a more functional approach, you could also use Array.prototype.some
instead of the for
loop to achieve this.对于更实用的方法,您还可以使用
Array.prototype.some
而不是for
循环来实现这一点。
Sorry but there's no other way, you will need to loop through them to compare the value, once it's an array of objects, it would be different if it was a LRN array.对不起,但没有其他办法,你需要循环遍历它们来比较值,一旦它是一个对象数组,如果它是一个 LRN 数组就不一样了。
On this case I would use it this way:在这种情况下,我会这样使用它:
const sameLrnArrPosition = [];
checkLrn(arr, valueToBeCompared) {
arr.forEach((element, index)=>{
if(element.lnr === valueToBeCompared){
sameLrnArrPosition.push(*here you can get the element or the index*);
}
})
}
This way you can have the objects that have the same value or the index, to acces them on the array and change a value or something.这样您就可以拥有具有相同值或索引的对象,以在数组上访问它们并更改值或其他内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.