1

I have created a fiddle here of my question, with the help of some of the answers below.

I have an array of objects. An individual object in the a array contains:

obj1, obj2, obj3...

An example of one of the object is:

{
client_id: "85"
id: 1477
organisation_id: 5
task_project_id: 26
project_name: "Reiciendis adipisci fugiat."
project_description: "Soluta consequatur labore et dolores"
task_description: "Qui sunt at aut."
task_end_time: null
task_start_time: "2016-05-21 09:00:00"
transaction_id: null
}

I want to generate a new array of objects from this first array containing distinct entries of task_project_id. The new array should contain distinct objects structured per below:

{
task_project_id: 26
project_name: "Reiciendis adipisci fugiat."
}

I have tried jQuery.grep, array.filter, array.map but to no avail. I also cannot seem to find examples using "extract array of distinct objects" as search. Could someone point me to an example please. Thanks.

1
  • 1
    I don't get it. How does the first object example relate to the second? And how do you define a "unique" object? Commented Sep 3, 2016 at 21:02

3 Answers 3

2

array.map should work just fine:

var b = a.map(function(e) {
  return {
    task_project_id: e.task_project_id,
    project_description: e.project_description
  }
});

Edit: filter out duplicates, by storing a list of already encountered "seen" task project ids:

var seen = {}; // keys = list of unique task project ids
var b = a.filter(function (e) {
  return seen[e.task_project_id] ? false : (seen[e.task_project_id] = true);
});

Putting it all together:

var newArray = (function (a) {
  var seen = {};
  return a.filter(function (e) {
    return seen[e.task_project_id] ? false : (seen[e.task_project_id] = true);
  }).map(function (e) {
    return {
      task_project_id: e.task_project_id,
      project_description: e.project_description
    }
  });
})(oldArray);
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @sebnukem thanks for the reply. I am looking to filter out duplicate task_project_id entries though.
2

Is this what you want ? Check the output.!

var oldArray = [{
    client_id: "85",
    id: 1477,
    organisation_id: 5,
    task_project_id: 26,
    project_name: "Reiciendis adipisci fugiat.",
    project_description: "Soluta consequatur labore et dolores",
    task_description: "Qui sunt at aut.",
    task_end_time: null,
    task_start_time: "2016-05-21 09:00:00",
    transaction_id: null
  }, {
    client_id: "86",
    id: 1434,
    organisation_id: 5,
    task_project_id: 22,
    project_name: "Reiciddgdgdfgci fugiat.",
    project_description: "Soluta consequatur labore et dolores",
    task_description: "Qui sunt at aut.",
    task_end_time: null,
    task_start_time: "2016-05-21 09:00:00",
    transaction_id: null
  }],
  newArray;
newArray = oldArray.map(function(obj) {
  return {
    task_project_id: obj.task_project_id,
    project_description: obj.project_description
  }
});

console.log(newArray);

1 Comment

Hi @Nikhilesh Shivarathri close! I am seeking the new array to contain distinct task_project_id entries only. I think I can work from this now though. Thanks for the help!
0

ES6:

let map = new Map();
a = [{a:1, b:"blue"},{a:1, b:"red"}]
a.map(item=>map.set(item.a, item));
let result = [...map.values()];

result:

[ { a: 1, b: 'red' } ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.