我在这里编写了一些代码,以使用以下逻辑在数组上添加一个元素
如果该元素存在于数组中,则函数应从数组中删除该元素并返回没有给定元素的数组,否则应返回附加了给定元素的数组。
考虑到这一点,我在想是否有更好的方法来做到这一点,这是我为此编写的代码。
function filterArray(arr, obj, key) {
for (let i = 0; i < arr.length; i++) {
if (arr[i][key] === obj[key]) {
const newArr = [...arr]
newArr.splice(i, 1)
return newArr
}
}
return [...arr, obj]
}
const fruits = [
{ id: 1, fruit: "Apple" },
{ id: 2, fruit: "Banana" },
{ id: 3, fruit: "Pineapple" }
]
const removedBanana = filterArray(fruits, {
id: 3,
fruit: "Banana"
}, "fruit")
const addedStrawberry = filterArray(fruits, {
id: 4,
fruit: "Strawberry" …Run Code Online (Sandbox Code Playgroud)我试图隐藏标题,仅在一条路线中。
假设我有 3 条路线route1、route2和route3。
我有一个名为 的组件app-header。
app-header我希望当用户输入时隐藏该组件route1,并在其他 2 个路由中显示该组件。
我在 stackoverflow 上找到了一些主题,但没有一个对我有帮助=/
你们能给我一些帮助吗?
这是我的代码:
应用程序组件.ts
export class AppComponent implements OnInit {
showHeader = true;
constructor(
private router: Router
) {
this.router.events.subscribe(event => this.modifyHeader(event));
}
ngOnInit() {
}
modifyHeader(location) { // This method is called many times
console.log(location.url); // This prints a loot of routes on console
if (location.url === '/route1') {
this.showHeader = false;
} else {
this.showHeader = …Run Code Online (Sandbox Code Playgroud)我正在尝试在我的 NestJS 上使用 typeOrm 创建一个实体,但它没有按我预期的那样工作。
我有以下实体
@Entity('TableOne')
export class TableOneModel {
@PrimaryGeneratedColumn()
id: number
@PrimaryColumn()
tableTwoID: number
@PrimaryColumn()
tableThreeID: number
@CreateDateColumn()
createdAt?: Date
}
Run Code Online (Sandbox Code Playgroud)
此代码生成一个迁移,该迁移生成一个类似于下面示例的表
+--------------+-------------+------+-----+----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+----------------------+-------+
| id | int(11) | NO | | NULL | |
| tableTwoID | int(11) | NO | | NULL | |
| tableThreeID | int(11) | NO | | NULL | |
| createdAt | datetime(6) | NO | …Run Code Online (Sandbox Code Playgroud) 我在这里有一个疑问,我正在尝试将一个变量memcpy()复制string[9]到一个unsigned long long int变量,这里是代码:
unsigned char string[9] = "message";
string[8] = '\0';
unsigned long long int aux;
memcpy(&aux, string, 8);
printf("%llx\n", aux); // prints inverted data
/*
* expected: 6d65737361676565
* printed: 656567617373656d
*/
Run Code Online (Sandbox Code Playgroud)
如何在不反转数据的情况下制作此副本?
angular ×1
angular6 ×1
arrays ×1
c ×1
ecmascript-6 ×1
javascript ×1
memcpy ×1
nestjs ×1
node.js ×1
string.h ×1
typeorm ×1
typescript ×1