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

ramya m

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. 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.

Sasi Kumar M

You can use 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

enter image description here

JS Fiddle: https://jsfiddle.net/2cpe3ts6/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I make the code compare 2 values in an array?

How to convert an array with objects that contain value,values to a single object with only key,values?

How to compare values in an array, that is inside an array of objects, with a property of the objects?

compare values of an array, with the key of an array of objects

Compare objects recursively and put duplicate key values into array

How do I flatten a nested json key/value pair into a single array of values?

How do I turn key value pairs in object into array of objects?

How I compare different parameters among the values of a single array in bash?

How do I query a set of objects with an array of values in mongoose?

How do I sort an array of objects by one of their property values in Powershell?

how to compare values of an array with a single value in matlab

How do I compare multiple key values from a list of dictionaries?

How do I go from an array of key => "array of values" to an array of an array of "key => values" in php

How do I compare pressed Key values in keyDown event

How do I add values to class objects then put them in an array?

How can I compare values between objects in an array?

How do i get values of id’s And also how do i merge multiple objects into single array into javascript

How do I compare two array Objects - Swift 4

How do I get all values in array of objects if hashMap has same key name

How do i assign and obtain values to objects created in an array?

How can i get nested json objects values in single key value pair array

compare array values with objects

How do I get values from a single row placed into an array?

Javascript - How to compare property values in objects in an array

How can i compare three array of objects so i an add or remove new values?

How to compare object values in an array of objects?

How do i access a property on the objects in an array and compare it?

How do I exclude null values when merging objects by key?

How do I compare values?