-1
a = {
    coffee: 'Coffees',
    mango: '10 Mangos',
    shoe: '2 Shoes',
    bag: '5 Bags',
    abc: 'D E F'
}

b = {
    coffee,
    abc,
    bag,
}


mergerd_output_will_be = {
    coffee: 'Coffees',
    abc: 'D E F',
    bag: '5 Bags'
}

How can I merger 'a' object like this? 'b' object key will be included only outputted object.

quirimmo
  • 9,800
  • 3
  • 30
  • 45
AkrAm
  • 1
  • 2
  • 7
    In your code example, "b" is not a valid JavaScript object. – Reactgular Jan 14 '19 at 13:48
  • 3
    Why is this tagged PHP and Laravel? – Jonnix Jan 14 '19 at 13:49
  • Possible duplicate of [Surely ES6+ must have a way to merge two javascript objects together, what is it?](https://stackoverflow.com/questions/13852852/surely-es6-must-have-a-way-to-merge-two-javascript-objects-together-what-is-it) – quirimmo Jan 14 '19 at 13:49
  • Possible duplicate of [Best way to get intersection of keys of two objects?](https://stackoverflow.com/questions/34392741/best-way-to-get-intersection-of-keys-of-two-objects) – Hussain Ali Akbar Jan 14 '19 at 13:51
  • 1
    @cgTag I think it's shorthand property names (ES2015). It's a valid example – R3tep Jan 14 '19 at 13:55

5 Answers5

2

You can use Array.reduce and construct an object with each key value.

    let a = {
        coffee: 'Coffees',
        mango: '10 Mangos',
        shoe: '2 Shoes',
        bag: '5 Bags',
        abc: 'D E F'
    }
    let b = {'coffee':null, 'abc':null, 'bag':null};
    let c = Object.keys(b).reduce((current,key)=>({...current, [key]:a[key]}), {});
    console.log(c);
Reactgular
  • 52,335
  • 19
  • 158
  • 208
1

You _.pick() from object a the _.keys() of object b:

const a = {
  coffee: 'Coffees',
  mango: '10 Mangos',
  shoe: '2 Shoes',
  bag: '5 Bags',
  abc: 'D E F'
}

const b = {
  coffee: null,
  abc: null,
  bag: null
}

const result = _.pick(a, _.keys(b))

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
0

Initial code that you've provided is invalid (variable b)

You can make something like this:


const a = {
    coffee: 'Coffees',
    mango: '10 Mangos',
    shoe: '2 Shoes',
    bag: '5 Bags',
    abc: 'D E F'
}

const props = ['coffee', 'abc', 'bag'];

const mergerd_output_will_be = {};
props.forEach(propName => mergerd_output_will_be[propName] = a[propName]);

or

const a = {
    coffee: 'Coffees',
    mango: '10 Mangos',
    shoe: '2 Shoes',
    bag: '5 Bags',
    abc: 'D E F'
}

const b = {coffee: null, abc: null, bag: null};

const mergerd_output_will_be = {...b};
Object.keys(mergerd_output_will_be).forEach(propName => 
  mergerd_output_will_be[propName]=a[propName]
)
Danil Gudz
  • 2,233
  • 16
  • 16
0

basically you have 2 ways, using ... spread operator or Object.assign()

const coffee = 'Coffees'
const abc = 'abc';
const bag = 'bag'

const a = {
  coffee: 'Coffees',
  mango: '10 Mangos',
  shoe: '2 Shoes',
  bag: '5 Bags',
  abc: 'D E F'
}

const b = {
  coffee,
  abc,
  bag,
}

const result1 = {...a, ...b};
const result2 = Object.assign({}, a, b)

console.log(result1)
console.log(result2)
Prince Hernandez
  • 3,623
  • 1
  • 10
  • 19
  • 3
    the answer does not seem to be right because the OP wants an intersection of 2 objects instead of a union. The merged_object in the description contains only the common fields. Your answer will merge both objects. – Hussain Ali Akbar Jan 14 '19 at 14:03
  • @HussainAliAkbar totally right, I did not get the question of the OP properly and I even upvoted this answer. Now I cannot even remove my upvote -.- By the way, let's say that the title is unclear, because "merge" two objects means what this answer says – quirimmo Jan 14 '19 at 14:10
  • yup you are right the title is unclear. What the OP wants is the intersection of 2 objects and not "merge". – Hussain Ali Akbar Jan 14 '19 at 14:25
0

First, an Object should be Key-value Pairs in it, for this you can change b to array such as b = ['coffee', 'D E F', '5 Bags']

Then, use the below code to produce the expected output.

let a = {
  coffee: 'Coffees',
  mango: '10 Mangos',
  shoe: '2 Shoes',
  bag: '5 Bags',
  abc: 'D E F'
 };
 let b = ['coffee', 'abc', 'bag'];
let output = {};
for( let i = 0; i < b.length; i++) {
 output[b[i]] = a[b[i]];
}
Lokeshkumar S
  • 256
  • 2
  • 6