简体   繁体   English

如何判断Javascript数组是否包含一个属性相等的object

[英]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.

相关问题 如何确定 Javascript 数组是否包含具有等于给定值的属性的 object? - How to determine if Javascript array contains an object with an attribute that equals a given value? 如何检查Javascript对象是否包含数组作为属性值? - How to check if a Javascript object contains an array as a value for an attribute? 确定数组是否包含JavaScript中指定值以外的任何内容? - Determine whether an array contains anything other than a specified value in javascript? 使javascript数组中的值之间的距离彼此相等 - Make the distance between values in javascript array equal between each other 如何确定字符串是否包含数组,对象数组或函数定义 - How to determine if string contains array, object array or function definition 检查对象数组中的每个 object 是否包含另一个数组中的值(JavaScript) - Check if each object in array of objects contains a value that is in another array (JavaScript) 如何在 Javascript 中确定数组 object 中的前 2 条记录 - How to determine top 2 records in an array object in Javascript 如何确定参数是JavaScript中的数组还是对象? - How to determine if parameter is an Array or an Object in JavaScript? 如何确定对象是否仅在JavaScript中具有属性的数组中? - how to determine if an object is in an array only with a property in javascript? 如何访问javascript数组包含对象中的responsedata - how to access responsedata in javascript array contains object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM