1
let array1 = [{name:"ABC",add:"XYZ"},{name:"PQR",add:"CCC"}];

let array2 = [1,2]

I want output to be

array1=[{name:"ABC",add:"XYZ",num:1},{name:"PQR",add:"CCC",num:2}];

And I was also wondering if my array2 was also array of object with a weird key name but in resultant array i want a good key name like

let array1 = [{name:"ABC",add:"XYZ"},{name:"PQR",add:"CCC"}];

let array2=[{weirdKey:1},{weirdKey:2}];

but resultant array will be neat like

array1=[{name:"ABC",add:"XYZ",num:1},{name:"PQR",add:"CCC",num:2}];
2
  • Will both arrays always be of equal size? Commented Apr 7, 2020 at 10:11
  • yes both will be same in size Commented Apr 7, 2020 at 10:15

6 Answers 6

2

You could do something like the following (assuming the same length for both arrays):

array1 = array1.map((el, i) => {
    el.num = array2[i];
    return el;
});

Edit: For your second case, you can do the following if you're not sure what the weirdKey will be:

array1 = array1.map((el, i) => {
    el.num = Object.values(array2[i])[0];
    return el;
});
Sign up to request clarification or add additional context in comments.

Comments

1

This following will work for both your scenarios:

let array1 = [{ name: "ABC", add: "XYZ" }, { name: "PQR", add: "CCC" }];
let array2 = [{ weirdKey: 1 }, { weirdKey: 2 }];

array1.forEach((element, i) => {
  array1[i].num = array2[i].weirdKey ? array2[i].weirdKey : array2[i];
});

console.log(array1);

5 Comments

array1[i]["num"] will return the value of num, not the whole object
Sorry, didn't get what you are trying to convey. But it gives the desired output for either of the conditions.
you use map to change globally, why?
OP wanted the change to be applied to array1
then uses forEach
1

Try this below :

If array2 is just a list :

let array1 = [{name:"ABC",add:"XYZ"},{name:"PQR",add:"CCC"}];
                let array2 = [1,2];



                array1 = array1.map(function (item_1, index_1) {
                    array2.map(function (item_2, index_2) {
                        if (index_1 == index_2) {
                            item_1['num'] = item_2
                        }
                    });
                    return item_1
                });

                console.log(array1);

If array2 is list of objects , try this below :

array1 = array1.map(function (item_1, index_1) {
                    array2.map(function (item_2, index_2) {
                        if (index_1 == index_2) {
                            item_1['num'] = item_2['weirdKey']
                        }
                    });
                    return item_1
                });

                console.log(array1);

Comments

1

simple pure function:

const combineToArrayFn =
  (array1, array2, key = '') =>
    array1.map((item, i) => ({...item, num: array2[i][key] || array2[i]}));

const newArray = combineToArrayFn(array1, array2);

const newArray1 = combineToArrayFn(array1, array2, 'weirdKey');

Comments

1

To make it more simpler:

array1.forEach((e,index)=>{
 array1[index]["num"]=array2[index].weirdKey;
});

maybe this can solve your problem.
you can also use simple for loop on array1 instead of forEach() or map() function.

Comments

1

try use map() method for arrays

// without weirdKey
let array1 = [{ name: "ABC", add: "XYZ" }, { name: "PQR", add: "CCC" }]
let array2 = [1, 2];

array1.map((item, index) => item.num = (array2[index].weirdKey) ? array2[index].weirdKey : array2[index])
console.log(array1)

// with weirdKey same function
let array3 = [{ name: "ABC", add: "XYZ" }, { name: "PQR", add: "CCC" }]
let array4 = [{ weirdKey: 1 }, { weirdKey: 2 }];

array3.map((item, index) => item.num = (array4[index].weirdKey) ? array4[index].weirdKey : array4[index])
console.log(array3)

2 Comments

can u tell me when to use map and when to use forEach?
forEach use when you don’t need to return any result

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.