最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to push object in specified index position in array? - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Jan 23, 2018 at 8:02 dferenc 8,12612 gold badges44 silver badges52 bronze badges asked Jan 23, 2018 at 7:56 Soumya BeheraSoumya Behera 2,5125 gold badges17 silver badges25 bronze badges 3
  • 3 Possible duplicate of How to insert an item into an array at a specific index? – Tibrogargan Commented Jan 23, 2018 at 7:57
  • you can hide it...rather than deleting – Ashish sah 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) – Tibrogargan Commented Jan 23, 2018 at 8:00
Add a ment  | 

3 Answers 3

Reset to default 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);

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

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)

发布评论

评论列表(0)

  1. 暂无评论