0

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.

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks!! I'm actually getting "id" attribute in the object. That's where I have the real challenge. Sorry I missed to post it in the first place. I edited my op
Works fine. Thank you sir.
1

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

Comments

1

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

Comments

1

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>

3 Comments

Theres no check here to only merge if the key matches the ID
@realseanp lodash is already doing the check. If undefined is passed in then _.merge will discard it.
Thanks!! I'm actually getting "id" attribute in the object. That's where I have the real challenge. Sorry I missed to post it in the first place.
0

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

3 Comments

@DV, if you are brave enough, tell me the reason of dv ;d
I am actually getting "id" attribute in the object. And with that.. the Array#forEach is giving weird responses
@SrikanthBandaru giving weird responses - it doesn't help at all. Provide more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.