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.

3
  • 3
    Possible duplicate of How to insert an item into an array at a specific index? Commented Jan 23, 2018 at 7:57
  • you can hide it...rather than deleting Commented Jan 23, 2018 at 7: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) Commented Jan 23, 2018 at 8:00

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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;
       }
    }
}

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.