1

I have two arrays details and options that coming from different sources (web api requests)

details: [
      { id: 'groups', option: true },
      { id: 'category', option: false }
]


 options: {
        groups: [ 
          { id: 'g1' },
          { id: 'g2' }
        ],
        category: [
          { id: 'c1' },
          { id: 'c2' }
        ],
        other: [
          { id: 'o1' },
          { id: 'o2' }
        ],
    }

I want to combine these tow arrays like

combined: [
        groups: 
            {
                options:[ 
                    { id: 'g1' },
                    { id: 'g2' }
                ], 
                details: { option: true}
            },
        category: 
                {
                 options:   [ 
                    { id: 'c1' },
                    { id: 'c2' }
                ], 
                details: { option: false}
            },
    ]

Basically if any id from details is matching to options property it should go in to new array under the same property name and all details except id goes to related details property.

What is the best way of doing that? Is lodash can handle that ?

5
  • 1
    Please add what you have tried, so that we can help you to solve the problem you are facing. Commented Oct 30, 2017 at 3:48
  • 2
    I'd say you have just one array. Commented Oct 30, 2017 at 3:51
  • @GerardoFurtado just updated the question, I am not building two arrays myself, they are coming form external api. So that is why I want to build one array based on these two. Commented Oct 30, 2017 at 3:55
  • options isn't an array, it's an object Commented Oct 30, 2017 at 4:09
  • Your question contains invalid code. And your expected combined array is not a valid array. You should take your time to ask and show your effort while asking a question, which is only fair since you're asking for help. Please read this - stackoverflow.com/help/how-to-ask Commented Oct 30, 2017 at 4:18

3 Answers 3

1

If you only want the items in both options and details (intersection):

var details = [
      { id: 'groups', option: true },
      { id: 'category', option: false }
]

var options = {
        groups: [ 
          { id: 'g1' },
          { id: 'g2' }
        ],
        category: [
          { id: 'c1' },
          { id: 'c2' }
        ],
        other: [
          { id: 'o1' },
          { id: 'o2' }
        ]
    }

var combined = {};
details.forEach(({id: id, option: option}) => {
  if (options[id]) {
    combined[id] = combined[id] || {};
    combined[id].options = options[id];
    combined[id].details = {option: option};
  }
})

console.log(JSON.stringify(combined, null, "\t"))
/*
{
  "groups": {
    "options": [
      {
        "id": "g1"
      },
      {
        "id": "g2"
      }
    ],
    "details": {
      "option": true
    }
  },
  "category": {
    "options": [
      {
        "id": "c1"
      },
      {
        "id": "c2"
      }
    ],
    "details": {
      "option": false
    }
  }
}
*/

If you want to retain all items from options and details whether or not they match (union):

var details = [
      { id: 'groups', option: true },
      { id: 'category', option: false }
]

var options = {
        groups: [ 
          { id: 'g1' },
          { id: 'g2' }
        ],
        category: [
          { id: 'c1' },
          { id: 'c2' }
        ],
        other: [
          { id: 'o1' },
          { id: 'o2' }
        ]
    }

var combined = {};

Object.keys(options).forEach(id => {
  combined[id] = {};
  combined[id].options = options[id];
})
details.forEach(({id: id, option: option}) => {
  combined[id] = combined[id] || {};
  combined[id].details = {option: option};
})

console.log(JSON.stringify(combined, null, "\t"))
/*
{
  "groups": {
    "options": [
      {
        "id": "g1"
      },
      {
        "id": "g2"
      }
    ],
    "details": {
      "option": true
    }
  },
  "category": {
    "options": [
      {
        "id": "c1"
      },
      {
        "id": "c2"
      }
    ],
    "details": {
      "option": false
    }
  },
  "other": {
    "options": [
      {
        "id": "o1"
      },
      {
        "id": "o2"
      }
    ]
  }
}
*/
Sign up to request clarification or add additional context in comments.

Comments

0

You need to use [] notation to push details values.

options['groups']['details'] = true
options['groups']['category'] = false

Comments

0

Here's the solution for your problem

var details= [
      { id: 'groups', option: true },
      { id: 'category', option: false }
];


var options= {
        groups: [ 
          { id: 'g1' },
          { id: 'g2' }
        ],
        category: [
          { id: 'c1' },
          { id: 'c2' }
        ],
        other: [
          { id: 'o1' },
          { id: 'o2' }
        ],
    };
 
var combined = {};

for (var i= 0;i<details.length;i++){
var obj = {}
obj.options=options[details[i].id];
obj.details = {};
obj.details.option=details[i].option;
combined[details[i].id] = obj;

}

console.log(combined)

Comments

Your Answer

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.