简体   繁体   中英

How to insert an object into array in javascript?

actions = [{ 'name' : 'Share',
  'icon' : 'fa fa-share-alt library_icon mylibrary-icon-right'
    },{
    'name' : 'Edit',
    'icon' : 'fa fa-pencil-square-o library_icon mylibrary-icon-right'
  },
  {
    'name' : 'Embed',
    'icon' : 'fa fa-link library_icon mylibrary-icon-right'
  },
  {
     'name' : 'Schedule',
     'icon' : 'fa fa-calendar library_icon mylibrary-icon-right'
  }
];

I have to remove the second object and insert it again at same position after passing a condition . Is there any way to do it ?

Just update the index (index start from 0) with new object

 const actions = [{ 'name' : 'Share', 'icon' : 'fa fa-share-alt library_icon mylibrary-icon-right' },{ 'name' : 'Edit', 'icon' : 'fa fa-pencil-square-o library_icon mylibrary-icon-right' }, { 'name' : 'Embed', 'icon' : 'fa fa-link library_icon mylibrary-icon-right' }, { 'name' : 'Schedule', 'icon' : 'fa fa-calendar library_icon mylibrary-icon-right' } ]; function replace(index,object){ const array = actions; array[index] = object; return array } console.log(replace(2,{new:'one'}))

Maybe you are looking for splice , that can remove any number of elements in the array, and insert any number of elements at a specified index.

 const actions = [ { 'name' : 'Share', 'icon' : 'fa fa-share-alt library_icon mylibrary-icon-right'}, { 'name' : 'Edit', 'icon' : 'fa fa-pencil-square-o library_icon mylibrary-icon-right' }, { 'name' : 'Embed', 'icon' : 'fa fa-link library_icon mylibrary-icon-right' }, { 'name' : 'Schedule', 'icon' : 'fa fa-calendar library_icon mylibrary-icon-right' } ]; const saved = actions.splice(1,1); // At position 1 (remember it is zero based), remove 1 element console.log("Test1", actions); console.log("Removed", saved); actions.splice(1,0,...saved); // At position 1, remove 0, insert all saved elements console.log("Test2", actions);

I assume that you are using this for some menu, and another approach would be to save the original array and filter it for the new condition, and then throw away the filtered array when it is no longer needed.

 const actions = [ { 'name' : 'Share', 'icon' : 'fa fa-share-alt library_icon mylibrary-icon-right'}, { 'name' : 'Edit', 'icon' : 'fa fa-pencil-square-o library_icon mylibrary-icon-right' }, { 'name' : 'Embed', 'icon' : 'fa fa-link library_icon mylibrary-icon-right' }, { 'name' : 'Schedule', 'icon' : 'fa fa-calendar library_icon mylibrary-icon-right' } ]; const filtered = actions.filter( item => item.name != "Edit"); console.log("Filtered", filtered);

Use splice(). To remove element at position 1:

actions.splice(1,1);

To add the item at position 1:

actions.splice(1,0,itemToAdd);

You can add an object to an array using push .

example:

 const actions = [{ 'name': 'Share', 'icon': 'fa fa-share-alt library_icon mylibrary-icon-right' }, { 'name': 'Edit', 'icon': 'fa fa-pencil-square-o library_icon mylibrary-icon-right' }, { 'name': 'Embed', 'icon': 'fa fa-link library_icon mylibrary-icon-right' }, { 'name': 'Schedule', 'icon': 'fa fa-calendar library_icon mylibrary-icon-right' } ]; var newObj = { name: 'thing', icon: 'smiley face' } actions.push(newObj) console.log(actions)

if you want to remove the second object, then add it at the end, you could do so using filter or splice:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

 const actions = [{ 'name': 'Share', 'icon': 'fa fa-share-alt library_icon mylibrary-icon-right' }, { 'name': 'Edit', 'icon': 'fa fa-pencil-square-o library_icon mylibrary-icon-right' }, { 'name': 'Embed', 'icon': 'fa fa-link library_icon mylibrary-icon-right' }, { 'name': 'Schedule', 'icon': 'fa fa-calendar library_icon mylibrary-icon-right' } ]; var secondObj = actions[1] actions.push(secondObj) // adds secondObj to end of array actions.splice(1, 1) // removes one elment from index 1 console.log(actions)

You can remove object at certain index keep this index and add at this index later on here is how. Here is more about splice

 var action = [{ 'name' : 'Share', 'icon' : 'fa fa-share-alt library_icon mylibrary-icon-right' },{ 'name' : 'Edit', 'icon' : 'fa fa-pencil-square-o library_icon mylibrary-icon-right' }, { 'name' : 'Embed', 'icon' : 'fa fa-link library_icon mylibrary-icon-right' }, { 'name' : 'Schedule', 'icon' : 'fa fa-calendar library_icon mylibrary-icon-right' } ]; var idx = -1; var item = {"foo":"bar"}; // first step to remove element matching by condition action.forEach(function (el,index){ if("Edit" == el.name ){ action.splice(index,1); idx=index; item = el; return ; } }) console.log(`items before addition `,action); // step 2 is inserting element at deleted index action.splice(idx, 0, item) console.log(`items after addition `,action);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM