1

I have two javascript array

let a=[{id:1},{id:2},{id:3}];
let b=[{count:35, name:'test', age:12}, {count:45 name:'test2', age:9}];

and two array push into i need final Array format is

[{count:35,name:'test', age,12, id:1} , {count:35, name:'test', age:12,id:2},{count:35, name:'test', age:12,id:3},{count:45,name:'test', age,9, id:1} , {count:45, name:'test', age:9,id:2},{count:45, name:'test', age:9,id:3} ]

And i trying

for ( var index=0; index<b.length; index++ ) {
 
 for ( var j=0; j<a.length; j++ ) {
 for (const [key, value] of Object.entries(a[j])) {
 //if(j==index)
 b[index][key]=value;
 k[j]=b[index];
 }
 console.log( b[index]);
 c.push(b[index]);
 
 }
}
console.log(c);

and it shows final value is enter image description here

please any body help to fix the problem

Scott Marcus
66k6 gold badges54 silver badges81 bronze badges
asked Jul 10, 2020 at 18:52
1
  • I hope that clears things up. Commented Jul 12, 2020 at 6:28

5 Answers 5

2

Your current implementation, is essentially updating the same b objects over and over, x number of times, changing their IDs each time through, ending at 3.

You need to "clone" the objects, so they are separate objects, like so:

 let p = []; // Permutations
 
 let a = [{id:1},{id:2}]; // Renditions...
 // Presets...
 let b = [{count:35, name:'test', age:12}, {count:45, name:'test2', age:9}];
 
 // for each Rendition
 a.forEach(function(a) {
 // for each Preset
 b.forEach(function(b){
 // generate Permutation and Clone, object of b with additional id prop
 p.push(Object.assign({id: a.id}, b)); // <-- Cloned here...
 });
 });
 console.log(p)

for the sake of clarity, you might consider changing the id prop to grouping or group or group_id.

answered Jul 10, 2020 at 19:03
Sign up to request clarification or add additional context in comments.

Comments

2

You could use flatMap:

let a=[{id:1},{id:2},{id:3}];
let b=[{count:35, name:'test', age:12}, {count:45, name:'test2', age:9}];
let m = b.flatMap(itemB => a.map(itemA => ({...itemB, ...itemA })))
console.log(m);

answered Jul 10, 2020 at 22:47

Comments

0

I got your problem. The thing is that you are mutating the same b object every iteration, that's why in the end you get id = 3 for every element. You can use a deep copy of b in each cycle, and the error should be gone.

Here's one way to deep clone, not the most efficient but it is very clear:

 const bCopy = JSON.parse(JSON.stringify(b[index]));
 for (const [key, value] of Object.entries(a[j])) {
 //if(j==index)
 bCopy[key]=value;
 k[j]=bCopy; // what's this for?
 }
 console.log(bCopy);
 c.push(bCopy);
answered Jul 10, 2020 at 19:09

Comments

0

Reduce b and for each item b1, reduce a. When reducing a, just assign both b1 and a1 to a new object.

const expectedValue = JSON.stringify(JSON.parse(
 document.getElementById('expected').value))
let a = [
 { id: 1 },
 { id: 2 },
 { id: 3 }
]
let b = [
 { count: 35, name: 'test', age: 12 },
 { count: 45, name: 'test2', age: 9 }
]
let c = b.reduce((res, b1) =>
 a.reduce((res1, a1) =>
 [...res1, Object.assign({}, b1, a1)], res), [])
console.log(JSON.stringify(c) === expectedValue)
.as-console-wrapper { top: 0; max-height: 100% !important; }
#expected { display: none; }
<textarea id="expected">
[
 { "count": 35, "name": "test", "age": 12, "id": 1 },
 { "count": 35, "name": "test", "age": 12, "id": 2 },
 { "count": 35, "name": "test", "age": 12, "id": 3 },
 { "count": 45, "name": "test2", "age": 9, "id": 1 },
 { "count": 45, "name": "test2", "age": 9, "id": 2 },
 { "count": 45, "name": "test2", "age": 9, "id": 3 }
]
</textarea>

answered Jul 10, 2020 at 19:12

Comments

0

Try this changes.

 let a=[{id:1},{id:2},{id:3}];
 let b=[{count:35, name:'test', age:12}, {count:45, name:'test2', age:9}];
 let c=[];
 for ( var index=0; index<b.length; index++ ) {
 for ( var j=0; j<a.length; j++ ) {
 var obj=b[index];
 for (const [key, value] of Object.entries(a[j])) {
 obj[key]=value;
 }
 c.push(obj);
 }
 }
 console.log(c);
answered Jul 10, 2020 at 19:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.