0

I would like to assign the first object to the second object that have the same properties. What you suggest as quick method? Do I need to loop over the stores object separately?

1st object:

{
  title: 'test',
  description: 'desc',
  tags: ['tag1','tag2']
}

2nd object:

{
 title: '',
 description: '',
 tags: [],
 stores: [
    {
        id: 1,
        title: '',
        description: '',
        tags: '',
        template: '',
        link: '',
        active: true
    },
    {
        id: 2,
        title: '',
        description: '',
        tags: '',
        template: '',
        link: '',
        active: false
    }
 ]
}

Results:

{
 title: 'test',
 description: 'desc',
 tags: ['tag1', 'tag2'],
 stores: [
    {
        id: 1,
        title: 'test',
        description: 'desc',
        tags: '['tag1', 'tag2']',
        template: '',
        link: '',
        active: true
    },
    {
        id: 2,
        title: 'test',
        description: 'desc',
        tags: '['tag1', 'tag2']',
        template: '',
        link: '',
        active: false
    }
 ]
}
Med
  • 2,772
  • 1
  • 11
  • 14

3 Answers3

1

You could use object spread operator to shallow merge.

Try this.

let obj1 = {
    title: 'test',
    description: 'desc',
    tags: ['tag1', 'tag2']
};

let obj2 = {
    title: '',
    description: '',
    tags: [],
    stores: [
        {
            id: 1,
            title: '',
            description: '',
            tags: '',
            template: '',
            link: '',
            active: true
        },
        {
            id: 2,
            title: '',
            description: '',
            tags: '',
            template: '',
            link: '',
            active: false
        }
    ]
};

console.log({...obj2, ...obj1});

If you want to merge the deeply nested object then you could use the below approach.

let obj1 = {
    title: 'test',
    description: 'desc',
    tags: ['tag1', 'tag2']
};

let obj2 = {
    title: '',
    description: '',
    tags: [],
    stores: [
        {
            id: 1,
            title: '',
            description: '',
            tags: '',
            template: '',
            link: '',
            active: true
        },
        {
            id: 2,
            title: '',
            description: '',
            tags: '',
            template: '',
            link: '',
            active: false
        }
    ]
};

function isObject(item) {
    return (item && typeof item === 'object' && !Array.isArray(item));
}

function deepMerge(target, ...sources) {
    if (!sources.length) return target;
    const source = sources.shift();
    if (isObject(target) && isObject(source)) {
        for (const key in source) {
            if (isObject(source[key])) {
                if (!target[key]) Object.assign(target, { [key]: {} });
                mergeDeep(target[key], source[key]);
            } else {
                Object.assign(target, { [key]: source[key] });
            }
        }
    }
    return deepMerge(target, ...sources);
}

console.log(deepMerge(obj2, obj1));
Sohail Ashraf
  • 10,078
  • 2
  • 26
  • 42
1

You probably want some kind of recursive function to go through all the keys and check if they exist on the other object, something like:

let a = {
  title: "test",
  description: "desc",
  tags: ["tag1", "tag2"],
};

let b = {
  title: "",
  description: "",
  tags: [],
  stores: [{
      id: 1,
      title: "",
      description: "",
      tags: "",
      template: "",
      link: "",
      active: true,
    },
    {
      id: 2,
      title: "",
      description: "",
      tags: "",
      template: "",
      link: "",
      active: false,
    },
  ],
};

const merge = (a, b) => {
  Object.keys(b).forEach((key) => {
    if (typeof b[key] === "object" && key !== 'tags') {
      merge(a, b[key]);
    }

    if (Object.keys(a).includes(key)) {
      b[key] = a[key];
    }
  });
};

merge(a, b);

console.log(b);

console.log(b.stores[0].tags); // [ 'tag1', 'tag2' ]

I had to add a check for the case of the key tags which even though is an object, we don't want to go deeper into it. Nonetheless, this should point you in the right direction.

Luís Ramalho
  • 10,018
  • 4
  • 52
  • 67
0

let obj = {
  title: 'test',
  description: 'desc',
  tags: ['tag1','tag2']
};

let obj2 = {
 title: '',
 description: '',
 tags: [],
 stores: [
    {
        id: 1,
        title: '',
        description: '',
        tags: '',
        template: '',
        link: '',
        active: true
    },
    {
        id: 2,
        title: '',
        description: '',
        tags: '',
        template: '',
        link: '',
        active: false
    }
 ]
}

console.log(Object.assign(obj2, obj))

Just using Object.assign, and flip between two object as parameter will do

Isaac
  • 12,042
  • 16
  • 52
  • 116