Javascript对象-将对象数组与一些经典对象合并

时间:2018-08-20 01:01:10

标签: javascript ecmascript-6

我正在尝试将javascript中的对象数组与一些经典对象合并。但是我得到了一个奇怪的大对象,其中包含一些对象数组和一些个体对象,如下所示:

Here a demo of my configuration

这是我要合并的两个对象:

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"
  }
];

希望的结果:

       // 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 
      }

从我的console.log中获得的结果:

// 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}
}

object2 =

到目前为止,我已经尝试了以下不同方法:

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) 

据我所知,它们都不起作用。 为什么我的对象不能正确合并?任何提示都会很棒,  谢谢。

1 个答案:

答案 0 :(得分:4)

只需将object2映射到新的对象数组中即可。为object2中的每个对象创建一个新对象,该对象是它与object1中的等效对象的融合(使用id属性进行检索):

var result = object2.map(o => Object.assign({}, o, object1[o.id]));

示例:

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);