简体   繁体   English

合并对象数组和对象对象-JavaScript

[英]Merge an array of objects and an object of objects - JavaScript

I have 我有

const arrayOfObjects = [{
            "id": 123, // Id I want to use to map
            "date": 20172301,
            "model": 2017
        },
        {
            "id": 221, // Id I want to use to map
            "date": 20172301,
            "model": 2015
        },
        {
            "id": 1, // Id I want to use to map
            "date": 20172301,
            "model": 2012
        }];

and

const object = {
            "123": { // Id I want to use to map
                "id": 1 // I am also getting this Id that I don't want
                "uri": "www.google.com"
            },
            "221": { // Id I want to use to map
                "id": 2 // I am also getting this Id that I don't want
                "uri": "www.bing.com"
            }
        };

I want 我想要

  result = [{
                "id": 123,
                "date": 20172301,
                "model": 2017,
                "uri": "www.google.com"
            },
            {
                "id": 221,
                "date": 20172301,
                "model": 2015,
                "uri": "www.bing.com"
            },
            {
                "id": 431,
                "date": 20172301,
                "model": 2012
            }];

I am doing 我在做

        const result = _.map(arrayOfObjects, (item) => _.merge(item, _.find(object, {'uri' : item.uri})));

What am I missing here? 我在这里想念什么?

PS Noob here. PS Noob在这里。

Thanks in advance. 提前致谢。

================= Edit: Added the "id" attribute in the const object. ================编辑:在const对象中添加了“ id”属性。

You can use .map() , Object.assign() 您可以使用.map()Object.assign()

 const arrayOfObjects = [{ "id": 123, // Id I want to use to map "date": 20172301, "model": 2017 }, { "id": 221, // Id I want to use to map "date": 20172301, "model": 2015 }, { "id": 1, // Id I want to use to map "date": 20172301, "model": 2012 }]; const object = { "123": { // Id I want to use to map "id": 1, // I am also getting this Id that I don't want "uri": "www.google.com" }, "221": { // Id I want to use to map "id": 2, // I am also getting this Id that I don't want "uri": "www.bing.com" } }; let result = arrayOfObjects.map(o => object[o.id] ? Object.assign({}, o, {url} = object[o.id]) : o); console.log(result); 

No need for _.find as you can get the item directly using item 's id, then use _.defaults instead of _.merge so the source's id won't override the target's id : 不需要_.find因为您可以直接使用item的ID获取该项目,然后使用_.defaults而不是_.merge这样源的id不会覆盖目标的id

const result = _.map(arrayOfObjects, (item) => _.defaults(item, object[item.id]));
//                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

 const arrayOfObjects = [{ "id": 123, "date": 20172301, "model": 2017 }, { "id": 221, "date": 20172301, "model": 2015 }, { "id": 431, "date": 20172301, "model": 2012 } ]; const object = { "123": { "uri": "www.google.com" }, "221": { "uri": "www.bing.com" } }; const result = _.map(arrayOfObjects, (item) => _.defaults(item, object[item.id])); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script> 

You could do this: 您可以这样做:

// use built in map method
const result = arrayOfObjects.map(item => {

  // get the uri for the current id
  const { uri } = object[item.id] || {};

  // if uri is not undefined add it to item
  if(uri) {
     item.uri = uri;
  }
  return item;
});

 const arrayOfObjects = [{ "id": 123, "date": 20172301, "model": 2017 }, { "id": 221, "date": 20172301, "model": 2015 }, { "id": 431, "date": 20172301, "model": 2012 } ]; const object = { "123": { "uri": "www.google.com" }, "221": { "uri": "www.bing.com" } }; const result = arrayOfObjects.map(item => { const { uri } = object[item.id] || {}; if (uri) { item.uri = uri; } return item; }); console.log(result); 

I will do that in the following way: 我将通过以下方式进行操作:

 const arrayOfObjects = [{ "id": 123, "date": 20172301, "model": 2017 }, { "id": 221, "date": 20172301, "model": 2015 }, { "id": 431, "date": 20172301, "model": 2012 }]; const object = { "123": { "uri": "www.google.com" }, "221": { "uri": "www.bing.com" } }; for(var k in object){ arrayOfObjects.forEach(function(val,i){ if(k==val.id) arrayOfObjects[i]['uri']=object[k].uri; }); } console.log(arrayOfObjects); 

Possible solution using Array#forEach . 使用Array#forEach可能解决方案。

 const arrayOfObjects = [{"id":123,"date":20172301,"model":2017},{"id":221,"date":20172301,"model":2015},{"id":431,"date":20172301,"model":2012}], object = {"123":{"uri":"www.google.com"},"221":{"uri":"www.bing.com"}}; Object.keys(object).forEach(v => arrayOfObjects.find(c => c.id == v).uri = object[v].uri); console.log(arrayOfObjects); 

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

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