如何在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'
}
];

我必须删除第二个对象,并在传递条件后再次将其插入同一位置。有什么办法吗?

也许您正在寻找拼接,它可以删除数组中任意数量的元素,并在指定索引处插入任意数量的元素。

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

我假设您将其用于某些菜单,另一种方法是保存原始数组并针对新条件对其进行过滤,然后在不再需要时丢弃过滤后的数组。

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

只需使用新对象更新索引(index start from 0)

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'}))

使用 splice((。要删除位置 1 处的元素:

actions.splice(1,1);

要将项目添加到位置 1:

actions.splice(1,0,itemToAdd);

您可以在某些index删除对象以保留此index并在此index添加,稍后将介绍如何。这是更多关于拼接的信息

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

您可以使用push将对象添加到数组中。

例:

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)

如果要删除第二个对象,请在末尾添加它,可以使用过滤器或拼接来执行此操作:

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)

相关内容

  • 没有找到相关文章

最新更新