简体   繁体   English

如何使用 lodash 连接数组对象

[英]How to join array objects with lodash

I have two variables with two different array objects.我有两个带有两个不同数组对象的变量。 One is the collection(this.collection) object and the second is the user(this.user) object.一个是collection(this.collection)对象,第二个是user(this.user)对象。

I want to join this two objects into one by the UID.我想通过 UID 将这两个对象合二为一。 Here is the collection object :这是collection object

   {  
       "id":"d67de5QJ",
       "admins":[  
          "494949393"
       ],
       "color":"#029ae4",
       "components":{  
          "forums":false,
          "pages":true,
          "photos":false,
          "videos":false
       },
       "createdAt":"2018-02-09 14:38:59",
       "description":"Angular is a TypeS",
       "homepage":"pages",
       "photoURL":"https://firebase..",
       "status":"Public",
       "title":"Angular 5",
       "uid":"hlyAbEUfJhbxy",
       "updatedAt":"2018-02-09 14:38:59"
    }

And here is the user object这是user object

{
  "bio": "<strong>PROFILE NEEDS EDITING",
  "contactInfo": {
    ...
  },
  "createdAt": "2018-02-09 12:43:47",
 ,
  "email": "email@gmail.com",
  "avatar": "https://lh5.googleusercontent.com/-3LjYEmTIZlo/A...",
  "roles": {
    "admin": false,
    "dealer": false,
    "user": true
  },
  "status": "online",
  "uid": "hlyAbEUfJhbxy",
  "updatedAt": "2018-02-09 14:37:23",
}

How can I achieve this with LoDash or vanilla JavaScript?如何使用 LoDash 或 vanilla JavaScript 实现这一点? As you can see, there are other common properties like updatedAt and createdAd .正如你所看到的,也有像updatedAtcreatedAd其他公共属性。

I tried this but I get an empty object我试过这个,但我得到一个空对象

if(this.collection) {
          this._auth.getUser(this.collection.uid).subscribe((user) => {
            this.user = user;
            const col = unionBy(this.user, this.collection, this.collection.uid)
             console.log(col)
          });

Object.assign() would work for this. Object.assign() 可以解决这个问题。 Here you can find detail explanation. 在这里你可以找到详细的解释。

See fiddle here在这里看到小提琴

var col =   {  
       "id":"d67de5QJ",
       "admins":[  
          "494949393"
       ],
       "color":"#029ae4",
       "components":{  
          "forums":false,
          "pages":true,
          "photos":false,
          "videos":false
       },
       "createdAt":"2018-02-09 14:38:59",
       "description":"Angular is a TypeS",
       "homepage":"pages",
       "photoURL":"https://firebase..",
       "status":"Public",
       "title":"Angular 5",
       "uid":"hlyAbEUfJhbxy",
       "updatedAt":"2018-02-09 14:38:59"
    }

 var user = {
  "bio": "<strong>PROFILE NEEDS EDITING",
  "createdAt": "2018-02-09 12:43:47",

  "email": "email@gmail.com",
  "avatar": "https://lh5.googleusercontent.com/-3LjYEmTIZlo/A...",
  "roles": {
    "admin": false,
    "dealer": false,
    "user": true
  },
  "status": "online",
  "uid": "hlyAbEUfJhbxy",
  "updatedAt": "2018-02-09 14:37:23",
}

console.log(Object.assign(col,user))

You can use loadash merge : _.merge .您可以使用 loadash 合并: _.merge unlike Object.assign , _.merge even handles nested objects.Object.assign不同, _.merge . _.merge甚至可以处理嵌套对象。 Find the documentation and example here .此处查找文档和示例。

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

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