0

I have an array I need to group them based on size_y;

[
    {"col":4,"row":1,"size_x":1,"size_y":4}, 
    {"col":2,"row":2,"size_x":1,"size_y":2}, 
    {"col":1,"row":1,"size_x":3,"size_y":1},
    {"col":1,"row":4,"size_x":3,"size_y":1}, 
    {"col":1,"row":5,"size_x":4,"size_y":1}
]

Now, if size_y is 4 I want to group all the rows less than or equal to 4.

[
    {"col":4,"row":1,"size_x":1,"size_y":4}, 
    {"col":2,"row":2,"size_x":1,"size_y":2}, 
    {"col":1,"row":1,"size_x":3,"size_y":1},
    {"col":1,"row":4,"size_x":3,"size_y":1},
],
[
    {"col":1,"row":5,"size_x":4,"size_y":1}
]

thanks in advance.

14
  • So you want to get top data structure, from the bottom one? And bottom one is nested array? Is underscore a must? Commented Feb 3, 2019 at 4:05
  • no, from top data structure I need to get the bottom one. Underscore is recommended but not necessary. Commented Feb 3, 2019 at 4:08
  • 1
    What is the data structure at the updated question based on? All of the objects in the array have a "size_y" property, which "size_y" value should be evaluated? Commented Feb 3, 2019 at 4:16
  • initially, the 1st item should be checked. next, if the "size_y" is more then one then that should create another array as shown in the example. in the second step if the "size_y" is 2 and "row" is 2 then the nested array should contain the elements of row 2,3 only Commented Feb 3, 2019 at 4:20
  • @SravanSriram "initially, the 1st item should be checked." Ok. "next if the "size_y" is more then one then that should create another array as shown in the example." The element adjacent to the fist element has a "size_y" value less than the first element, which is not reflected in the resulting data structure at the updated question, correct?. Commented Feb 3, 2019 at 4:22

2 Answers 2

2

You can use a generator function and .splice()

let arr = [
    {"col":4,"row":1,"size_x":1,"size_y":4}, 
    {"col":2,"row":2,"size_x":1,"size_y":2}, 
    {"col":1,"row":1,"size_x":3,"size_y":1},
    {"col":1,"row":4,"size_x":3,"size_y":1}, 
    {"col":1,"row":5,"size_x":4,"size_y":1}
];

function* divideBy(a) {
  const [copy, [{size_y}] = copy] = [[...a]];
  while (copy.length) yield copy.splice(0, size_y);
}

let res = [...divideBy(arr)];

console.log(res)

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

2 Comments

size_y this will not be always same.
@SravanSriram The generator function accepts n as second parameter, where n is a positive integer
1

Using pure JS you can simply do using reduce

let arr = [{"col":4,"row":1,"size_x":1,"size_y":4},{"col":2,"row":2,"size_x":1,"size_y":2},{"col":1,"row":1,"size_x":3,"size_y":1}, {"col":1,"row":4,"size_x":3,"size_y":1},{"col":1,"row":5,"size_x":4,"size_y":1}]

let op = arr.reduce((out,inp)=>{
  if(inp.row > 4){
    if(out['>4']){
      out['>4'].push(inp)
    } else {
      out['>4'] = [inp]
    }
  }else{
    if(out['<4']){
      out['<4'].push(inp)
    } else {
      out['<4'] = [inp]
    }
  }
  return out
},{})

console.log(Object.values(op))

1 Comment

size_y this will not be always same

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.