2

I have an array.

var tableHeader = [
    {
        key: 1, 
        value: 'inputname', 
        defaultChecked: true, 
        columnName: 'input.name',
    }, {
        key: 3,
        value: 'callname',
        defaultChecked: true,
        columnName: 'call.name',
    }, {
        key: 4,
        value: 'rank',
        defaultChecked: true,
        columnName: 'call.rank',
    }, {
        key: 5,
        value: 'threshold',
        defaultChecked: true,
        columnName: 'call.threshold',
    }, {
        key: 9,
        value: 'matchname',
        defaultChecked: true,
        columnName: 'match.name',
    },
]

My requirement: I will remove the object having key 3. While I will push the same object to the array it will push to same position as before. The same will happen if I do for other objects.

dferenc
  • 7,918
  • 12
  • 41
  • 49
Soumya Behera
  • 2,325
  • 4
  • 15
  • 25
  • 3
    Possible duplicate of [How to insert an item into an array at a specific index?](https://stackoverflow.com/questions/586182/how-to-insert-an-item-into-an-array-at-a-specific-index) – Tibrogargan Jan 23 '18 at 07:57
  • you can hide it...rather than deleting – Ashish sah Jan 23 '18 at 07:59
  • cheap way to do this: `var tableHeader = { 1: { key: 1, ... }, 3: {key: 3, ... } }` (Obviously, `tableHeader` is now an object, not an array, so this may not work - depending on your other code) – Tibrogargan Jan 23 '18 at 08:00

3 Answers3

3

I just tried in Typescript I dnt know I much it helps to you,I added empty string in removed object place, after that I replaced with original object

let removedObj, removedIndex: any;
this.tableHeader.forEach((ele, i) => {
  if (ele.key == 3) {
    removedObj = ele; removedIndex = i;
    this.tableHeader.splice(i, 1, '');
  }
});
this.tableHeader.splice(removedIndex, 1, removedObj);
console.log(this.tableHeader);
Anil K
  • 81
  • 7
2

to replace the array element use:

TheNewObject = { key: 9,
                 value: 'matchname',
                 defaultChecked: true,
                 columnName: 'match.name',};

tableHeader[3] = TheNewObject; 

just like that , and to search for object Index you can use the following method :

function getObjectIndex(skey)
{
    for (i = 0; i < tableHeader.length; i++) {
       obj = tableHeader[i];
       if (obj.hasOwnProperty('key') && obj.key == skey) {
          return i;
       }
    }
}
Ahmad Alkaraki
  • 106
  • 1
  • 6
1

This looks like two distinct problems, one is to filter out an element inside an array by its property, and the second (and slightly trickier to push a new element back in the same place if it has the same key). I think your best bet is to leave .push alone, and instead look into Array.filter and Array.sort after you add a new element (to restore order), Like this:

var tableHeader = [{
  key: 1,
  value: 'inputname',
  defaultChecked: true,
  columnName: 'input.name',
}, {
  key: 3,
  value: 'callname',
  defaultChecked: true,
  columnName: 'call.name',
}, {
  key: 4,
  value: 'rank',
  defaultChecked: true,
  columnName: 'call.rank',
}, {
  key: 5,
  value: 'threshold',
  defaultChecked: true,
  columnName: 'call.threshold',
}, {
  key: 9,
  value: 'matchname',
  defaultChecked: true,
  columnName: 'match.name',
}, ]

console.log('Original', tableHeader)

//Filter out {key:3}
tableHeader = tableHeader.filter(function(e) {
  return e.key !== 3
})
console.log('Filtered', tableHeader)

tableHeader.push({
  key: 3,
  value: 'callname',
  defaultChecked: true,
  columnName: 'call.name',
})
tableHeader.sort(function(a, b) {
  return a.key - b.key
})

console.log('Resorted', tableHeader)
Jason Sperske
  • 29,816
  • 8
  • 73
  • 124