Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Typescript数组-拼接&;插入_Arrays_Angular_Typescript - Fatal编程技术网

Arrays Typescript数组-拼接&;插入

Arrays Typescript数组-拼接&;插入,arrays,angular,typescript,Arrays,Angular,Typescript,我这里有3个通道阵列 currentPickSelection: Array<channel>; draggedChannel: channel; droppedChannel: channel; 这是我声明通道模型的方式: export class CompChannel { constructor(public compChannelCbsCode: string, public compChannelName: string, pu

我这里有3个通道阵列

currentPickSelection: Array<channel>;
draggedChannel: channel;
droppedChannel: channel;
这是我声明通道模型的方式:

export class CompChannel {
    constructor(public compChannelCbsCode: string, 
        public compChannelName: string, 
        public compChannelLogo: string) {}
}

export class channel {
    public pickCode: string;
    public cbsCode: string;
    public channel: string;
    public logo: string;
    public compChannel: CompChannel[];

    constructor(pickCode: string, cbsCode: string, channel: string,
        logo: string, compChannel: CompChannel[]) {
        this.pickCode = pickCode;
        this.cbsCode = cbsCode;
        this.channel = channel;
        this.logo = logo;
        this.compChannel = compChannel;
    }
}

请告诉我出了什么问题

droppedChannel对象和在
currentPickSelection
中找到的项目可能不完全相同

尝试与
findIndex
方法中的唯一值(如
pickCode
cbsCode
)进行比较,而不是与整个对象进行比较

let currentPickSelection: any[] = [
 { id: 1, label: 'Test1' },
 { id: 2, label: 'Test2' },
 { id: 3, label: 'Test3' }
];
let draggedChannel: any = { id: 5, label: 'Test5' };
let droppedChannel: any = { id: 3, label: 'Test3' };
let index = currentPickSelection.findIndex(item => {
   return item.id == droppedChannel.id; //use unique value instead of item object
});
if (index > -1) 
 currentPickSelection.splice(index, 1, draggedChannel);

您正在将一个通道与一组通道进行比较。给定变量的名称,draggedChannel和droppedChannel应该是一个通道,而不是一个通道数组。draggedChannel和droppedChannel,它们只是单个通道,但currentPickSelection数组是一个通道数组。那你为什么要声明draggedChannel和droppedChannel为数组呢?这是droppedChannel的输出{pickCode:“785”,cbsCode:“喜剧”,Channel:“喜剧网络”,logo:“/assets/Cometic Network.jpg”,compChannel:Array(1)},这是droppedChannel的输出{pickCode:“800”,cbsCode:“SHCASE”,频道:“Showcase”,徽标:“/assets/Showcase.png”,compChannel:Array(1)}如果声明:
draggedChannel:channel,它是否有效;下降通道:通道,如@JBNizet所建议?
let currentPickSelection: any[] = [
 { id: 1, label: 'Test1' },
 { id: 2, label: 'Test2' },
 { id: 3, label: 'Test3' }
];
let draggedChannel: any = { id: 5, label: 'Test5' };
let droppedChannel: any = { id: 3, label: 'Test3' };
let index = currentPickSelection.findIndex(item => {
   return item.id == droppedChannel.id; //use unique value instead of item object
});
if (index > -1) 
 currentPickSelection.splice(index, 1, draggedChannel);