How can i merge the same elements into one line

Please help! I have array list I want merge this

var listItem = [
  { LAN_NO: 1, LAN_NAME: "LANE1", CASE_NO: "SC" },
  { LAN_NO: 1, LAN_NAME: "LANE1", CASE_NO: "SC" },
  { LAN_NO: 1, LAN_NAME: "LANE1", CASE_NO: "SC" },
  { LAN_NO: 1, LAN_NAME: "LANE1", CASE_NO: "HC" },
  { LAN_NO: 2, LAN_NAME: "LANE2", CASE_NO: "SC" },
  { LAN_NO: 2, LAN_NAME: "LANE2", CASE_NO: "SC" },
  { LAN_NO: 2, LAN_NAME: "LANE2", CASE_NO: "SC" },
  { LAN_NO: 2, LAN_NAME: "LANE2", CASE_NO: "HC" },
  { LAN_NO: 3, LAN_NAME: "LANE3", CASE_NO: "SC" },
  { LAN_NO: 3, LAN_NAME: "LANE3", CASE_NO: "SC" },
  { LAN_NO: 3, LAN_NAME: "LANE3", CASE_NO: "SC" },
  { LAN_NO: 3, LAN_NAME: "LANE3", CASE_NO: "HC" },
  { LAN_NO: 4, LAN_NAME: "LANE4", CASE_NO: "SC" },
  { LAN_NO: 4, LAN_NAME: "LANE4", CASE_NO: "SC" },
  { LAN_NO: 4, LAN_NAME: "LANE4", CASE_NO: "SC" },
  { LAN_NO: 4, LAN_NAME: "LANE4", CASE_NO: "HC" }
];

function removeDuplicates(arr, prop) {
  var obj = {};
  for (var i = 0, len = arr.length; i < len; i++) {
    if (!obj[arr[i][prop]]) {
      obj[arr[i][prop]] = arr[i];
    }
  }
  var newArr = [];
  for (var key in obj) {
    newArr.push(obj[key]);
  }
  return newArr;
}
removeDuplicates(listItem, CASE_NO);

to this

var listItem = [
  { LAN_NO: 1, LAN_NAME: "LANE1", CASE_NO: "3SC" },
  { LAN_NO: 1, LAN_NAME: "LANE1", CASE_NO: "HC" },
  { LAN_NO: 2, LAN_NAME: "LANE2", CASE_NO: "3SC" },
  { LAN_NO: 2, LAN_NAME: "LANE2", CASE_NO: "HC" },
  { LAN_NO: 3, LAN_NAME: "LANE3", CASE_NO: "3SC" },
  { LAN_NO: 3, LAN_NAME: "LANE3", CASE_NO: "HC" },
  { LAN_NO: 4, LAN_NAME: "LANE4", CASE_NO: "3SC" },
  { LAN_NO: 4, LAN_NAME: "LANE4", CASE_NO: "HC" }
];

How can i do that ?

how do you want to merge them?
where do you have the two arrays, they have the same name?

what have you tried to accomplish this?

1 Like

I hope you understand - i don’t know how to explain.

 const listItem = [
  { LAN_NO: 1, LAN_NAME: "LANE1", CASE_NO: "SC" },
  { LAN_NO: 1, LAN_NAME: "LANE1", CASE_NO: "SC" },
  { LAN_NO: 1, LAN_NAME: "LANE1", CASE_NO: "SC" },
  { LAN_NO: 1, LAN_NAME: "LANE1", CASE_NO: "HC" },
  { LAN_NO: 2, LAN_NAME: "LANE2", CASE_NO: "SC" },
  { LAN_NO: 2, LAN_NAME: "LANE2", CASE_NO: "SC" },
  { LAN_NO: 2, LAN_NAME: "LANE2", CASE_NO: "SC" },
  { LAN_NO: 2, LAN_NAME: "LANE2", CASE_NO: "HC" },
  { LAN_NO: 3, LAN_NAME: "LANE3", CASE_NO: "SC" },
  { LAN_NO: 3, LAN_NAME: "LANE3", CASE_NO: "SC" },
  { LAN_NO: 3, LAN_NAME: "LANE3", CASE_NO: "SC" },
  { LAN_NO: 3, LAN_NAME: "LANE3", CASE_NO: "HC" },
  { LAN_NO: 4, LAN_NAME: "LANE4", CASE_NO: "SC" },
  { LAN_NO: 4, LAN_NAME: "LANE4", CASE_NO: "SC" },
  { LAN_NO: 4, LAN_NAME: "LANE4", CASE_NO: "SC" },
  { LAN_NO: 4, LAN_NAME: "LANE4", CASE_NO: "HC" }
];

const reduced = listItem.reduce((acc, curr) => {
    curr = JSON.stringify(curr)
    acc[curr] ? acc[curr]++ : acc[curr] = 1;
    return acc;
}, {})

const mapped = Object.keys(reduced).map((el, index) => {
    el = JSON.parse(el);
    values = Object.values(reduced);
    return values[index] !== 1 ? {...el, CASE_NO: `${values[index]}${el['CASE_NO']}`} : {...el, CASE_NO: `${el['CASE_NO']}`}
})

console.log(mapped)

could have made it more concise though

1 Like

Hello @ilenia thanks for reply , Sorry my question not clearly . I want removing duplicate Object from array listItem but i want count and show into newArr with CASE_NO like this : CASE_NO : count_dupicate + “somthing like SC”

Hi @samolex! Thanks for your answer , This is solution i looking for

1 Like