简体   繁体   English

如何向对象添加属性

[英]How to add a property to an object

I need to pass the exact data to the webAPI: 我需要将确切的数据传递给webAPI:

{
 email: "useremail",
 password: "userpassword",
 grant_type: "password"
}

From the login form, I receive the following data: 从登录表单中,我收到以下数据:

{
 email: "useremail",
 password: "userpassword"
}

I need to add the grant_type: "password" to the object. 我需要将grant_type:“ password”添加到对象。

Here is my method 这是我的方法

signIn(credentials) {

  console.log(credentials);

  this.authService.login(credentials)

    .subscribe(result => { 
      if (result)
        this.router.navigate(['/']);
      else  
        this.invalidLogin = true; 
    });
}

The credentials is the data coming from the Login form. 凭据是来自“登录”表单的数据。

If you have this: 如果您有这个:

let jsonObject = {
  email: "useremail",
  password: "userpassword",
}

I think you can simply do like so 我想你可以这样做

jsonObject['grant_type'] = password;

Otherwise, simple and effective: 否则,简单有效:

let extendedJsonObject = {
  email: jsonObject.email,
  password: jsonObject.password,
  grant_type: 'password'
};

As @jonrsharpe said in the comments, you can also do so: 正如@jonrsharpe在评论中所说,您也可以这样做:

let extendedJsonObject = { grant_type: 'password', ...jsonObject }

According to @Kyrsberg you can also do 根据@Kyrsberg,您也可以

let extendedJsonObject = Object.assign({grant_type: 'password'}, jsonObject);

What you appear to want to do is to add a property to an object. 您似乎想要做的是向对象添加属性。 This is one of the most basic operations in all of JavaScript . 这是所有JavaScript中最基本的操作之一 It is something that you would learn early on in any of the hundreds or thousands of tutorials, intros, beginner books, blog posts, and documentation pages that cover JavaScript. 您可以在涉及JavaScript的成百上千的教程,简介,初学者书籍,博客文章和文档页面中的任何一个开始就学到这一点。

I strongly suggest you go back and review whatever materials you were using to learn JavaScript. 我强烈建议您回顾一下学习JavaScript所用的任何材料。 For instance, you could start with this friendly intro to property accessors . 例如,您可以从属性访问器的友好介绍开始。

This has nothing to do with TypeScript or Angular. 这与TypeScript或Angular无关。 TypeScript is a type layer on top of JS. TypeScript是JS之上的类型层。 Angular is an application framework. Angular是一个应用程序框架。 Your question is purely about JavaScript. 您的问题纯粹是关于JavaScript。 It's a bit concerning that you thought this problem might have something to do with TypeScript or Angular. 有点担心您认为此问题可能与TypeScript或Angular有关。 It would indicate you don't actually understand the relationship among the tools you are using, namely JavaScript and TypeScript and Angular. 这表明您实际上并不了解所使用的工具之间的关系,即JavaScript和TypeScript和Angular。 Understand clearly that you are working with the language called JavaScript. 清楚地了解您正在使用JavaScript语言。 You must study it carefully and know it well before even starting to work with TypeScript or Angular. 在开始使用TypeScript或Angular之前,您必须仔细研究并充分了解它。

You also seem deeply confused about the meaning of the term "JSON". 您还对术语“ JSON”的含义深感困惑。 JSON is exactly one thing: a text-based format for exchanging information . JSON完全是一回事:一种用于交换信息的基于文本的格式 JavaScript objects are not JSON, although they resemble each other (hence the "J" of "JSON"), and can be converted back and forth ("parsing" and "stringifying"). JavaScript对象虽然彼此相似(因此是“ JSON”的“ J”),但它们不是 JSON,并且可以来回转换(“解析”和“字符串化”)。 Your problem has absolutely nothing to do with JSON. 您的问题与JSON完全无关。 The objects you are working with, which include properties such as email , are not JSON. 您正在使用的对象(包括诸如email属性) 不是 JSON。 If you insist on calling them JSON--and there is no shortage of similarly misguided folks trying to work in JavaScript--you will merely confuse yourself and those around you. 如果您坚持要用JSON命名-并且不乏尝试使用JavaScript的类似错误向导的人-您只会使自己和周围的人感到困惑。 These objects are plain, old, ordinary, JavaScript objects, and that's what you should call them. 这些对象是普通的,旧的,普通的JavaScript对象,这就是您应该调用的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM