1

I have an array:

let ar = [
    {
         uid:1, 
         flat_no: 1
    },
    {
         uid:2,
         flat_no: 2
    },
    {
         uid:1,
         flat_no:3
    }
];

If uid are same then I want to remove duplicate uid and concatenate its flat_no. The output array should be like this:

[
     {
         uid:1, 
         flat_no: [1,3]
     },
     {
         uid:2,
         flat_no: 2
     }
];
cse
  • 4,066
  • 2
  • 20
  • 37
Abhi
  • 1,512
  • 2
  • 22
  • 46
  • 1
    The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Dec 06 '18 at 07:13
  • check this out https://stackoverflow.com/questions/53537577/loop-multiple-arrays-and-sum-the-values-of-same-keys/53537919#53537919 – Karan Dec 06 '18 at 07:15
  • 1
    What have you tried so far? – Jack Bashford Dec 06 '18 at 07:15

4 Answers4

5

You can use a combination of Array.reduce and Array.find.

If you find an existing item in your accumulator array, just update it's flat_no property, otherwise push it to the accumulator array.

let arr = [
  {
     uid: 1,
     flat_no: 1
  },
  {
     uid: 2,
     flat_no: 2
  },
  {
     uid: 1,
     flat_no: 3
  }
]

arr = arr.reduce((arr, item) => {
  const existing = arr.find(innerItem => innerItem.uid === item.uid)

  if (existing) {
    existing.flat_no = Array.isArray(existing.flat_no)
      ? existing.flat_no
      : [existing.flat_no]

    existing.flat_no.push(item.flat_no)
  } else {
    arr.push(item)
  }

  return arr
}, [])

console.log(arr)
nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
0

You can iterate over your array and fill an object (used as a hashmap here).

Once done, you extract the values to get your result.

let hashResult = {}
ar.forEach(element => {
    if (hashResult[element.uid] == undefined) {
        hashResult[element.uid] = { uid: element.uid, flat_no: [] }
    }
    hashResult[element.uid].flat_no.push(element.flat_no)
})

let result = Object.values(hashResult)
console.log(new Date(), result)
Eponyme Web
  • 966
  • 5
  • 10
0

You can do this in a concise way with a single Array.reduce and Object.values to match your desired output:

let data = [ { uid:1, flat_no: 1 }, { uid:2, flat_no: 2 }, { uid:1, flat_no:3 } ];

const result = data.reduce((r, {uid, flat_no}) => {
  r[uid] ? r[uid].flat_no = [r[uid].flat_no, flat_no] : r[uid] = {uid, flat_no}
  return r
}, {})

console.log(Object.values(result))
Akrion
  • 18,117
  • 1
  • 34
  • 54
-1

1)Reduce the initial array to an object which has uid as the key and the flat_no as the value.

2)Then run a map on the keys to convert it into an array of objects with uid and flat_no.

1) First Step Code

let ar = [{uid:1, flat_no: 1},{uid:2, flat_no: 2},{uid:1, flat_no:3}];
let outputObj = ar.reduce((outputObj,currObj,currIndex) => {
  let {uid,flat_no} = currObj
  if (outputObj[uid]) {
    outputObj[uid].push(flat_no)
  }
  else {
   outputObj[uid] = [flat_no]
  }
  return outputObj
},{})

2)

let finalOutput = Object.keys(outputObj).map(key => 
  ({uid:key,flat_no:outputObj[key]}))
console.log(finalOutput)
anuragb26
  • 1,467
  • 11
  • 14