7

I have following object array:

var arr = [
  {
    id    : "a1",
    guid  : "sdfsfd",
    ...
    value : "abc",
    status: false
  },
  {
    id    : "a2",
    guid  : "sdfsfd",
    ...
    value : "def",
    status: true
  },
  ...
]

I have this object:

var obj = {
  id      : "a1",
  guid    : "sdfsfd",
  ...
  value   : "xyz",
  status  :  true
}

I need to replace the object in the array with this object where the "id" is same. So the resulting array will be:

var arr = [
  {
    id    : "a1",
    guid  : "sdfsfd",
    ...
    value : "xyz",
    status: true
  },
  {
    id    : "a2",
    guid  : "sdfsfd",
    ...
    value : "def",
    status: true
  },
  ...
]

Additionally I need to add this object to the array if an object with that id doesn't exists.

How to achieve this using minimal lodash code? Looking for something like

arr = _.merge_by_key(arr,obj,"id");
0

5 Answers 5

10

you can do it with _.unionBy

var res = _.unionBy([obj], arr, 'id');

but check a note at this comment

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

3 Comments

This is very creative, but may do a lot more work than needed. It will search through the rest of the items in the array to also remove duplicates.
@4castle i dont sure about "remove duplicates", can you show proof?
Observe the output in this JSFiddle.
7

You can use .findIndex()

var i = arr.findIndex(o => o.id === obj.id);
if (arr[i]) { arr[i] = obj } else { arr.push(obj) };

2 Comments

This is the best solution, as it doesn't require any external libraries. Thanks!
Very elegant approach +1
3

You can use _.findIndex with the _.matchesProperty shorthand:

var index = _.findIndex(arr, ['id', obj.id]);
arr[index >= 0 ? index : arr.length] = obj;

var arr = [{
  id: "a1",
  guid: "sdfsfd",
  value: "abc",
  status: false
}, {
  id: "a2",
  guid: "sdfsfd",
  value: "def",
  status: true
}];

var obj = {
  id      : "a1",
  guid    : "sdfsfd",
  value   : "xyz",
  status  :  true
};

var index = _.findIndex(arr, ['id', obj.id]);
arr[index >= 0 ? index : arr.length] = obj;
                        
console.log(arr);
.as-console-wrapper { min-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

Comments

2

Here's a lodash solution that uses keyBy to create an object wherein each item in the collection is represented by their id, set to override the new object or possibly add the new object and lastly values to get the array representation of the object.

var result = _(arr).keyBy('id').set(obj.id, obj).values().value();

var arr = [
  {
    id    : "a1",
    guid  : "sdfsfd",
    value : "abc",
    status: false
  },
  {
    id    : "a2",
    guid  : "sdfsfd",
    value : "def",
    status: true
  }
];

var obj = {
  id      : "a1",
  guid    : "sdfsfd",
  value   : "xyz",
  status  :  true
}

var result = _(arr).keyBy('id').set(obj.id, obj).values().value();

console.log(result);
body > div { min-height: 100%; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

Comments

1

you can loop and use 'splice' function:

var added = false;
for(var i=0;i<arr.length; i++){
   if(arr[i].id === obj.id){
        arr.splice(i,1,obj);
        added = true;
        break;
   }
}

if(!added) arr.push(obj);

EDIT: missed added condition

1 Comment

This won't add the object if the id wasn't found in the array.

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.