Testaccount
Testaccount

Reputation: 2911

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?

P.S. Noob here.

Thanks in advance.

================= Edit: Added the "id" attribute in the const object.

Upvotes: 0

Views: 51

Answers (5)

guest271314
guest271314

Reputation: 1

You can use .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);

Upvotes: 1

ibrahim mahrir
ibrahim mahrir

Reputation: 31712

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:

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>

Upvotes: 1

Mamun
Mamun

Reputation: 68933

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

Upvotes: 1

pizzarob
pizzarob

Reputation: 12059

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

Upvotes: 1

kind user
kind user

Reputation: 41913

Possible solution using 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);

Upvotes: 0

Related Questions