js 原色插到数组最后第二位

js 原色插到数组最后第二位

怎么把b插入到a中,从a倒数第二个元素往后插入
a = [

{

name:'1'

},

{

name:'2'

},

{

name:'3'

}

]

b = [

 {

name:'4'

},

{

name:'5'

}

]


回答:

a.splice(-1, 0, ...b)

console.log(a)


回答:



a = [

{

name:'1'

},

{

name:'2'

},

{

name:'3'

}

]

b = [

{

name:'4'

},

{

name:'5'

}

]

a.slice(0,-2).concat(b.concat(a.slice(-2)))

/*

0: {name: "1"}

1: {name: "4"}

2: {name: "5"}

3: {name: "2"}

4: {name: "3"}

length: 5

*/

以上是 js 原色插到数组最后第二位 的全部内容, 来源链接: utcz.com/p/935589.html

回到顶部