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
-
I hope that clears things up.Open AI - Opting Out– Open AI - Opting Out2020年07月12日 06:28:04 +00:00Commented Jul 12, 2020 at 6:28
5 Answers 5
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.
Comments
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);
Comments
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);
Comments
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>
Comments
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);