2

I have this object:

a = {"formData": {
    "total": "60.00",
    "tr0_RTN": "PZH",
    "tr0_amount": "10.00",
    "tr2_RTN": "SUB",
    "tr2_amount": "30.00",
    "tr7_RTN": "KFC",
    "tr7_amount": "20.00",
}};

How can I change the key sequence to this:

a = {"formData": {
    "total": "60.00",
    "tr0_RTN": "PZH",
    "tr0_amount": "10.00",
    "tr1_RTN": "SUB",
    "tr1_amount": "30.00",
    "tr2_RTN": "KFC",
    "tr2_amount": "20.00",
}};

Anyway I could do this with native or lodash?

sg552
  • 1,365
  • 4
  • 26
  • 53
  • If this comes in as JSON string then you can use string replace method. If comes in JS object then you have to create new properties based on old one's values and delete the old properties afterwards. – Redu Aug 27 '16 at 15:57
  • Object. I already removed part of the object data with lodash and need to rearrange it. – sg552 Aug 27 '16 at 16:03

3 Answers3

1

I imagine you're having a form with tr rows, and you'd like to rearrange them. As the indeces are pretty much random, you can use this function to organize/reindex them.

var keys = Object.keys(a["formData"]);
var formData = {};
var index = 0;

for (var i = 0; i < keys.length; i++) {
    var key = keys[i];
    if (key.indexOf('tr') == 0) {
        if (key.indexOf('_RTN') >= 0) {
            key = 'tr' + index + '_RTN';
        } else if (key.indexOf('_amount') >= 0) {
            key = 'tr' + index + '_amount';
        }
        if ((formData.hasOwnProperty('tr' + index + '_RTN')
            || formData.hasOwnProperty('tr' + index + '_amount'))
            && !formData.hasOwnProperty(key)) {
            index++;
        }
    }
    formData[key] = a["formData"][keys[i]];
}

a["formData"] = formData;

This will rebuild your object with altered keys for the respective _RTN and _amount items.

Stefan Wittwer
  • 773
  • 6
  • 16
1

You could get all relevant keys, look for the parts and build new consecutive numbers.

Then assign the old values to the new properties and delete the old keys.

 old key       new key                      action
----------  ----------  --------------------------------------------
tr0_RTN     tr0_RTN     no, because old key and new key are the same
tr0_amount  tr0_amount  same as above
tr2_RTN     tr1_RTN     create new key, delete old key
tr2_amount  tr1_amount  same
tr7_RTN     tr2_RTN     same
tr7_amount  tr2_amount  same

var a = { "formData": { "total": "60.00", "tr0_RTN": "PZH", "tr0_amount": "10.00", "tr2_RTN": "SUB", "tr2_amount": "30.00", "tr7_RTN": "KFC", "tr7_amount": "20.00", } };

Object.keys(a.formData).filter(function (k) {
    return k.indexOf('tr') === 0;
}).sort(function (a, b) {
    return a.match(/\d+/) - b.match(/\d+/);
}).forEach(function (k) {
    var m = k.match(/(\d+)_(.*)/),
        kk;
    if (m[1] !== this.last) {
        this.counter++;
        this.last = m[1];
    }
    kk = 'tr' + this.counter + '_' + m[2];
    if (k !== kk) {
        a.formData[kk] = a.formData[k];
        delete a.formData[k];
    }
}, { counter: -1, last: '' });

console.log(a);
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324
1

You may do as follows;

var   data = {"formData": {      "total": "60.00",
                               "tr0_RTN": "PZH",
                            "tr0_amount": "10.00",
                               "tr2_RTN": "SUB",
                            "tr2_amount": "30.00",
                               "tr7_RTN": "KFC",
                            "tr7_amount": "20.00",
                          }
             },
propsToDel = ["tr2_RTN", "tr2_amount", "tr7_RTN", "tr7_amount"],
propsToIns = ["tr1_RTN", "tr1_amount", "tr2_RTN", "tr2_amount"],
   newData = propsToIns.reduce((o,p,i) => { o.formData[p] = o.formData[propsToDel[i]];
                                            delete o.formData[propsToDel[i]];
                                            return o;
                                          },data);
console.log(newData);
Redu
  • 19,106
  • 4
  • 44
  • 59