1.1.19 • Published 2 years ago

merge-arrays-of-objects v1.1.19

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Merge arrays of objects

Workflow Status Coverage Status

Arrays, not objects are being merged. Objects with matching identifiers are replaced. Objects deep copies are placing into result array. To merge, you can select an object field as an identifier. Without identifier objects will be compared by md5 hash. You can use callback function to calc object identifier.

There are sync and async function versions.

Example 1

Simple arrays; no identifier.

const { arrMergeSync } = require('merge-arrays-of-objects');

let original = [1, 2, 3];
let update = [3, 4, 5];
let merged = arrMergeSync(original, update);

console.log(merged); 
// [1, 2, 3, 4, 5]

Example 2

Objects arrays; identifier as number; updating old values;

const { arrMergeSync } = require('merge-arrays-of-objects');

let original = [
  {id:1, value:'A'}, 
  {id:2, value:'B'}
];

let update = [
  {id:2, value:'C'}, 
  {id:3, value:'D'}
];

let merged = arrMergeSync(original, update, 'id');

console.log(merged);
// [ { id: 1, value: 'A' },
//   { id: 2, value: 'C' },
//   { id: 3, value: 'D' } ]

Example 3

Objects arrays; identifier as number; update object structure.

const { arrMergeSync } = require('merge-arrays-of-objects');

let original = [
  {id:1, value:'A'}, 
  {id:2, value:'B'}
];

let update = [
  {id:2}, 
  {id:3, value:'D'}
];

let merged = arrMergeSync(original, update, 'id');

console.log(merged); 
// [ { id:1, value:'A' }, 
//   { id:2 }, 
//   { id:3, value:'D' } ]

Example 4

Objects arrays; no identifiers.

const { arrMergeSync } = require('merge-arrays-of-objects');

let original = [
  {value:'A', text:'text-A'}, 
  {value:'B', key:42}
];

let update = [
  {value:'B', key:42}, 
  {value:'D'}
];

let merged = arrMergeSync(original, update);

console.log(merged); 
// [ { value:'A', text:'text-A' }, 
//   { value:'B', key:42 }, 
//   { value:'D'} ]

Example 5

Objects arrays; identifier as function.

const { arrMergeSync } = require('merge-arrays-of-objects');

let original = [
    {key:'A', text:'text-A', child: {key: 'A'}}, 
    {key:'A', text:'text-B', child: {key: 'B'}}
  ];
  
let update = [
    {key:'A', text:'text-C', child: {key: 'B'}}, 
    {key:'B', text:'text-D', child: {key: 'A'}}
  ];

function key(item) {
    return `${item.key}${item.child.key}`;
}  
  
let merged = arrMergeSync(original, update, key);

console.log(merged);
// [ { key: 'A', text: 'text-A', child: { key: 'A' } },
//   { key: 'A', text: 'text-C', child: { key: 'B' } },
//   { key: 'B', text: 'text-D', child: { key: 'A' } } ]

Example 6

Objects arrays; result array has objects copies.

const { arrMergeSync } = require('merge-arrays-of-objects');

let original = [
  {id:1, value:'A'},
  {id:2, value:'B'}
];

let update = [
  {id:2, value:'C'},
  {id:3, value:'D'}
];

let merged = arrMergeSync(original, update, 'id');

original[0].value = 'AA';
update[1].value = 'DD';

console.log(merged);
// [ { id: 1, value: 'A' },
//   { id: 2, value: 'C' },
//   { id: 3, value: 'D' } ]

Example 7

Objects arrays; async version.

const { arrMergeAsync } = require('merge-arrays-of-objects');

async function test () {
  try {
    let original = [
      {id: 1, value: 'A'},
      {id: 2, value: 'B'}
    ];

    let update = [
      {id: 2, value: 'C'},
      {id: 3, value: 'D'}
    ];

    let merged = await arrMergeAsync(original, update, 'id');
    console.log(merged);
  } catch (error) {
    console.error(error.message);
  }
}

test().then(()=>{});
// [ { id: 1, value: 'A' },
//   { id: 2, value: 'C' },
//   { id: 3, value: 'D' } ]
1.1.19

2 years ago

1.1.18

2 years ago

1.1.17

2 years ago

0.0.15

2 years ago

1.1.16

2 years ago

1.1.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago