0

I want to compare objects in array of objects based on a property value if property value of two objects match then I want to merge them my data looks like the below given array of objects.

[{
        "master": "filpkart",
        "business": [{
                "label": "flipkart process",
                "value": "flipkart.process"
            }
        ]
    }, {
        "master": "amazon",
        "business": [{
                "label": "amazon process",
                "value": "amazon.process"
            }
        ]
    }, {
        "master": "chat",
        "business": [{
                "label": "chat process",
                "value": "chat.process"
            }
        ]
    }, {
        "master": "flipkart",
        "business": [{
                "label": "flipkart1 process",
                "value": "flipkart1.process"
            }
        ]
    }
] 

after comparing the above arary of objects my output should look like this

[{
        "master": "filpkart",
        "business": [{
                "label": "flipkart process",
                "value": "flipkart.process"
            },
            {
                "label": "flipkart1 process",
                "value": "flipkart1.process"
            }
            
        ]
    }, {
        "master": "amazon",
        "business": [{
                "label": "amazon process",
                "value": "amazon.process"
            }
        ]
    }, {
        "master": "chat",
        "business": [{
                "label": "chat process",
                "value": "chat.process"
            }
        ]
    }
]

could anyone please tell me how can I achieve this

2
  • 1
    what does not work? please add your code. Commented Oct 22, 2020 at 7:04
  • @NinaScholz I have no idea how to approach this Commented Oct 22, 2020 at 7:10

3 Answers 3

3

Just play with Map:

const input = [{
  "master": "flipkart",
  "business": [{
          "label": "flipkart process",
          "value": "flipkart.process"
      }
  ]
}, {
  "master": "amazon",
  "business": [{
          "label": "amazon process",
          "value": "amazon.process"
      }
  ]
}, {
  "master": "chat",
  "business": [{
          "label": "chat process",
          "value": "chat.process"
      }
  ]
}, {
  "master": "flipkart",
  "business": [{
          "label": "flipkart1 process",
          "value": "flipkart1.process"
      }
  ]
}];

const result = Array.from(input.reduce((itemMap, value) => {
  if (itemMap.has(value.master)) {
    const business = itemMap.get(value.master).business.concat(value.business);
    itemMap.set(value.master, {...value, business });
  } else {
    itemMap.set(value.master, value);
  }

  return itemMap;
}, new Map()).values());

console.log(result);
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you have two master: "flipkart", you could take an object fro grouping and get the values as result set.

const
    data = [{ master: "flipkart", business: [{ label: "flipkart process", value: "flipkart.process" }] }, { master: "amazon", business: [{ label: "amazon process", value: "amazon.process" }] }, { master: "chat", business: [{ label: "chat process", value: "chat.process" }] }, { master: "flipkart", business: [{ label: "flipkart1 process", value: "flipkart1.process" }] }],
    grouped = Object.values(data.reduce((r, { master, business }) => {
        r[master] ??= { master, business: [] };
        r[master].business.push(...business);
        return r;
    }, {}));

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

Comments

1

You can use Map and forEach

let data = [{
  "master": "flipkart",
  "business": [{
    "label": "flipkart process",
    "value": "flipkart.process"
  }]
}, {
  "master": "amazon",
  "business": [{
    "label": "amazon process",
    "value": "amazon.process"
  }]
}, {
  "master": "chat",
  "business": [{
    "label": "chat process",
    "value": "chat.process"
  }]
}, {
  "master": "flipkart",
  "business": [{
    "label": "flipkart1 process",
    "value": "flipkart1.process"
  }]
}]
// initiate a new map 
let map = new Map();
data.forEach((item) => {
  // check if map have a key same as value of master in current object in iteration
  const mapData = map.get(item.master);
  // if not the set the key like flipkart, amazon..
  if (!mapData) {
    map.set(item.master, item)
  } else {
   // if already have the key the update the business array 
    map.get(item.master).business = map.get(item.master).business.concat(item.business)
  }

});
let formattedArr = []
// converting array of arrays 
for (let [k, v] of map.entries()) {
  formattedArr.push(v)

};
console.log(formattedArr)

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.