1

Hello I want to merge a array based on the unique item in the array.

The object that I have

totalCells = []

In this totalCells array I have several objects like this

totalCells = [
  {
    cellwidth: 15.552999999999999,
    lineNumber: 1
  }, 
  {
    cellwidth: 14,
    lineNumber: 2
  },
  {
    cellwidth: 14.552999999999999,
    lineNumber: 2
  }, 
  {
    cellwidth: 14,
    lineNumber: 1
  }
];

Now I want to make a array where I have combination of array based on the lineNumber.

Like I have a object with lineNumber property and cellWidth collection. Can I do this ?

I can loop through each row and check if the line number is same and then push that cellwidth. Is there any way that I can figure ?

I'm trying to get an output like this.

totalCells = [
{
  lineNumber : 1,
  cells : [15,16,14]
},
{
  lineNumber : 2,
  cells : [17,18,14]
}
]
5
  • w3schools.com/jsref/jsref_concat_array.asp Commented Jun 19, 2014 at 8:59
  • I want to concat based on the lineNumber Commented Jun 19, 2014 at 9:00
  • can you show what result you're trying to get? Commented Jun 19, 2014 at 9:00
  • 3
    Your objects are not valid. Objects are enclosed in { ... }, [...] is only for arrays. Commented Jun 19, 2014 at 9:01
  • This is a array with sub object with array Commented Jun 19, 2014 at 9:03

3 Answers 3

1
var newCells = [];
for (var i = 0; i < totalCells.length; i++) {
    var lineNumber = totalCells[i].lineNumber;
    if (!newCells[lineNumber]) { // Add new object to result
        newCells[lineNumber] = {
            lineNumber: lineNumber,
            cellWidth: []
        };
    }
    // Add this cellWidth to object
    newcells[lineNumber].cellWidth.push(totalCells[i].cellWidth);
}
Sign up to request clarification or add additional context in comments.

Comments

1

What about something like this :

totalCells.reduce(function(a, b) {
  if(!a[b.lineNumber]){
    a[b.lineNumber] = {
      lineNumber: b.lineNumber,
      cells: [b.cellwidth]
    }
  }
  else{
    a[b.lineNumber].cells.push(b.cellwidth);
  }
  return a;
}, []);

Hope this helps!

Comments

0

Do you mean something like this?

var cells = [
{
  cellwidth: 15.552999999999999,
  lineNumber: 1
}, 
{
  cellwidth: 14,
  lineNumber: 2
},
{
  cellwidth: 14.552999999999999,
  lineNumber: 2
}, 
{
  cellwidth: 14,
  lineNumber: 1
}
]

var totalCells = [];
for (var i = 0; i < cells.length; i++) {
    var cell = cells[i];
    if (!totalCells[cell.lineNumber]) {
        // Add object to total cells
        totalCells[cell.lineNumber] = {
            lineNumber: cell.lineNumber,
            cellWidth: []
        }
    }
    // Add cell width to array
    totalCells[cell.lineNumber].cellWidth.push(cell.cellwidth);
}

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.