1

Hi I'm trying to group my array by merge siblings if it has a same key with the same value

      let array = [
        {el: 1, ready: true},
        {el: 2, ready: false},
        {el: 3, ready: false},
        {el: 4, ready: false},
        {el: 5, ready: true},
        {el: 6, ready: true},
        {el: 7, ready: false},
      ];

want to map like this

      let arrayGroup = [
        [
          {el: 1, ready: true},
        ],
        [
          {el: 2, ready: false},
          {el: 3, ready: false},
          {el: 4, ready: false}
        ],
        [
          {el: 5, ready: true},
          {el: 6, ready: true}
        ],
        [
          {el: 7, ready: false}
        ]
      ];
1

4 Answers 4

1

You can follow my method:

let array = [
        {el: 1, ready: true},
        {el: 2, ready: false},
        {el: 3, ready: false},
        {el: 4, ready: false},
        {el: 5, ready: true},
        {el: 6, ready: true},
        {el: 7, ready: false},
      ];
/*
      let arrayGroup = [
        [
          {el: 1, ready: true},
        ],
        [
          {el: 2, ready: false},
          {el: 3, ready: false},
          {el: 4, ready: false}
        ],
        [
          {el: 5, ready: true},
          {el: 6, ready: true}
        ],
        [
          {el: 7, ready: false}
        ]
      ];
*/
var arrayGroup = [];
array.forEach((current,index)=>{
  if(index===0){
    arrayGroup.push([{el:current.el, ready:current.ready}]);
  }else{
    //check previous group if same will add to there, or not same, will add to new 
    if(current.ready === array[index-1].ready){
      arrayGroup[arrayGroup.length-1].push({el:current.el, ready:current.ready});
    }else{
      arrayGroup.push([{el:current.el, ready:current.ready}]);
    };
  };
});
console.log(arrayGroup);

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

Comments

0

This should work:

let array = [{
    el: 1,
    ready: true
  },
  {
    el: 2,
    ready: false
  },
  {
    el: 3,
    ready: false
  },
  {
    el: 4,
    ready: false
  },
  {
    el: 5,
    ready: true
  },
  {
    el: 6,
    ready: true
  },
  {
    el: 7,
    ready: false
  },
];

let previousArrValue = array[0].ready;
let newArrayBuilt = [];
let newArr = [];

for (let i = 0; i < array.length; i++) {


  if (array[i].ready !== previousArrValue) {
    newArrayBuilt.push(newArr);
    newArr = [];
  }


  previousArrValue = array[i].ready;
  newArr.push(array[i]);

  if (i + 1 === array.length) {
    newArrayBuilt.push(newArr);
    break;
  }


}

console.log(newArrayBuilt)

Comments

0

In this situation Array.reduce can be helpful. For each object visited, it is returned the ready property, that becomes the next accumulator.

  let starter = !array[0].ready;
  let new_arr = [];
  let new_index = -1;

  array.reduce((acc, cur, index, arr) => {
    if (acc != arr[index].ready)
    {
        new_arr.push([cur]);
        new_index++;
    }
    else
    {
        new_arr[new_index].push(cur);
    }

    return cur.ready;
  }, starter);

new_arr is the array you need. In this case an initial value (starter) is needed so that the reduce function begins with a creation of a new sub-array.

Comments

0

let array = [
  {el: 1, ready: true},
  {el: 2, ready: false},
  {el: 3, ready: false},
  {el: 4, ready: false},
  {el: 5, ready: true},
  {el: 6, ready: true},
  {el: 7, ready: false},
];

let currentReadyStatus = array[0].ready;
finalArray = [[]];
currentIndex = 0;

array.forEach((item) => {
  if(item.ready === currentReadyStatus)
     finalArray[currentIndex].push(item);
  else{
    currentReadyStatus = !currentReadyStatus;
    currentIndex += 1;
    finalArray[currentIndex] = [item]
  }
})

console.log(finalArray)

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.