代码之家  ›  专栏  ›  技术社区  ›  Nicolás A. Rodríguez

在对象数组中添加对象Javascript

  •  0
  • Nicolás A. Rodríguez  · 技术社区  · 7 年前

    我需要在数组中添加一个由键标识的对象,该数组指定 course 所有物

    阵列

    [{
      "course": 1035, <- The id to find in the objects
      "start_time": "2018-01-04T20:55:00Z",
      "end_time": "2018-01-04T22:00:00Z",
      "details": { <- it has to be created
        <- here the corresponding object according to the id, 
            in this case 1035
      }
     }, {
      "course": 1106,
      "start_time": "2018-01-04T21:00:00Z",
      "end_time": "2018-01-04T22:00:00Z",
    }]
    

    对象

    {
      "1035": {
        "id": 1035,
        "title": "Some Title1",
        "slug": "live-show",
        "free": true,
        "order": 0,
        "color": "#CB13A1"
      },
      "1106": {
        "id": 1106,
        "title": "Some Title2",
        "slug": "live-show",
        "free": true,
        "order": 0,
        "color": "#CB13A1"
      }
    }
    

    预期结果

    [{
      "course": 1035,
      "start_time": "2018-01-04T20:55:00Z",
      "end_time": "2018-01-04T22:00:00Z",
      "details": {
        "id": 1035,
        "title": "Some Title1",
        "slug": "live-show",
        "free": true,
        "order": 0,
        "color": "#CB13A1"
      }
     }, {
      "course": 1106,
      "start_time": "2018-01-04T21:00:00Z",
      "end_time": "2018-01-04T22:00:00Z",
      "details": {
        "id": 1106,
        "title": "Some Title2",
        "slug": "live-show",
        "free": true,
        "order": 0,
        "color": "#CB13A1"
      }
    }]
    
    3 回复  |  直到 7 年前
        1
  •  2
  •   Nayeem Zen    7 年前

    一行代码而不改变对象/数组(这通常是个坏主意)。

    target.map(item => Object.assign({}, item, { details: source[item.course]}));
    

    哪里 target = The Array source = The Objects

        2
  •  2
  •   Michele Dibenedetto    7 年前

    var arr =[{
      "course": 1035, 
      "start_time": "2018-01-04T20:55:00Z",
      "end_time": "2018-01-04T22:00:00Z",
      "details": {
      }
     }, {
      "course": 1106,
      "start_time": "2018-01-04T21:00:00Z",
      "end_time": "2018-01-04T22:00:00Z",
    }];
    
    var obj = {
      "1035": {
        "id": 1035,
        "title": "Some Title1",
        "slug": "live-show",
        "free": true,
        "order": 0,
        "color": "#CB13A1"
      },
      "1106": {
        "id": 1106,
        "title": "Some Title2",
        "slug": "live-show",
        "free": true,
        "order": 0,
        "color": "#CB13A1"
      }
    };
    
    arr.forEach(item=>{ 
           item.details = obj[item.course];
      });
    console.log(arr);
    
    /*RESULT:
    [
      {
        "course": 1035,
        "start_time": "2018-01-04T20:55:00Z",
        "end_time": "2018-01-04T22:00:00Z",
        "details": {
          "id": 1035,
          "title": "Some Title1",
          "slug": "live-show",
          "free": true,
          "order": 0,
          "color": "#CB13A1"
        }
      },
      {
        "course": 1106,
        "start_time": "2018-01-04T21:00:00Z",
        "end_time": "2018-01-04T22:00:00Z",
        "details": {
          "id": 1106,
          "title": "Some Title2",
          "slug": "live-show",
          "free": true,
          "order": 0,
          "color": "#CB13A1"
        }
      }
    ]
    */
        3
  •  0
  •   Sergii Rudenko    7 年前

    您可以这样做:

    Object.keys(object).map(key => {
      const obj = object[key]
      // here, you can add all missed keys to your object
      return obj
    })