3

I have an array of objects (dictionary is the term in Python but in this case I am using Javascript). What I am trying to do is to group everything in different objects. My first attempt is to create two arrays based on the Inventory values. Then for each of the two arrays, I want to create three different arrays based on the Objective values.

I think I have lost the logic as it doesn't work for the second grouping (Objective).

var inventory = [
    {ID: "K111", Objective: "One", Inventory: "Second" },
    {ID: "K112", Objective: "Two", Inventory: "Second" },
    {ID: "K113", Objective: "One", Inventory: "Second" },
    {ID: "K114", Objective: "Three", Inventory: "Second" },    
    {ID: "K132", Objective: "One", Inventory: "First" }
];

var OBJECTIVE = ["One", "Two", "Three"];
var INVENTORY = ["Second", "First"];

//Create arrays per Inventory (2 possible values)
for (var i = 0; i < INVENTORY.length; i++) {
 
  var variable = inventory.filter(function(el) {
      return el.Inventory == INVENTORY[i];
  });
  
  window['arr'+i] = variable;
  document.getElementById("arr"+i).innerHTML =JSON.stringify(variable);

}

//Create arrays per Objective for each array created above
for (var i = 0; i < OBJECTIVE.length; i++) {

  j = i + 2;
  var variable = arr0.filter(function(el) {
      return el.Objective == OBJECTIVE[i];
  });
  
  window['arr'+j] = JSON.stringify(variable);
  document.getElementById("arr"+j).innerHTML =JSON.stringify(variable);

}


for (var i = 0; i < OBJECTIVE.length; i++) {

  var variable = arr1.filter(function(el) {
      return el.Objective == OBJECTIVE[i];
  });
  
  window['arr'+j] = JSON.stringify(variable);
  document.getElementById("arr"+j).innerHTML =JSON.stringify(variable);

}
<div style="background:yellow;" id="arr0"></div>
<div style="background:green;" id="arr1"></div>
<div style="background:grey;" id="arr2"></div>
<div style="background:blue; color:white;" id="arr3"></div>
<div style="background:red; color:white;" id="arr4"></div>
<div style="background:black; color:white;" id="arr5"></div>
<div style="background:orange;" id="arr6"></div>

 

1 Answer 1

2

You could instead return one object using reduce method where you group first by Inventory and then by Objective. To return array of arrays as a result you can use Object.values with map.

var data = [{ID: "K111", Objective: "One", Inventory: "Second" },{ID: "K112", Objective: "Two", Inventory: "Second" },{ID: "K113", Objective: "One", Inventory: "Second" },{ID: "K114", Objective: "Three", Inventory: "Second" },    {ID: "K132", Objective: "One", Inventory: "First" }];

var res = data.reduce((r, e) => {
  let {Objective, Inventory} = e;
  r[Inventory] = r[Inventory] || {}
  r[Inventory][Objective] = r[Inventory][Objective] || {}
  Object.assign(r[Inventory][Objective], e);
  return r;
}, {})

var array = Object.values(res).map(Object.values)

console.log(array)
console.log(res)

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

4 Comments

@Nenan Vracar Nice one. One observation. K111 is missing because there is a duplicate I guess?
@Apolo Radomer yes.
Is there a way to alter the code and have it as well? I don't think there is. Maybe another function though without reduce. Is that right?
Yes. I will possibly make another question. I am trying to experiment. So, depending on the scenarios, I will build (innerHTML) tables with the body. Depending on a third object that doesn't exist in my query above, it will show/hide some other objects (number). So. I might have to aggregate the data too.

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.