I am using Lodash for combining two arrays to return new values,
example,
var lastValue = [{"id": 5, "view": {"id": 2932, "name": "Available"}, "rowAttribute": "last value 1"}, {"id": 6, "view": {"id": 2933, "name": "Active"}, "rowAttribute": "last value 2"}]
var updatedValue = [{"view": {"id": 2932, "name": "Available"}, "rowAttribute": "updated value 1"}, {"view": {"id": 2934, "name": "inactive"}, "rowAttribute": "updated value 2"}]
I am using unionBy like,
_.unionBy(lastValue, updatedValue, 'view.id');
and I am getting like,
[{"view": {"id": 2932, "name": "Available"}, "rowAttribute": "updated value 1"}, {"id": 6, "view": {"id": 2933, "name": "Active"}, "rowAttribute": "last value 2"}, {"view": {"id": 2934, "name": "inactive"}, "rowAttribute": "updated value 2"}]
My expectation is like,
[
{
"id": 5,
"view": {
"id": 2932,
"name": "Available"
},
"rowAttribute": "updated value 1"
},
{
"id": 6,
"view": {
"id": 2933,
"name": "Active"
},
"rowAttribute": "last value 2"
},
{
"view": {
"id": 2934,
"name": "inactive"
},
"rowAttribute": "updated value 2"
}
]
I want combined values of both the array. if view.id is/are available in updatedValue then it should return with updated value with Id else same from both the arrays.
You can simply use Object.assign to get the desired result
var lastValue = [
{ id: 5, view: { id: 2932, name: "Available" }, rowAttribute: "last value" },
];
var updatedValue = [
{ view: { id: 2932, name: "Available" }, rowAttribute: "updated value" },
];
const result = lastValue.map((o, i) => Object.assign({}, o, updatedValue[i]));
console.log(result);
If you want to update the object with view.id
var lastValue = [
{ id: 5, view: { id: 2932, name: "Available" }, rowAttribute: "last value" },
];
var updatedValue = [
{ view: { id: 2932, name: "Available" }, rowAttribute: "updated value" },
];
const result = lastValue.map((o, i) =>
Object.assign(
{},
o,
updatedValue.find((obj) => obj.view.id === o.view.id)
)
);
console.log(result);
You can even optimise the result using Map as
var lastValue = [
{ id: 5, view: { id: 2932, name: "Available" }, rowAttribute: "last value" },
];
var updatedValue = [
{ view: { id: 2932, name: "Available" }, rowAttribute: "updated value" },
];
const dict = new Map(updatedValue.map((o) => [o.view.id, o]));
const result = lastValue.map((o, i) => Object.assign({}, o, dict.get(o.view.id)));
console.log(result);
Updated#4 If you want to join the array then you have to use find and Object.assign
var lastValue = [
{
id: 5,
view: { id: 2932, name: "Available" },
rowAttribute: "last value 1",
},
{ id: 6, view: { id: 2933, name: "Active" }, rowAttribute: "last value 2" },
];
var updatedValue = [
{ view: { id: 2932, name: "Available" }, rowAttribute: "updated value 1" },
{ view: { id: 2934, name: "inactive" }, rowAttribute: "updated value 2" },
];
updatedValue.forEach((o) => {
const objInLastValue = lastValue.find((obj) => obj.view.id === o.view.id);
if (objInLastValue) Object.assign(objInLastValue, o);
else lastValue.push(o);
});
console.log(lastValue);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }