Q:

How to add two row of TableView depending on same id

First I am attaching an image of the scenario:

I wan to add the same subID and show them in a single row, adding their total as well. There should be any simple array append or map function, which I am unable to find. My cellForRowAt indexPath: looks like this: 
   var walletTransactionModel = [WalletTransactionModel]()
   ...

   let cell = tableView.dequeueReusableCell(withIdentifier: "walletCell", for: indexPath)
   let data = walletTransactionModel[indexPath.row - 1]
   let myNumber = doubleToIntWhenDecimalZero(number: data.amount)
   cell.textLabel?.text = "\(data.slug) | \(data.name) | \(myNumber)"
   return cell

And my simple model looks like this: 
import Foundation
import SwiftyJSON

class WalletTransactionModel {
var slug: String
var name: String
var amount: Double

required init(slug: String, name: String, amount: Double) {
    self.slug = slug
    self.name = name
    self.amount = amount
}

convenience init?(item: JSON) {
    guard let slug = item["slug"].string,
           let name = item["name"].string,
        let amount = item["amount"].double else { return nil}

    self.init(slug: slug, name: name, amount: amount)
}

}

Any help would be appreciated.

A:

I think the best thing to do is to get your data correct before you try to display it in the table view.  After you have loaded your data, run a processing step that reduces that data to the form you want.  
Something like this (copy paste to a playground):
import Foundation

class WalletTransactionModel {
    var slug: String
    var name: String
    var amount: Double

    required init(slug: String, name: String, amount: Double) {
        self.slug = slug
        self.name = name
        self.amount = amount
    }
}

var data = [WalletTransactionModel(slug: "subID1", name: "James", amount: 1),
            WalletTransactionModel(slug: "subID1", name: "James", amount: 2),
            WalletTransactionModel(slug: "subID2", name: "Sarah", amount: 10),
            WalletTransactionModel(slug: "subID2", name: "Sarah", amount: 20)]

var reducedData = data.reduce( [WalletTransactionModel]()) { (partial, model) in
    var partial = partial

    let matchingIndex = partial.firstIndex(where: { (existingModel) -> Bool in
        existingModel.slug == model.slug
    })

    if let matchingIndex = matchingIndex {
        partial[matchingIndex].amount += model.amount
    } else {
        partial.append(model)
    }

    return partial
}

for model in reducedData {
    print("\(model.slug) \(model.amount)")
}

You can probably do this a bit more efficiently, but hopefully it's clear what's going on there.  Basically it reduces your existing data, and on each partial result checks to see if the id has already been added to the new result.  If it has then the amounts are summed, otherwise it adds the model to the new result.
This will process your data, and create a new data array.  You can then use this as your table view data source, and you won't need to do anything in cellForRowAtIndex other than display the row.

