JavaScript Javascript - Put array items, including their duplicates, into a new array javascript array item exists,javascript array item index,javascript array

I couldn't find an answer to this specific question on S.O.

Let's say I have an array of strings, or in this case, numbers:

var x = [1, 1, 1, 2, 3, 3, 5, 3, 3, 5, 4, 5];

I'd like the output to be:

var output = [[1,1,1], [2], [3,3,3,3,3], [4], [5, 5, 5]];

I was hoping to use Lodash but most of that stuff tends to remove duplicates rather chunk them together into their own array. Maybe some kind of .map iterator?

The order of the output doesn't really matter so much. It just needs to chunk the duplicates into separate arrays that I'd like to keep.

Answer:1

You can use reduce to group the array elements into an object. Use Object.values to convert the object into an array.

var x = [1, 1, 1, 2, 3, 3, 5, 3, 3, 5, 4, 5];
var result = Object.values(x.reduce((c, v) => {
  (c[v] = c[v] || []).push(v);
  return c;
}, {}));

console.log(result);
Answer:2

You can do this with Array.reduce in a concise way like this:

var x = [1, 1, 1, 2, 3, 3, 5, 3, 3, 5, 4, 5]

let result = x.reduce((r,c) => (r[c] = [...(r[c] || []), c],r), {})

console.log(Object.values(result))
Answer:3

Use Array#prototype#reduce to group them:

const x = [1, 1, 1, 2, 3, 3, 5, 3, 3, 5, 4, 5];

let helperObj = {};
const res = x.reduce((acc, curr) => {

  // If key is not present then add it
  if (!helperObj[curr]) {
    helperObj[curr] = curr;
    acc.push([curr]);
  }

  // Else find the index and push it
  else {
    let index = acc.findIndex(x => x[0] === curr);

    if (index !== -1) {
      acc[index].push(curr);
    }
  }
  return acc;
}, []);

console.log(res);
Answer:4

Since you're hoping to use Lodash, you might be interested in groupBy. It returns on object, but the _.values will give you the nested array:

var x = [1, 1, 1, 2, 3, 3, 5, 3, 3, 5, 4, 5];
let groups = _.values(_.groupBy(x))
console.log(groups)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
Answer:5

For some reason, d3 uses this to refer to the current element in a .each() iteration. I had this code: var me = this; ... d3.selectAll(".region").each(function(d) { d3.select(this).style("fill", ...

For some reason, d3 uses this to refer to the current element in a .each() iteration. I had this code: var me = this; ... d3.selectAll(".region").each(function(d) { d3.select(this).style("fill", ...

  1. d3 season 20
  2. d303
  3. d300
  4. d3 football
  5. d3 baseball
  6. d3hoops
  7. d3planner
  8. d3 vitamin
  9. d3500
  10. d3 basketball tournament
  11. d3 football playoffs
  12. d365
  13. d3 basketball rankings
  14. d3 dice
  15. d3 wrestling championships 2020
  16. d3 hockey rankings
  17. d3 football rankings

I need to replace every element in an array with the max element that belong to the right side set of elements in that array. I got a solution with the below code: let arr1 = [5,0,5,7,9,4,8]; var ...

I need to replace every element in an array with the max element that belong to the right side set of elements in that array. I got a solution with the below code: let arr1 = [5,0,5,7,9,4,8]; var ...

I have a form with an input type="file". I have a div surrounding the input. I then set the input to display:none. In JavaScript, I set that when you select the div, the input gets selected. That all ...

I have a form with an input type="file". I have a div surrounding the input. I then set the input to display:none. In JavaScript, I set that when you select the div, the input gets selected. That all ...

  1. trigger drop event javascript
  2. trigger drop event jquery
  3. js trigger drop event
  4. trigger drag and drop event
  5. trigger dropdown change event
  6. event trigger drop unity
  7. drop event trigger

So I am using bootstrap and have collapse panels I wish to implement plus and minus icons. I have currently done it using the code below found on stackoverflow, i can't remember the link so will edit ...

So I am using bootstrap and have collapse panels I wish to implement plus and minus icons. I have currently done it using the code below found on stackoverflow, i can't remember the link so will edit ...

  1. minus plus icons
  2. plus minus icons css
  3. plus minus icons.png
  4. plus minus icons free
  5. plus minus icons free download
  6. plus minus icon black
  7. plus minus icon font awesome
  8. plus minus icon bootstrap
  9. plus minus icon in accordion
  10. plus minus icon toggle
  11. plus minus icon svg
  12. plus minus icon html
  13. plus minus icon in bootstrap 4
  14. plus minus icon png transparent
  15. plus minus icon animation css
  16. plus minus icon transparent
  17. plus minus icon button
  18. plus minus icon blue
  19. plus minus icon transparent background
  20. plus minus icon symbol