most recent 30 from stackoverflow.com
2026-03-01T06:34:36Z
https://stackoverflow.com/feeds/question/51923006
https://creativecommons.org/licenses/by-sa/4.0/rdf
https://stackoverflow.com/q/51923006
1
Diagathe Josué
https://stackoverflow.com/users/9817602
2018-08-20T01:01:09Z
2018-08-20T01:32:53Z
<p>I'm trying to merge an array of object in javascript with some classicals objects. But I obtain a weird big object with some array of object and some individuals objects like this: </p>
<p><a href="https://codesandbox.io/s/x2q3jwp1rq" rel="nofollow noreferrer">Here a demo of my configuration</a>.</p>
<p>Here the two objects I want to merge: </p>
<pre><code>var object1 = {
"5b7973bdce3eb938a6de86a3": { addAt: 1534727291296, quantity: 1 },
"5b7973bdce3eb938a6de86a5": { addAt: 1534727292889, quantity: 1 },
"5b7973bdce3eb938a6de86a7": { addAt: 1534727297106, quantity: 1 }
};
var object2 = [
{
id: "5b7973bdce3eb938a6de86a3",
category: "main dish",
ingredients:
" Peeled groundnuts ↵ Onion, garlic, pepper, salt ↵ Vegetable oil ↵ Ground crayfish ↵ Meat",
price: "5.50"
},
{
id: "5b7973bdce3eb938a6de86a5",
category: "entry",
ingredients:
"1.2kg (2.6 lbs) goat meat (cut with the skin) ↵ 2 …g spoon vegetable oil. ↵ Black pepper (optional)",
price: "6.50"
},
{
id: "5b7973bdce3eb938a6de86a7",
category: "appetizers",
ingredients:
"2 cups of warm water. ↵ 2 tsp. baking powder. ↵ …ar. ↵ 2 tsps. vegetable oil. ↵ Pinch of salt.",
price: "5.00"
}
];
</code></pre>
<p>Wished result: </p>
<pre><code> // for each object, merge addAt and quantity with previous object
{
id: "5b7973bdce3eb938a6de86a7",
category: "appetizers",
ingredients:
"2 cups of warm water. ↵ 2 tsp. baking powder. ↵ …ar. ↵ 2 tsps. vegetable oil. ↵ Pinch of salt.",
price: "5.00",
quantity: 1,
addAt: 1534727297106
}
</code></pre>
<p>Obtained result from my console.log: </p>
<pre><code>// one big object, not merged
{
0:
{id: "5b7973bdce3eb938a6de86a3", category: "main dish", ingredients: " Peeled groundnuts ↵ Onion, garlic, pepper, salt, maggi ↵ Vegetable oil ↵ Ground crayfish ↵ Meat", price: "5.50", …}
1:
{id: "5b7973bdce3eb938a6de86a5", category: "entry", ingredients: "1.2kg (2.6 lbs) goat meat (cut with the skin) ↵ 2 …g spoon vegetable oil. ↵ Black pepper (optional)", price: "6.50", …}
2:
{id: "5b7973bdce3eb938a6de86a7", category: "appetizers", ingredients: "2 cups of warm water. ↵ 2 tsp. baking powder. ↵ …ar. ↵ 2 tsps. vegetable oil. ↵ Pinch of salt.", price: "5.00", …}
5b7973bdce3eb938a6de86a3:
{addAt: 1534727291296, quantity: 1}
5b7973bdce3eb938a6de86a5:
{addAt: 1534727292889, quantity: 1}
5b7973bdce3eb938a6de86a7:
{addAt: 1534727297106, quantity: 1}
}
</code></pre>
<p>object2 = </p>
<p>I have tried so far these differents methods: </p>
<pre><code>Object.keys(this.props.data.compilation).map((key, index) => {
if(this.props.data.compilation[key] === this.props.articles[index]) {
this.props.data.compilation[index].quantity = this.props.articleID[key]["quantity"]
return compilation = Object.assign({}, this.props.data.compilation[key], {
quantity : this.props.articles[index][quantity]
}, {
addAt: this.props.articles[index][addAt]
})
}
})
Object.keys(this.props.data.compilation).map((key, index) => {
if(this.props.data.compilation[key] === this.props.articles[index]) {
this.props.data.compilation[index].addAt = this.props.articleID[key]["addAt"],
this.props.data.compilation[index].quantity = this.props.articleID[key]["quantity"]
}
})
// lodash method
var compilation = _.merge( {}, this.props.data.compilation, this.props.articles)
//lodash method
var compilation = _.assign( {}, this.props.data.compilation, this.props.articles)
</code></pre>
<p>None of them works so far as I would.
Why my object doesn't merge properly ? Any hint would be great,
thanks.</p>
https://stackoverflow.com/questions/51923006/-/51923160#51923160
4
ibrahim mahrir
https://stackoverflow.com/users/9867451
2018-08-20T01:32:53Z
2018-08-20T01:32:53Z
<p>Just map <code>object2</code> into a new array of objects. For each object in <code>object2</code> create a new object that is the fusion of it with the equivalent object from <code>object1</code> (retrieved using the <code>id</code> property):</p>
<pre><code>var result = object2.map(o => Object.assign({}, o, object1[o.id]));
</code></pre>
<p><strong>Example:</strong></p>
<p><div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="false">
<div class="snippet-code snippet-currently-hidden">
<pre class="snippet-code-js lang-js prettyprint-override"><code>var object1 = {"5b7973bdce3eb938a6de86a3":{"addAt":1534727291296,"quantity":1},"5b7973bdce3eb938a6de86a5":{"addAt":1534727292889,"quantity":1},"5b7973bdce3eb938a6de86a7":{"addAt":1534727297106,"quantity":1}};
var object2 = [{"id":"5b7973bdce3eb938a6de86a3","category":"main dish","ingredients":" Peeled groundnuts ↵ Onion, garlic, pepper, salt ↵ Vegetable oil ↵ Ground crayfish ↵ Meat","price":"5.50"},{"id":"5b7973bdce3eb938a6de86a5","category":"entry","ingredients":"1.2kg (2.6 lbs) goat meat (cut with the skin) ↵ 2 …g spoon vegetable oil. ↵ Black pepper (optional)","price":"6.50"},{"id":"5b7973bdce3eb938a6de86a7","category":"appetizers","ingredients":"2 cups of warm water. ↵ 2 tsp. baking powder. ↵ …ar. ↵ 2 tsps. vegetable oil. ↵ Pinch of salt.","price":"5.00"}];
var result = object2.map(o => Object.assign({}, o, object1[o.id]));
console.log(result);</code></pre>
</div>
</div>
</p>