Home > Net >  Create a Map from an array of objects with a condition on array elements
Create a Map from an array of objects with a condition on array elements

Time:07-06

I have an array of objects which i receive from a db:

[{
    key: 'PRODUCT',
    item_key: 'ITEM_01',
    sequence: 1
  },
  {
    key: 'STORE',
    item_key: 'STORE_01',
    sequence: 2
  }
]

I want to create a Map from it but the condition is, i want the item_key to be the map key, and only the corresponding array element which matches the item_key will be the value of that map key. So the map looks like

{
  'ITEM_01' => [{
    key: 'PRODUCT',
    item_key: 'ITEM_01',
    sequence: 1
  }],
  'STORE_01' => [{
    key: 'STORE',
    item_key: 'STORE_01',
    sequence: 2
  }]
}

I've kept the value of map keys as an array because there can be more values matching the map key in future. How can i do this?

P.S. by Map i don't mean the array function map, but the JS/TS Map

CodePudding user response:

Use reduce to create an object, if the key exists in the object then push to the array, otherwise create an array and push the item:

const items = [{
    key: 'PRODUCT',
    item_key: 'ITEM_01',
    sequence: 1
  },
  {
    key: 'STORE',
    item_key: 'STORE_01',
    sequence: 2
  }
]

const result = items.reduce((obj, item) => {
  if (!obj.hasOwnProperty(item.item_key)) {
    obj[item.item_key] = [];
  }
  
  obj[item.item_key].push(item);

  return obj;
}, {});

console.log(result);

Here is an exmaple with Map:

const map = new Map();

const items = [{
    key: 'PRODUCT',
    item_key: 'ITEM_01',
    sequence: 1
  },
  {
    key: 'STORE',
    item_key: 'STORE_01',
    sequence: 2
  }
]

items.forEach(item => {
  if (map.has(item.item_key)) {
    map.get(item.item_key).push(item);
  } else {
    map.set(item.item_key, [item])
  }
});

console.log(Object.fromEntries(map))

CodePudding user response:

You can use the Map constructor

const arr = [{
    key: 'PRODUCT',
    item_key: 'ITEM_01',
    sequence: 1
  },
  {
    key: 'STORE',
    item_key: 'STORE_01',
    sequence: 2
  }
];

const myMap = new Map();
arr.forEach(obj => myMap.set(obj.item_key, obj));

console.log(Object.fromEntries(myMap));

CodePudding user response:

 /**
 * return the value of map keys as an array
 * @param list data from DB
 * @param map new map or any map you already have
 */
function saveToMap(list: any[], map: Map<string, any[]>) {
    list.forEach(obj => {
        if (map.has(obj.key)) {
            map.get(obj.key)?.push(obj)
        } else {
            map.set(obj.key, [obj])
        }
    })
    return [...map.keys()]
}

const list = [
    {
        key: "PRODUCT",
        item_key: "ITEM_01",
        sequence: 1,
    },
    {
        key: "STORE",
        item_key: "STORE_01",
        sequence: 2,
    },
]
// new map or any map you already have
const map = new Map()
saveToMap(list, map)

CodePudding user response:

You reduce it.

let arr =[{
    key: 'PRODUCT',
    item_key: 'ITEM_01',
    sequence: 1
  },
  {
    key: 'STORE',
    item_key: 'STORE_01',
    sequence: 2
  }
]

let result = arr.reduce((prev, curr) => {
   prev[curr.item_key] = curr
   return prev;
}, {})

console.log(result);

  • Related