JavaScript数组中的对象插入问题[重复]

时间:2021-05-02 04:15:58

This question already has an answer here:

这个问题在这里已有答案:

I have a JavaScript array like below:

我有一个如下的JavaScript数组:

[
    {type: 'text', name: 'title', id: 'title', placeholder: 'Type here'},
    {type: 'textarea', name: 'description', id: 'description', placeholder: 'Type here'}
]

Now I want to inset {type: 'text', name: 'age', id: 'age', placeholder: 'Type here'} after first object. So my final result set will be looks like:

现在我想在第一个对象之后插入{type:'text',name:'age',id:'age',占位符:'Type here'}。所以我的最终结果集如下:

[
    {type: 'text', name: 'title', id: 'title', placeholder: 'Type here'},
    {type: 'text', name: 'age', id: 'age', placeholder: 'Type here'}
    {type: 'textarea', name: 'description', id: 'description', placeholder: 'Type here'}
]

I want in plain JavaScript or jQuery!

我想要简单的JavaScript或jQuery!

3 个解决方案

#1


5  

like this:

var a = [
    {type: 'text', name: 'title', id: 'title', placeholder: 'Type here'},
    {type: 'textarea', name: 'description', id: 'description', placeholder: 'Type here'}
]

var b= {type: 'text', name: 'age', id: 'age', placeholder: 'Type here'} 

a.splice(1,0,b);

console.log(a)

#2


4  

If your array is in variable array, then:

如果您的数组是变量数组,那么:

array.splice(1, 0, {
    type: 'text',
    name: 'age',
    id: 'age',
    placeholder: 'Type here'
});

The 1 means that you want to place it at index 1, 0 means you want to delete 0 items from the array. See splice documentation, it is quite the abomination of a method with 2 purposes of which both are never used at the same time :D

1意味着您要将其放在索引1,0处表示您要从数组中删除0个项目。参见拼接文档,它完全是一种方法的憎恶,有两个目的,它们都不会同时使用:D

#3


-1  

If you want that object at that exact position, use the splice method.

如果您希望该对象位于该确切位置,请使用拼接方法。

var myArray = [{
  type: 'text',
  name: 'title',
  id: 'title',
  placeholder: 'Type here'
}, {
  type: 'textarea',
  name: 'description',
  id: 'description',
  placeholder: 'Type here'
}];
var myObject = {
  type: 'text',
  name: 'age',
  id: 'age',
  placeholder: 'Type here'
};
myArray.push(myObject);

#1


5  

like this:

var a = [
    {type: 'text', name: 'title', id: 'title', placeholder: 'Type here'},
    {type: 'textarea', name: 'description', id: 'description', placeholder: 'Type here'}
]

var b= {type: 'text', name: 'age', id: 'age', placeholder: 'Type here'} 

a.splice(1,0,b);

console.log(a)

#2


4  

If your array is in variable array, then:

如果您的数组是变量数组,那么:

array.splice(1, 0, {
    type: 'text',
    name: 'age',
    id: 'age',
    placeholder: 'Type here'
});

The 1 means that you want to place it at index 1, 0 means you want to delete 0 items from the array. See splice documentation, it is quite the abomination of a method with 2 purposes of which both are never used at the same time :D

1意味着您要将其放在索引1,0处表示您要从数组中删除0个项目。参见拼接文档,它完全是一种方法的憎恶,有两个目的,它们都不会同时使用:D

#3


-1  

If you want that object at that exact position, use the splice method.

如果您希望该对象位于该确切位置,请使用拼接方法。

var myArray = [{
  type: 'text',
  name: 'title',
  id: 'title',
  placeholder: 'Type here'
}, {
  type: 'textarea',
  name: 'description',
  id: 'description',
  placeholder: 'Type here'
}];
var myObject = {
  type: 'text',
  name: 'age',
  id: 'age',
  placeholder: 'Type here'
};
myArray.push(myObject);