我有两个对象数组,如下所示:
result = [{id:24, name:"xyz"}, {id:45,name:"tze"}]
moreDetails = [{id:24, name2:"hyi"}, {id:45, name2:"tikw"}]
Run Code Online (Sandbox Code Playgroud)
我想要一个上面这样的合并结果
mergedResult= [{id:24, name:"xyz", name2:"hyi"}, {id:45,name:"tze", name2:"tikw"}]
Run Code Online (Sandbox Code Playgroud)
请注意合并发生在id
,这两个数组都有。
我试图遵循这个How to merge these arrays/json objects? 而这个我如何动态合并两个 JavaScript 对象的属性?
但是,我想我迷路了,我的问题可能需要一个简短而简单的解决方案。
编辑
我试图简化我的例子。实际上,上面两个数组只是有一个id
共同点,它们不仅仅是name,name2。例如,有时result
数组将具有id, name, name2, name3
并且moreDetails
将具有id, name, name4
. 我想说的是,我并不总是提前知道两个数组除了id
. 因此,我不能像某些答案所建议的那样对字段名称进行硬编码。
你好,我正在尝试使用 Angular 来使用 redux。但当我尝试运行我的角度应用程序时,出现此错误消息:
ERROR in node_modules/@angular-redux/store/lib/src/components/ng-redux.d.ts(10,31): error TS2420: Class 'NgRedux<RootState>' incorrectly implements interface 'ObservableStore<RootState>'.
Types of property 'dispatch' are incompatible.
Type 'Dispatch<RootState>' is not assignable to type 'Dispatch<AnyAction>'.
Type 'RootState' is not assignable to type 'AnyAction'.
node_modules/@angular-redux/store/lib/src/components/ng-redux.d.ts(37,33): error TS2344: Type 'RootState' does not satisfy the constraint 'Action<any>'.
node_modules/@angular-redux/store/lib/src/components/root-store.d.ts(18,24): error TS2344: Type 'RootState' does not satisfy the constraint 'Action<any>'.
node_modules/redux-observable/index.d.ts(32,56): error TS2344: Type 'S' does not satisfy the constraint 'Dispatch<AnyAction>'.
Run Code Online (Sandbox Code Playgroud)
我的减速机:
export default function reducer(state= {
persones: [],
selectedPersone: {}, …
Run Code Online (Sandbox Code Playgroud) 我有一个上传功能,但出现以下错误:
“窗口”类型上不存在属性“文件”。类型“Window”上不存在属性“FileList”。类型“Window”上不存在属性“FileReader”。类型“EventTarget”上不存在属性“结果”。
我的组件
import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';
import { UploadService } from './upload.service';
import 'jquery-ui-bundle';
@Component({
selector: 'app-upload',
templateUrl: './upload.component.html',
styleUrls: ['./upload.component.css']
})
export class UploadComponent implements OnInit {
categories: any;
constructor( private uploadService: UploadService) { }
ngOnInit() {
$(document).ready(function() {
document.getElementById('pro-image').addEventListener('change', readImage, false);
$( ".preview-images-zone" ).sortable();
$(document).on('click', '.image-cancel', function() {
let no = $(this).data('no');
$(".preview-image.preview-show-"+no).remove();
});
});
var num = 1;
function readImage(event) {
if (window.File && window.FileList && window.FileReader) …
Run Code Online (Sandbox Code Playgroud) 我需要在不同的用户 URL 输入中提取目录和文件名。
一些例子包括:
我真正需要的是TOP_PROD_IMAGE
和WS-25612-BK_IMRO_1.jpg
文件名。
所以我需要考虑输入http://
orhttps://
或 just 的用户www
。所以我尝试使用,string.split('/')
但这显然不适用于所有情况。尽管//
在用户输入 http 的情况下是双重的,但有什么东西可以给我一个数组?谢谢!
我使用 keycloak 作为 simplesamlphp 身份提供者的身份代理,以便登录到 angular 应用程序。
keycloak 使用登录掩码正确重定向到身份提供者。登录后,身份提供者按预期重定向到 keycloak。不幸的是,我收到以下错误消息(作为 JSON):
{"error":"invalid_request","error_description":"Missing parameter: username"}
Run Code Online (Sandbox Code Playgroud)
我的 IdP 有一个用户,我的 keycloak 没有,因为我不想将用户另外存储在 keycloak 中。
我既不熟悉 SAML 也不熟悉 Keycloak,所以如果我需要提供任何其他信息,请告诉我。
我有一个角度材质输入。在大多数形式中,当我单击它时,我喜欢这种行为(让占位符位于输入上方)。但在某些情况下,我需要这个占位符消失。
另外,我需要控制不使用输入“上方”的一些空白。
1)可能吗?如何?
2)如果没有,你会怎么做仍然使用角度材料主题的主题样式(文本颜色,无边框,大小,...)?
不确定它有帮助,但这是一个我不想要它的示例(搜索文本):https://stackblitz.com/edit/angular-scgar8
尝试了打字稿中元素引用的点击事件,但不起作用。如何编写元素引用的点击事件。如果有人知道,请帮助找到解决方案。
应用程序组件.ts:
@ViewChild('testElem') el:ElementRef;
@ViewChild('testElem2') el2:ElementRef;
this.el.on('click',()=>{
alert("test");
});
Run Code Online (Sandbox Code Playgroud)
应用程序组件.html:
<div #el>testElem</div>
<div #el2>testElem2</div>
Run Code Online (Sandbox Code Playgroud) 考虑到我有以下代码:
private readonly postAction$ = new Subject();
postStream$ = this.postAction$.pipe(
exhaustMap(() => {
this.count ++;
console.log('fired')
return of('my other post');
}),
startWith(''),
exhaustMap(()=> {
this.count ++;
console.log('fired first')
return of('my post' + this.count);
})
)
Run Code Online (Sandbox Code Playgroud)
我使用async
管道在我的模板中订阅。
我没想到这会起作用,但控制台的输出是:
> fired first
Run Code Online (Sandbox Code Playgroud)
在我谈到.next()
这个postAction$
主题之前,第一个console.log('fired')
永远不会被调用。
RxJS 操作符的执行上下文是什么?它们是如何工作的?我原以为第一个排气图需要在运行其余操作符之前发出一个值。在 rxjs文档中找不到任何内容
我正在尝试创建一个计时器来从 0 开始计算时间。
但是,当我单击按钮时,时间不计算在内,有谁知道为什么?
如何以这种格式获取此计时器 {{hours}}: {{mminutes}}: {{sseconds}}?
谢谢!
我的代码
组件.ts
time: number = 0;
interval;
startTimer() {
this.interval = setInterval(() => {
if(this.time = 0) {
this.time++;
} else {
this.time= 0;
}
},1000)
}
pauseTimer() {
clearInterval(this.interval);
}
Run Code Online (Sandbox Code Playgroud) 当我的页面加载并在屏幕上显示响应文本时,我尝试向 API 发出 HTTP 请求。我正在使用 Angular 框架。
目前,当您按下按钮时,它会按照我的意愿工作。我想要按钮具有的确切功能,但它会在页面加载时自动发生。
//TypeScript
import { Component, OnInit } from '@angular/core';
import { Observable, Subscription } from 'rxjs';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
@Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {
posts: Observable<String[]>;
constructor(private http:HttpClient) {
}
public getPosts() {
this.posts = this.http.get<any[]>('https://jsonplaceholder.typicode.com/posts');
}
ngOnInit() {
}
}
Run Code Online (Sandbox Code Playgroud)
//TypeScript
import { Component, OnInit } from '@angular/core';
import { Observable, Subscription } from 'rxjs';
import { HttpClient, …
Run Code Online (Sandbox Code Playgroud) 我有一个名为Component1
. 它包含同一组件的多个嵌套实例,如下所示:
<div>
<ng-container *ngFor='let item of items'>
<component-1>
</ng-container>
</div>
Run Code Online (Sandbox Code Playgroud)
中的所有 5 个项目items
都被显示,所以我知道它们在那里。但是,当我尝试检索包含所有 5 个这样的实例的数组时Component1
:
@ViewChild(Component1) children: QueryList<Component1>;
Run Code Online (Sandbox Code Playgroud)
then children
,而不是包含一个Component1
s数组,只包含一个Component1
:5 个中的第一个。
有谁知道为什么会这样?
假设我有一个公开主题的服务:
import { Injectable } from "@angular/core";
import { BehaviorSubject } from "rxjs";
@Injectable()
export class StateService {
private mySubject = new BehaviorSubject<string>("test");
mySubject$ = this.mySubject.asObservable();
constructor() {}
updateSubject(value: string) {
this.mySubject.next(value);
}
get mySubject2$() {
return this.mySubject.asObservable();
}
}
Run Code Online (Sandbox Code Playgroud)
在我的组件中:
import { Component } from "@angular/core";
import { StateService } from "./state.service";
@Component({
//...
})
export class AppComponent {
name = this.state.mySubject$;
name2 = this.state.mySubject2$;
constructor(private state: StateService) {}
}
Run Code Online (Sandbox Code Playgroud)
在使用访问器返回主题或通过类上的属性直接访问主题方面,除了风格之外还有什么区别吗?
我正在尝试使用 Typescript 从 Angular 应用程序向 ASP .NET CORE API 发出请求。
当我发送请求时,它会在 SQL 数据库中搜索是否存在具有该值的行。
问题是当我尝试向何处发送请求Syntax = "C++"
或"C#"
仅发送此请求时:
/FindBySyntax?Syntax=C
Run Code Online (Sandbox Code Playgroud)
而不是/FindBySyntax?Syntax=C#
或/File/FindBySyntax?Syntax=C++
示例:如果我尝试发送“Javascript”它的工作,它会返回我所在位置的所有行Syntax="Javascript"
。
我想知道是否有办法在请求中发送“++”和“#”等特殊字符,我该怎么做?
/FindBySyntax?Syntax=C
Run Code Online (Sandbox Code Playgroud)