简体   繁体   English

如何将单个对象数组与其键值进行比较

[英]How do I compare single array of objects with it key values

I have an array of objects, Where I need to compare "taskId" & "resourceId" of its own array of objects and push the result in a new array.我有一个对象数组,我需要比较它自己的对象数组的“taskId”和“resourceId”并将结果推送到一个新数组中。 Here is my array这是我的数组

data = [{
  "resourceId":1234
  "taskId":5001
  "taskName":"Test task1"
 },
 {
  "resourceId":1234
  "taskId":5001
  "taskName":"Test task2"
}
{
 "resourceId":1234
 "taskId":5002
 "taskName":"Test task3"
},
{
 "resourceId":1234
 "taskId":5001
 "taskName":"Test task4"
},
{
 "resourceId":5678
 "taskId":5003
 "taskName":"Test task5"
},
{
 "resourceId":5678
 "taskId":5004
 "taskName":"Test task6"
}
]

Please help me to build this logic.请帮我建立这个逻辑。 I appreciate your efforts in advance.我提前感谢您的努力。

Create a new method filterArray to filter the array.创建一个新方法filterArray来过滤数组。

function filterArray(dataArray, resourceId, taskId){
   return dataArray.filter(item => item.resourceId === resourceId && item.taskId === taskId)
}

The filter method on the array will loop through each item in the dataArray , compare the values of resourceId and taskId and if they matches, push them to the new array.数组上的filter方法将遍历dataArray中的每个项目,比较resourceIdtaskId的值,如果它们匹配,则将它们推送到新数组。 This new array is returned from the method.这个新数组是从方法返回的。

You can now call this method to filter:您现在可以调用此方法进行过滤:

let filteredArray = filterArray(data, 1234, 5001)

filteredArray will now contain all items with resourceId = 1234 and taskId = 5001 filteredArray数组现在将包含所有resourceId = 1234taskId = 5001的项目

1- Your JSON array isn't well formatted. 1- 您的 JSON 数组格式不正确。

2- As per your expected outcome in comments, basically you need those data which have multiple occurrences only. 2-根据您在评论中的预期结果,基本上您需要那些仅出现多次的数据。 So here is how you can get:因此,您可以通过以下方式获得:

 var data = [{ "resourceId":1234, "taskId":5001, "taskName":"Test task1" }, { "resourceId":1234, "taskId":5001, "taskName":"Test task2" }, { "resourceId":1234, "taskId":5002, "taskName":"Test task3" }, { "resourceId":1234, "taskId":5001, "taskName":"Test task4" }, { "resourceId":5678, "taskId":5003, "taskName":"Test task5" }, { "resourceId":5678, "taskId":5004, "taskName":"Test task6" } ]; const result = []; data.forEach((item) => { let dupes = data.filter((elem) => elem.resourceId == item.resourceId && elem.taskId == item.taskId); if (dupes.length >1) { result.push(item); } }); console.log(result);

 data = [{ "resourceId":1234, "taskId":5001, "taskName":"Test task1" }, { "resourceId":1234, "taskId":5001, "taskName":"Test task2" }, { "resourceId":1234, "taskId":5002, "taskName":"Test task3" }, { "resourceId":1234, "taskId":5001, "taskName":"Test task4" }, { "resourceId":5678, "taskId":5003, "taskName":"Test task5" }, { "resourceId":5678, "taskId":5004, "taskName":"Test task6" } ] var result=[]; data.map(x=>{ var i=result.findIndex(y=> y.resourceId==x.resourceId && y.taskId==x.taskId ); if( i == -1){ result.push( { "resourceId":x.resourceId,"taskId":x.taskId,"taskName":x.taskName } ) }else{ result[i].taskName += " "+x.taskName; } }) console.log(result)

 data = [{ "resourceId":1234, "taskId":5001, "taskName":"Test task1" }, { "resourceId":1234, "taskId":5001, "taskName":"Test task2" }, { "resourceId":1234, "taskId":5002, "taskName":"Test task3" }, { "resourceId":1234, "taskId":5001, "taskName":"Test task4" }, { "resourceId":5678, "taskId":5003, "taskName":"Test task5" }, { "resourceId":5678, "taskId":5004, "taskName":"Test task6" } ] var result=[]; data.map(x=>{ var i=result.findIndex(y=> y.resourceId==x.resourceId && y.taskId==x.taskId ); if( i == -1){ result.push( { "resourceId":x.resourceId,"taskId":x.taskId,"taskName":[x.taskName] } ) }else{ result[i].taskName.push( x.taskName); } }) console.log(result)

You can use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce您可以使用https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

var result = data.reduce(function (r, a) {
    var value = a.resourceId.toString().concat(a.taskId);
    r[value] = r[value] || [];
    r[value].push(a);
    return r;
}, Object.create(null));

This will result the new array as shown below, you can modify the key name(array names) as required这将产生如下所示的新数组,您可以根据需要修改键名(数组名)

在此处输入图像描述

JS Fiddle: https://jsfiddle.net/2cpe3ts6/ JS小提琴: https://jsfiddle.net/2cpe3ts6/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在一个键下对 JSON 数组中的所有对象的键下存在的值进行分组? - How do I group values present under a key in all objects in an array of JSON, under a single key? 比较数组的值和对象数组的键 - compare values of an array, with the key of an array of objects 如何解析对象数组并将其与其他数组对象进行比较? - How do I parse array of objects and compare it with other array objects? 循环对象数组,比较键值 - Loop array of objects, compare key values 如何在单键值对数组中获取嵌套的 json 对象值 - How can i get nested json objects values in single key value pair array 如何将嵌套的对象数组转换为单个对象数组? - How do I transform this nested array of objects into a single array of objects? 如何比较数组中对象之间的值? - How can I compare values between objects in an array? 如何将输入与一个 object 键和值从具有更多对象和更多键和值的数组中进行比较 - How to compare an input with one object key and value from an array with more objects with more key and values 如何访问数组中对象的属性并进行比较? - How do i access a property on the objects in an array and compare it? Javascript - 我如何比较数组的两个对象? - Javascript - How i do compare two objects of an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM