const arr = [
'code1',
'code2',
'code3',
'code4',
'code5'
];
const data = [
{
device: 'code1'
}, {
device: 'code2'
} ];
const code = data.map((x: any) => x.device);
const sample = arr.filter((x: any) => code.indexOf(x) < 0);已经创建了两个数据。即设备code1和code2。
我想要做的是。在我这边有一个编辑。
例如,我有一个指向代码5的列表code1
然后我有两个数据,那就是code1和代码2。
当我尝试编辑数据时,代码为1。
在我的列表中,它应该显示code1,但不会显示code2。
当我尝试编辑code1时,应该是这样的:
[
'code1',
'code3',
'code4',
'code5'
]发布于 2020-04-03 11:57:56
添加非常量字符串以告知您当前正在编辑的内容。此外,如果您当前正在编辑的代码不是可行的代码,您可能需要添加异常处理来抛出和错误。
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
name = 'Angular';
public currentEdit: string = 'code1';
ngOnInit() {
const arr = [
'code1',
'code2',
'code3',
'code4',
'code5'
];
const data = [
{
device: 'code1'
},
{
device: 'code2'
}
];
const code = data.map((x: any) => x.device);
const sample = arr.filter((x: any) => code.indexOf(x) < 0 || x === this.currentEdit);
console.log(sample);
}
}发布于 2020-04-03 12:09:31
既然你知道要编辑哪一个,为什么你不能使用该信息并过滤掉arr。从‘@angular/ OnInit’导入{ Component,core};`
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
name = 'Angular';
ngOnInit() {
const arr = [
'code1',
'code2',
'code3',
'code4',
'code5'
];
const data = [
{
device: 'code1'
},
{
device: 'code2'
}
];
const toBeEdited = "code1";
let code = data.map((x: any) => x.device);
code = code.filter((data)=>(data!=toBeEdited))[0];
const sample = arr.filter((x: any) => code.indexOf(x) < 0);
console.log(sample);
}
}`
https://stackoverflow.com/questions/61004650
复制相似问题