0

I need to add an object identified by a key in an array that specifies the id in the course property.

The Array

[{
 "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",
}]

The Objects

{
 "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"
 }
}

The expected 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"
 }
}]
asked Jan 3, 2018 at 18:24
1
  • 1
    see if one the answers solve your problem Commented Jan 3, 2018 at 18:43

3 Answers 3

2

One-liner without mutating your objects/arrays (which is generally a bad idea).

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

where target = The Array and source = The Objects

answered Jan 3, 2018 at 18:56
Sign up to request clarification or add additional context in comments.

Comments

2

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"
 }
 }
]
*/

answered Jan 3, 2018 at 18:38

Comments

0

You can do it like this:

Object.keys(object).map(key => {
 const obj = object[key]
 // here, you can add all missed keys to your object
 return obj
})
answered Jan 3, 2018 at 18: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.