0

I have an array:

let docArray = [ 
    { documentID: 304717110, doublettenID: 111, done: true },
  { documentID: 304717110, doublettenID: 222, done: true },
  { documentID: 304717110, doublettenID: 333, done: true },
  { documentID: 304717110, doublettenID: 444, done: true },
  { documentID: 304717110, doublettenID: 555, done: true },
  { documentID: 304717110, doublettenID: 666, done: true },
  { documentID: 304862280, doublettenID: null, done: true },
  { documentID: 304862281, doublettenID: null, done: true },
  { documentID: 304933685, doublettenID: null, done: true },
  { documentID: 304933871, doublettenID: null, done: true },
  { documentID: 304933870, doublettenID: null, done: true },
  { documentID: 304862282, doublettenID: null, done: true } 
  ]

I would like to group by documentID, and create an Object for each distinct documentID and push the "doublettenID" field values inside an array for each object. How can I easily do this?

3
  • just have a look here: stackoverflow.com/questions/40774697/… Commented Jun 13, 2019 at 12:47
  • Have you tried to do this by yourself? Commented Jun 13, 2019 at 12:47
  • I have tried it by myself, but somehow I couldn't figure it out after 4hours, I feel totally stupid sorry ;). I first tried to iterate through all values and create dynamically an object with a key with the id and check if that object with the key is already present or not, but I had the problem, that I could only create the object inside the array for-loop, therefore I had to create a new object all the time instead of a persisted object in the outer-loop, I also tried with .map and .reduce() function but couldn't figure it out... Commented Jun 13, 2019 at 12:50

3 Answers 3

1

This is a way to do it, I'm not sure if this is the object format that you need.

let docArray = [ 
    { documentID: 304717110, doublettenID: 111, done: true },
  { documentID: 304717110, doublettenID: 222, done: true },
  { documentID: 304717110, doublettenID: 333, done: true },
  { documentID: 304717110, doublettenID: 444, done: true },
  { documentID: 304717110, doublettenID: 555, done: true },
  { documentID: 304717110, doublettenID: 666, done: true },
  { documentID: 304862280, doublettenID: null, done: true },
  { documentID: 304862281, doublettenID: null, done: true },
  { documentID: 304933685, doublettenID: null, done: true },
  { documentID: 304933871, doublettenID: null, done: true },
  { documentID: 304933870, doublettenID: null, done: true },
  { documentID: 304862282, doublettenID: null, done: true } 
  ]
  
  var ids = []
  var res = []
  
  docArray.forEach(doc => {   
    if (!ids.includes(doc.documentID)) {
    	doc.doublettenID = [doc.doublettenID]
  		res.push(doc)
      ids.push(doc.documentID)
    } else {
    	res.find(d => doc.documentID = d.documentID).doublettenID.push(doc.doublettenID)
    }
 })
  
console.log(res)

Sign up to request clarification or add additional context in comments.

2 Comments

what does this syntax do: doc.doublettenID = [doc.doublettenID] ?
replaces the value of doc.doublettenID with a new array containing doc.doublettenID, is just to transform the current value to an array containing the current value
1

You could loop through it and use the documentID as a key, and push to a seperate object.

let docArray = [ 
  { documentID: 304717110, doublettenID: 111, done: true },
  { documentID: 304717110, doublettenID: 222, done: true },
  { documentID: 304717110, doublettenID: 333, done: true },
  { documentID: 304717110, doublettenID: 444, done: true },
  { documentID: 304717110, doublettenID: 555, done: true },
  { documentID: 304717110, doublettenID: 666, done: true },
  { documentID: 304862280, doublettenID: null, done: true },
  { documentID: 304862281, doublettenID: null, done: true },
  { documentID: 304933685, doublettenID: null, done: true },
  { documentID: 304933871, doublettenID: null, done: true },
  { documentID: 304933870, doublettenID: null, done: true },
  { documentID: 304862282, doublettenID: null, done: true } 
];
let groups = {};

docArray.forEach(entry => {
    if (!groups.hasOwnProperty(entry.documentID)) {
	groups[entry.documentID] = [];
    }

    groups[entry.documentID].push(entry.doublettenID);
});

console.log(groups);

4 Comments

if I have an object like this: { "304717110": [...]}, how do I address the object with that dynamically created key as name?
obj["key"] or obj[variableThatContainsKey] instead of obj.property.
well, but the problem is just that I don't know in advance the name of the key... as they are dynamically created based on some other field value, which I dont know beforehand...
Object.keys(obj) will give you an array of keys.
1

I would advise you to use Array#reduce function. In my understanding it looks much readable.

let docArray = [{
    documentID: 304717110,
    doublettenID: 111,
    done: true
  },
  {
    documentID: 304717110,
    doublettenID: 222,
    done: true
  },
  {
    documentID: 304717110,
    doublettenID: 333,
    done: true
  },
  {
    documentID: 304717110,
    doublettenID: 444,
    done: true
  },
  {
    documentID: 304717110,
    doublettenID: 555,
    done: true
  },
  {
    documentID: 304717110,
    doublettenID: 666,
    done: true
  },
  {
    documentID: 304862280,
    doublettenID: null,
    done: true
  },
  {
    documentID: 304862281,
    doublettenID: null,
    done: true
  },
  {
    documentID: 304933685,
    doublettenID: null,
    done: true
  },
  {
    documentID: 304933871,
    doublettenID: null,
    done: true
  },
  {
    documentID: 304933870,
    doublettenID: null,
    done: true
  },
  {
    documentID: 304862282,
    doublettenID: null,
    done: true
  }
];

const ar = docArray.reduce((a, c) => {
  debugger;
  let filtered = a.filter(el => el.documentID === c.documentID);
  if (filtered.length > 0) {
    a[a.indexOf(filtered[0])].doublettenID.push(c.doublettenID);
  } else {
    if (c.doublettenID !== null) {
      let originalValue = c.doublettenID;
      c.doublettenID = [];
      c.doublettenID.push(originalValue);
    }

    a.push(c);
  }
  return a;
}, []);

$('#pre').text(JSON.stringify(ar));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<pre id='pre'></pre>

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.