Home > Enterprise >  Extract one/many property from a json object array into another object
Extract one/many property from a json object array into another object

Time:10-14

I have got a JSON object array like this

[
  {"Project":"Project 1","Domain":"Domain1","Manager":"Manager1"},
  {"Project":"Project 2","Domain":"Domain2","Manager":"Manager2"},  
  {"Project":"Project 3","Domain":"Domain3","Manager":"Manager1"},
  {"Project":"Project 4","Domain":"Domain1","Manager":"Manager3"},
  {"Project":"Project 5","Domain":"Domain5","Manager":"Manager4"},
  {"Project":"Project 6","Domain":"Domain6","Manager":"Manager5"},
  {"Project":"Project 7","Domain":"Domain5","Manager":"Manager2"},
  {"Project":"Project 8","Domain":"Domain2","Manager":"Manager5"},
  {"Project":"Project 9","Domain":"Domain7","Manager":"Manager8"},
  {"Project":"Project 10","Domain":"Domain9","Manager":"Manager9"},
  {"Project":"Project 11","Domain":"Domain6","Manager":"Manager1"}
]

From the above object array I am required to extract all distinct domains and managers into another 2 string arrays

So what I am trying is like this

Domains:string[]=[];
Domains= res.map((x)=>x.Domain);
Managers:string[]=[];
Managers= res.map((x)=>x.Manager);

Its working, but it has duplicates in both arrays.

Also is there is any better way to assign those variables in a single map function rather than separately ?

CodePudding user response:

A hack is to use object keys as unique set. The quick but dirty way to write it is:

Domains= Object.keys(res.reduce((y, x)=>{y[x.Domain]=x.Domain; return y}, {}));

Above, you add each "domain" value as a key in an object, and get list of key names at the end.

The proper way to do things is to use Set: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

CodePudding user response:

const d = [{"Project":"Project 1","Domain":"Domain1","Manager":"Manager1"},{"Project":"Project 2","Domain":"Domain2","Manager":"Manager2"},{"Project":"Project 3","Domain":"Domain3","Manager":"Manager1"},{"Project":"Project 4","Domain":"Domain1","Manager":"Manager3"},{"Project":"Project 5","Domain":"Domain5","Manager":"Manager4"},{"Project":"Project 6","Domain":"Domain6","Manager":"Manager5"},{"Project":"Project 7","Domain":"Domain5","Manager":"Manager2"},{"Project":"Project 8","Domain":"Domain2","Manager":"Manager5"},{"Project":"Project 9","Domain":"Domain7","Manager":"Manager8"},{"Project":"Project 10","Domain":"Domain9","Manager":"Manager9"},{"Project":"Project 11","Domain":"Domain6","Manager":"Manager1"}]

// Using Set for unique values
/*const result = d.reduce((a, i) => {
  Object.keys(i).forEach(k => a[k] = a[k] ? [...new Set([...a[k], i[k]])] : [i[k]]);
  return a;
}, {});*/

// without Set
const result = d.reduce((a, i) => (Object.keys(i).forEach(k => !a[k] ? a[k] = [i[k]] : !a[k].includes(i[k]) ? a[k].push(i[k]) : '') , a), {});

// to access
console.log(result['Project']);
console.log(result['Manager']);
console.log(result['Domain']);
.as-console-wrapper { max-height: 100% !important; top: 0 }

  • Related