1

my arrays :

const create = [{"month":"Jan","createCount":"4"},{"month":"Feb","createCount":"5"}];

const close = [{"month":"Jan","closeCount":"3"},{"month":"Feb","closeCount":"5"}];

I need to merge the arrays objects to comparing month field.

I want my result like this...

const data = [{"month":"Jan","createCount":"4","closeCount":"3"},{"month":"Feb","createCount":"5","closeCount":"5"}];

I tried this code:

const data = [...create, ...close];

but this as merge the two array data only.

please give any solution to me!

4 Answers 4

3

You can use .map() and Object.assign() methods:

const create = [{"month":"Jan","createCount":"4"}, {"month":"Feb","createCount":"5"}],
      close = [{"month":"Jan","closeCount":"3"}, {"month":"Feb","closeCount":"5"}];

const result = create.map(
    o => Object.assign({}, o, close.find(({month}) => month === o.month))
);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

3 Comments

In my opinion - Using find within map function is going to hit the performance
@NikhilAggarwal May be but it depends on the data in arrays also.
Irrespective of the data, performance will be less. However, smaller data set will have less impact may be negligible.
1

Assuming that there will be no such entry in create array that does not exists in close array and vice-versa

Try following using Array.reduce, Array.map and Object.assign

const create = [{"month":"Jan","createCount":"4"},{"month":"Feb","createCount":"5"}];

const close = [{"month":"Jan","closeCount":"3"},{"month":"Feb","closeCount":"5"}];

// Create an object with key as month and createCount as value
let obj = create.reduce((a, o) => Object.assign(a, {[o.month]: o.createCount}), {});

// Iterate over close array and add createCount from obj
let result = close.map(o => ({...o, createCount : obj[o.month]}));
console.log(result);

Comments

0

simply using for in

const create = [{ "month": "Jan", "createCount": "4" }, { "month": "Feb", "createCount": "5" }];
const close = [{ "month": "Jan", "closeCount": "3" }, { "month": "Feb", "closeCount": "5" }];
for (var i in create) {
    var item = create[i];
    item.closeCount = close.find(x => x.month == item.month).closeCount;
}
console.log(create);

Comments

0

Here I have added some Sample please refer that code

const create = [{"month":"Jan","createCount":"4"},{"month":"Feb","createCount":"5"}];
const close = [{"month":"Jan","closeCount":"3"},{"month":"Feb","closeCount":"5"}];
console.log(create.length);

    for(var data = 0 ; data < create.length ; data++){ 
    var datas = []
    if(create[data].month == close[data].month){ 
        datas=Object.assign(create[data],close[data]);console.log(create[data]);
    }
}

Comments

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.