0

I am trying to create a new array with it's key being the index of the old array

var array = [
          {"tom": "red", "part":"green", "brow_id":45},
          {"tom": "red", "part":"yellow", "brow_id":1},
          {"tom": "red", "part":"yellow", "brow_id":2},
          {"tom": "maroon", "part":"cyan", "brow_id":45}
           ];
var newarray = {};

array.forEach(function(elem) {
        newarray[elem.brow_id] = elem;
                    });

The newly formed array is like this

         45: {"tom": "red", "part":"green", "brow_id":45},
         1:  {"tom": "red", "part":"yellow", "brow_id":1},
         2:  {"tom": "red", "part":"yellow", "brow_id":2},

I want it to contain all the id's of the old array like this

        45: [{"tom": "red", "part":"green", "brow_id":45},{"tom": "maroon", "part":"yellow", "brow_id":45}]
         1:  {"tom": "red", "part":"yellow", "brow_id":1},
         2:  {"tom": "red", "part":"yellow", "brow_id":2},

What's wrong?

7
  • 1
    later you have a mixed reuslt of single objects and arrays with objects. Commented Mar 29, 2018 at 19:28
  • if u notice , newly formed array u saying is just an object . Commented Mar 29, 2018 at 19:31
  • @NinaScholz I just want a multi-dimensional array in index 45, doesn't matter if it's an object or array . Is there any chance to work by assigning as my code does Commented Mar 29, 2018 at 19:37
  • Curiosity, why was accepted other answer and not mine? Just to know. Commented Mar 29, 2018 at 19:56
  • @Ele because array.reduce doesn't work in my code. Nina's code worked Commented Mar 29, 2018 at 20:21

3 Answers 3

1

You could check if the property is given and assign the element, otherwise check for array and if not create an array. Later push the element.

var array = [{ tom: "red", part: "green", brow_id: 45 }, { tom: "red", part: "yellow", brow_id: 1 }, { tom: "red", part: "yellow", brow_id: 2 }, { tom: "maroon", part: "cyan", brow_id: 45 }],
    object = {};

array.forEach(function(elem) {
      if (!object[elem.brow_id]) {
          object[elem.brow_id] = elem;
          return;
      }
      if (!Array.isArray(object[elem.brow_id])) {
          object[elem.brow_id] = [object[elem.brow_id]];
      }
      object[elem.brow_id].push(elem);
});

console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

1

You can use the function reduce to group and build the desired output.

var array = [{    "tom": "red",    "part": "green",    "brow_id": 45  },  {    "tom": "red",    "part": "yellow",    "brow_id": 1  },  {    "tom": "red",    "part": "yellow",    "brow_id": 2  },  {    "tom": "maroon",    "part": "cyan",    "brow_id": 45  }];

var result = array.reduce((a, elem) => {
  if (a[elem.brow_id]) {
    if (Array.isArray(a[elem.brow_id])) a[elem.brow_id].push(elem);
    else a[elem.brow_id] = [elem, a[elem.brow_id]];
  } else a[elem.brow_id] = elem; 
  
  return a;
}, {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Using your approach with forEach with a fix to check already added elements to the new object.

var array = [{    "tom": "red",    "part": "green",    "brow_id": 45  },  {    "tom": "red",    "part": "yellow",    "brow_id": 1  },  {    "tom": "red",    "part": "yellow",    "brow_id": 2  },  {    "tom": "maroon",    "part": "cyan",    "brow_id": 45  }];

var newarray = {};

array.forEach(function(elem) {
  if (newarray[elem.brow_id]) {
    if (Array.isArray(newarray[elem.brow_id])) newarray[elem.brow_id].push(elem);
    else newarray[elem.brow_id] = [elem, newarray[elem.brow_id]];
  } else newarray[elem.brow_id] = elem;  
});

console.log(newarray);
.as-console-wrapper { max-height: 100% !important; top: 0; }

2 Comments

The updated code works, but is it possible to check if the newarray has only arrays inside it, and if there are single elements like "tom": "red", "part": "yellow", "brow_id": 1 assign it to have an array and 0th element will have "tom": "red", "part": "yellow", "brow_id": 1
@JohnDoe can you post that requirement within the question's body?
0

Grouping using Array.forEach():

var array = [{"tom":"red","part":"green","brow_id":45},{"tom":"red","part":"yellow","brow_id":1},{"tom":"red","part":"yellow","brow_id":2},{"tom":"maroon","part":"cyan","brow_id":45}];

var result = {};

array.forEach(function(o) {
  var key = o['brow_id'];
  
  result[key] = key in result ? [].concat(result[key], o) : o;
});

console.log(result);

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.