如何在数组元素中添加数组 [英] How to add Array in Array element

查看:68
本文介绍了如何在数组元素中添加数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的 _assignedTripData 数组元素.

I had element of _assignedTripData Array like this.

0: {id: 100959872, cityCode: "PHX", airportID: "PHX", local: 0, guestID: 0, …}
1: {id: 100952759, cityCode: "PHX", airportID: "PHX", local: 0, guestID: 0, …}
2: {id: 100952761, cityCode: "PHX", airportID: "PHX", local: 0, guestID: 0, …}
3: {id: 100952766, cityCode: "PHX", airportID: "PHX", local: 0, guestID: 0, …}

但是当我使用 _assignedTripData.splice(0,1) 在 0 位置拼接元素并存储到 var newArray = new Array();之后我想使用 _assignedTripData.splice(0,0,newArray) 在相同位置插入相同的记录,最终输出将成为

But when I splice element at 0 Position using _assignedTripData.splice(0,1) and store into var newArray = new Array(); after that i want to insert same record at same position using _assignedTripData.splice(0,0,newArray) the final output will become is

只看到数组的0索引它是对象为什么?

Just See 0 index of array it is object why?

0: [{…}]
1: {id: 100952759, cityCode: "PHX", airportID: "PHX", local: 0, guestID: 0, …}
2: {id: 100952761, cityCode: "PHX", airportID: "PHX", local: 0, guestID: 0, …}
3: {id: 100952766, cityCode: "PHX", airportID: "PHX", local: 0, guestID: 0, …}

在 0 位置数组对象被添加,因为当我在第一次记录未定义显示时将 _assignedTripData 数据绑定到 tboday.

At 0 position array Object is added because of that when I bind _assignedTripData data to tboday at first record undefined show.

我的问题是如何删除 'x' 位置的数组并将它们添加到相同位置,以便数组对象结构不会改变.回复任何建议.我是 Jquery 的新手.

My question is that how to remove array at 'x' position and add them on same position so that array object structure does not change. Reply any Suggestion. I was new in Jquery.

推荐答案

来自 docsArray.splice 返回

包含已删除元素的数组.如果只删除一个元素,则返回一个包含一个元素的数组.如果没有删除任何元素,则返回一个空数组.

An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.

因此,您需要使用 Spread Syntax 达到预期的效果.

Hence, you need to use Spread Syntax to achieve the desired result.

let arr = [1,2];
let v = arr.splice(0,1);
arr.splice(0,0, ...v);
console.log(arr);

这篇关于如何在数组元素中添加数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆