1

I have a list of Cards :

"Cards": [
        {
          "BIN": "308103",
          "TIER": {
            "Nums": "1",
            "Phone": "111111111 *",
          }
        },
        {
          "BIN": "308103",
          "TIER": {
            "Nums": "2",
            "Phone": "0000000000",
          }
        },
        {
          "BIN": "308516",
          "TIER": {
            "Nums": "",
            "Phone": "00000000 *",
          }
        },
]

What i need to do is merge all cards having similair BIN into the attribute TIER of the first object in the list.

The requested output is :

"Cards": [
    {
      "BIN": "308103",
      "TIER": [
      {
        "Nums": "1",
        "Phone": "111111111 *",
      },
      {
        "Nums": "2",
        "Phone": "0000000000",
      }
    ],
  },
  {...}

I did it using combined loops into each other but i want a performance-friendly solution.

3
  • What is our definition of similar? This is important for the configuration and logic Commented Mar 19, 2021 at 16:50
  • 2
    If you have working code and you want something more efficient, SO is not the right place to ask and you might be better off on codereview.stackexchange.com - Stackoverflow is here to help you solve code problems, so if you have code that you tried to write for doing what you describe, and it doesn't work, you'll want to update your post to reflect that. Although given what you're asking for, I'd strongly recommend you search SO first, because you're not the first person to need to remap objects in an array, and answers for this definitely already exist on SO. Commented Mar 19, 2021 at 16:51
  • Combined loops are very performance friendly, why don't you show us. Commented Mar 19, 2021 at 17:04

2 Answers 2

1

You can group by BIN by destructing each item and reducing them by assigning them to a BIN key and then accessing the values of the map.

const data = {
  "Cards": [
    { "BIN": "308103", "TIER": { "Nums": "1", "Phone": "111111111 *" } },
    { "BIN": "308103", "TIER": { "Nums": "2", "Phone": "0000000000"  } },
    { "BIN": "308516", "TIER": { "Nums":  "", "Phone": "00000000 *"  } }
  ]
};

const groupedByBin = {
  Cards: Object.values(data.Cards.reduce((acc, { BIN, TIER: { Nums, Phone }}) =>
    ({ ...acc, [BIN] : { BIN, TIER: [...(acc[BIN]?.TIER ?? []), { Nums, Phone }] }}), {}))};

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

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

2 Comments

I get an "Unexpected token ." here : (acc[BIN]?.TIER ?? [])
@AbdallahJg If your browser does not support the nullish coalescing operator (??), replace them with a logical or (||).
0

let's say cardsArray was you original array.

const binMap = {};

cardsArray.forEach(card => {
    if(binMap[card.BIN])
        binMap[card.BIN].TIER.push(card.TIER);
    else
        binMap[card.BIN] = {
            BIN: card.BIN
            TIER: [ card.TIER ]
        };   
});
const finalArray = Object.values(binMap);

with finalArray being the desired output.

Note: Object.values is not accepted in all browsers, so if it doesn't work, you could do some additional steps:

const finalArray = [];
Object.keys(binMap).forEach(bin => finalArray.push(binMap[bin]));

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.