I am working with a huge array of objects. Through filtering, I have shrunk the data set. I need to add an array of properties to each object based on properties in another "lookup" object. My initial array of objects looks like so.
const arr = [{id: "12345", type: "square", current: "0", max: "1", code: "50"},
{id: "23456", type: "square", current: "0", max: "3", code: "50"},
{id: "54321", type: "square", current: "2", max: "4", code: "50"},
{id: "54321", type: "circle", current: "0", max: "1", code: "100"},
{id: "65432", type: "circle", current: "3", max: "3", code: "100"},
{id: "76543", type: "circle", current: "0", max: "2", code: "100"}]
I have a "lookup" object. The basis of the object is for each type and code there is a level 0 to 3. For each object above, I need to add the title property for each id that matches type and code from current to max. For this example I simplified the "lookup" object.
const lookup = [{title: "yes", code: "50", level: "0", type: "square"},
{title: "no", code: "50", level: "1", type: "square"},
{title: "maybe", code: "50", level: "2", type: "square"},
{title: "sure", code: "50", level: "3", type: "square"},
{title: "up", code: "100", level: "0", type: "circle"},
{title: "down", code: "100", level: "1", type: "circle"},
{title: "left", code: "100", level: "2"}, type: "circle"},
{title: "right", code: "100", level: "3"}, type: "circle"}]
So my resulting array of objects would look something like so
const result = [{id: "12345", type: "square", current: "0", max: "1", code: "50", titles: ["yes", "no"]},
{id: "23456", type: "square", current: "0", max: "3", code: "50", titles: ["yes", "no", "maybe", "sure"]},
{id: "54321", type: "square", current: "2", max: "4", code: "50", titles: ["maybe", "sure"]},
{id: "54321", type: "circle", current: "0", max: "2", code: "100", titles: ["up", "down"]},
{id: "65432", type: "circle", current: "3", max: "3", code: "100", titles: ["right"]},
{id: "76543", type: "circle", current: "0", max: "2", code: "100", titles: ["up", "down", "left"]}]
Is there a elegant way to do this without iterating over every single combination of type, code, and level?
lookupobject yourself or did get it from somewhere else? i.e. Can you restructure it in another way?lookupobject is being supplied to me, but it is static data so I could restructure it if needed.levelnumbers sparse? i.e Can there be level 1 and 3 without a level 2?max: 4for the third object, where there is only 3 levels? Do we have to care for this? What should be do in this case?