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

时间:2017-04-20 23:12:42

标签: javascript arrays lodash

我有

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"
            }
        };

我想要

  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
            }];

我在做

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

我在这里缺少什么?

P.S。 Noob在这里。

提前致谢。

================= 编辑: 在const对象中添加了“id”属性。

5 个答案:

答案 0 :(得分:1)

您可以使用.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);

答案 1 :(得分:1)

不需要_.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>

答案 2 :(得分:1)

你可以这样做:

// 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);

答案 3 :(得分:1)

我会用以下方式做到这一点:

&#13;
&#13;
destination_location
&#13;
&#13;
&#13;

答案 4 :(得分:0)

使用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);